toaruos/libc/dirent/dir.c
2018-07-18 10:45:42 +09:00

47 lines
800 B
C

#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <syscall.h>
#include <errno.h>
#include <bits/dirent.h>
DIR * opendir (const char * dirname) {
int fd = open(dirname, O_RDONLY);
if (fd < 0) {
errno = -fd;
return NULL;
}
DIR * dir = (DIR *)malloc(sizeof(DIR));
dir->fd = fd;
dir->cur_entry = -1;
return dir;
}
int closedir (DIR * dir) {
if (dir && (dir->fd != -1)) {
return close(dir->fd);
} else {
return -EBADF;
}
}
struct dirent * readdir (DIR * dirp) {
static struct dirent ent;
int ret = syscall_readdir(dirp->fd, ++dirp->cur_entry, &ent);
if (ret < 0) {
errno = -ret;
memset(&ent, 0, sizeof(struct dirent));
return NULL;
}
if (ret == 0) {
/* end of directory */
memset(&ent, 0, sizeof(struct dirent));
return NULL;
}
return &ent;
}