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