2 opendir -- open a directory stream
4 last edit: 16-Jun-1987 D A Gwyn
10 #include <sys/types.h>
14 typedef void *pointer; /* (void *) if you have it */
16 extern int _open(const char *path, int flags, int mode);
17 extern int _close(int d);
18 extern int _fstat(int fd, struct stat *buf);
28 #ifndef S_ISDIR /* macro to test for directory file */
29 #define S_ISDIR( mode ) (((mode) & S_IFMT) == S_IFDIR)
33 opendir(const char *dirname) /* name of directory */
35 register DIR *dirp; /* -> malloc'ed storage */
36 register int fd; /* file descriptor for read */
37 struct stat sbuf; /* result of fstat() */
39 if ( (fd = _open( dirname, O_RDONLY, 0 )) < 0 )
40 return NULL; /* errno set by open() */
42 if ( _fstat( fd, &sbuf ) != 0 || !S_ISDIR( sbuf.st_mode ) )
46 return NULL; /* not a directory */
49 if ( (dirp = (DIR *)malloc( sizeof(DIR) )) == NULL
50 || (dirp->dd_buf = (char *)malloc( (unsigned)DIRBUF )) == NULL
52 register int serrno = errno;
53 /* errno set to ENOMEM by sbrk() */
56 free( (pointer)dirp );
60 return NULL; /* not enough memory */
64 dirp->dd_loc = dirp->dd_size = 0; /* refill needed */