1#include <dirent.h>
2#include <fcntl.h>
3#include <sys/stat.h>
4#include <errno.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include "__dirent.h"
8
9extern uint64_t __get_dir_tag(DIR* dir);
10
11DIR *fdopendir(int fd)
12{
13	DIR *dir;
14	struct stat st;
15
16	if (fstat(fd, &st) < 0) {
17		return 0;
18	}
19	if (fcntl(fd, F_GETFL) & O_PATH) {
20		errno = EBADF;
21		return 0;
22	}
23	if (!S_ISDIR(st.st_mode)) {
24		errno = ENOTDIR;
25		return 0;
26	}
27	if (!(dir = calloc(1, sizeof *dir))) {
28		return 0;
29	}
30
31	fcntl(fd, F_SETFD, FD_CLOEXEC);
32	dir->fd = fd;
33	fdsan_exchange_owner_tag(fd, 0, __get_dir_tag(dir));
34	return dir;
35}
36