Line | |
---|
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>
|
---|
19 |
|
---|
20 | DIR *opendir(const char *name)
|
---|
21 | /* Open a directory for reading. */
|
---|
22 | {
|
---|
23 | int d, f;
|
---|
24 | DIR *dp;
|
---|
25 | struct stat st;
|
---|
26 |
|
---|
27 | /* Only read directories. */
|
---|
28 | if (stat(name, &st) < 0) return nil;
|
---|
29 | if (!S_ISDIR(st.st_mode)) { errno= ENOTDIR; return nil; }
|
---|
30 |
|
---|
31 | if ((d= open(name, O_RDONLY | O_NONBLOCK)) < 0) return nil;
|
---|
32 |
|
---|
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;
|
---|
44 | }
|
---|
45 |
|
---|
46 | dp->_fd= d;
|
---|
47 | dp->_v7= -1;
|
---|
48 | dp->_count= 0;
|
---|
49 | dp->_pos= 0;
|
---|
50 |
|
---|
51 | return dp;
|
---|
52 | }
|
---|
53 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.