Annotation of 43BSDReno/contrib/isode-beta/dirent/opendir.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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