1 /* opendir() Author: Kees J. Bot
10 #define opendir _opendir
12 #include <sys/types.h>
20 DIR *opendir(const char *name
)
21 /* Open a directory for reading. */
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. */
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