Annotation of coherent/b/lib/libc/ndir/opendir.c, revision 1.1.1.1

1.1       root        1: /*
                      2:        opendir -- open a directory stream
                      3: 
                      4:        last edit:      25-Apr-1987     D A Gwyn
                      5: */
                      6: 
                      7: #ifdef COHERENT
                      8: #include       <errno.h>
                      9: #include       <fcntl.h>
                     10: #else
                     11: #include       <sys/errno.h>
                     12: #endif
                     13: 
                     14: #include       <sys/types.h>
                     15: #include       <sys/stat.h>
                     16: #include       "dirent.h"
                     17: 
                     18: #ifdef BRL
                     19: #define open   _open                   /* avoid emulation overhead */
                     20: #endif
                     21: 
                     22: typedef char   *pointer;               /* (void *) if you have it */
                     23: 
                     24: extern void    free();
                     25: extern pointer malloc();
                     26: extern int     open(), close(), fstat();
                     27: 
                     28: extern int     errno;
                     29: 
                     30: #ifndef NULL
                     31: #define        NULL    0
                     32: #endif
                     33: 
                     34: #ifndef O_RDONLY
                     35: #define        O_RDONLY        0
                     36: #endif
                     37: 
                     38: #ifndef S_ISDIR                                /* macro to test for directory file */
                     39: #define        S_ISDIR( mode )         (((mode) & S_IFMT) == S_IFDIR)
                     40: #endif
                     41: 
                     42: DIR *
                     43: opendir( dirname )
                     44:        char            *dirname;       /* name of directory */
                     45:        {
                     46:        register DIR    *dirp;          /* -> malloc'ed storage */
                     47:        register int    fd;             /* file descriptor for read */
                     48:        struct stat     sbuf;           /* result of fstat() */
                     49: 
                     50:        if ( stat( dirname, &sbuf ) != 0 || !S_ISDIR( sbuf.st_mode ) )
                     51:                {
                     52:                errno = ENOTDIR;
                     53:                return NULL;            /* not a directory */
                     54:                }
                     55: 
                     56:        if ( (fd = open( dirname, O_RDONLY )) < 0 )
                     57:                return NULL;            /* errno set by open() */
                     58: 
                     59:        if ( (dirp = (DIR *)malloc( sizeof(DIR) )) == NULL
                     60:          || (dirp->dd_buf = (char *)malloc( (unsigned)DIRBUF )) == NULL
                     61:           )    {
                     62:                register int    serrno = errno;
                     63:                                        /* errno set to ENOMEM by sbrk() */
                     64: 
                     65:                if ( dirp != NULL )
                     66:                        free( (pointer)dirp );
                     67: 
                     68:                (void)close( fd );
                     69:                errno = serrno;
                     70:                return NULL;            /* not enough memory */
                     71:                }
                     72: 
                     73:        dirp->dd_fd = fd;
                     74:        dirp->dd_loc = dirp->dd_size = 0;       /* refill needed */
                     75: 
                     76:        return dirp;
                     77:        }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.