secondary cache feature in vm.
[minix.git] / lib / libc / posix / _opendir.c
blob8ede6e60dc71bb842150b5072cb4b539db0e6277
1 /* opendir() Author: Kees J. Bot
2 * 24 Apr 1989
3 */
4 #define nil 0
5 #include <lib.h>
6 #define close _close
7 #define fcntl _fcntl
8 #define fstat _fstat
9 #define open _open
10 #define opendir _opendir
11 #define stat _stat
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <dirent.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <fcntl.h>
18 #include <errno.h>
20 DIR *opendir(const char *name)
21 /* Open a directory for reading. */
23 int d, f;
24 DIR *dp;
25 struct stat st;
27 /* Only read directories. */
28 if (stat(name, &st) < 0) return nil;
29 if (!S_ISDIR(st.st_mode)) { errno= ENOTDIR; return nil; }
31 if ((d= open(name, O_RDONLY | O_NONBLOCK)) < 0) return nil;
33 /* Check the type again, mark close-on-exec, get a buffer. */
34 if (fstat(d, &st) < 0
35 || (errno= ENOTDIR, !S_ISDIR(st.st_mode))
36 || (f= fcntl(d, F_GETFD)) < 0
37 || fcntl(d, F_SETFD, f | FD_CLOEXEC) < 0
38 || (dp= (DIR *) malloc(sizeof(*dp))) == nil
39 ) {
40 int err= errno;
41 (void) close(d);
42 errno= err;
43 return nil;
46 dp->_fd= d;
47 dp->_count= 0;
48 dp->_pos= 0;
50 return dp;