|
|
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 <sys/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 ( (fd = open( dirname, O_RDONLY )) < 0 )
51: return NULL; /* errno set by open() */
52:
53: if ( fstat( fd, &sbuf ) != 0 || !S_ISDIR( sbuf.st_mode ) )
54: {
55: (void)close( fd );
56: errno = ENOTDIR;
57: return NULL; /* not a directory */
58: }
59:
60: if ( (dirp = (DIR *)malloc( sizeof(DIR) )) == NULL
61: || (dirp->dd_buf = (char *)malloc( (unsigned)DIRBUF )) == NULL
62: ) {
63: register int serrno = errno;
64: /* errno set to ENOMEM by sbrk() */
65:
66: if ( dirp != NULL )
67: free( (pointer)dirp );
68:
69: (void)close( fd );
70: errno = serrno;
71: return NULL; /* not enough memory */
72: }
73:
74: dirp->dd_fd = fd;
75: dirp->dd_loc = dirp->dd_size = 0; /* refill needed */
76:
77: return dirp;
78: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.