928742c0daf492cfab0e38131d8e52afcb0e0f96
[musl] / src / dirent / opendir.c
1 #define _GNU_SOURCE
2 #include <dirent.h>
3 #include <fcntl.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <limits.h>
9 #include "__dirent.h"
10 #include "syscall.h"
11
12 DIR *opendir(const char *name)
13 {
14         int fd;
15         DIR *dir;
16
17         if ((fd = open(name, O_RDONLY|O_DIRECTORY)) < 0)
18                 return 0;
19         fcntl(fd, F_SETFD, FD_CLOEXEC);
20         if (!(dir = calloc(1, sizeof *dir))) {
21                 __syscall(SYS_close, fd);
22                 return 0;
23         }
24         dir->fd = fd;
25         return dir;
26 }