regex: character classes
[neatlibc.git] / dirent.c
blob3154191d2ccf0abef07455088c8db646f1ac672d
1 #include <dirent.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
8 struct __dirent_dir {
9 int fd;
10 int buf_pos;
11 int buf_end;
12 char buf[2048];
15 DIR *opendir(char *path)
17 DIR *dir;
18 int fd;
19 if ((fd = open(path, O_RDONLY | O_DIRECTORY)) < 0)
20 return NULL;
21 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
22 if (!(dir = malloc(sizeof(*dir)))) {
23 close(fd);
24 return NULL;
26 memset(dir, 0, sizeof(*dir));
27 dir->fd = fd;
28 return dir;
31 int closedir(DIR *dir)
33 int ret;
34 ret = close(dir->fd);
35 free(dir);
36 return ret;
39 int getdents(int fd, struct dirent *de, size_t len);
41 struct dirent *readdir(DIR *dir)
43 struct dirent *de;
44 int len;
45 if (dir->buf_pos >= dir->buf_end) {
46 len = getdents(dir->fd, (void *) dir->buf, sizeof(dir->buf));
47 if (len <= 0)
48 return NULL;
49 dir->buf_pos = 0;
50 dir->buf_end = len;
52 de = (void *) (dir->buf + dir->buf_pos);
53 dir->buf_pos += de->d_reclen;
54 return de;