initial check-in, version 0.5.0
[musl] / src / dirent / fdopendir.c
1 #include <dirent.h>
2 #include <fcntl.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include "__dirent.h"
9
10 DIR *fdopendir(int fd)
11 {
12         DIR *dir;
13         struct stat st;
14
15         if (fstat(fd, &st) < 0 || !S_ISDIR(st.st_mode)) {
16                 errno = ENOTDIR;
17                 return 0;
18         }
19         if (!(dir = calloc(1, sizeof *dir))) {
20                 return 0;
21         }
22
23         fcntl(fd, F_SETFD, FD_CLOEXEC);
24         dir->fd = fd;
25         return dir;
26 }