X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/707c6653985f55463e8a59a58b248ba8af3b44b2..0ca0e2a565657cd3e37315fe665b45b13daaebf4:/lunaix-os/usr/libc/readdir.c diff --git a/lunaix-os/usr/libc/readdir.c b/lunaix-os/usr/libc/readdir.c new file mode 100644 index 0000000..9d2b23e --- /dev/null +++ b/lunaix-os/usr/libc/readdir.c @@ -0,0 +1,41 @@ +#include +#include +#include +#include + +DIR* +opendir(const char* dir) +{ + static DIR _dir; + int fd = open(dir, O_RDONLY); + if (fd < 0) { + return NULL; + } + + _dir = (DIR){ .dirfd = fd, .prev_res = 0 }; + return &_dir; +} + +struct dirent* +readdir(DIR* dir) +{ + static struct dirent _dirent; + if (!dir) { + return NULL; + } + + struct lx_dirent _lxd; + int more = sys_readdir(dir->dirfd, &_lxd); + + _dirent.d_type = _lxd.d_type; + strncpy(_dirent.d_name, _lxd.d_name, 256); + + if (more || dir->prev_res) { + dir->prev_res = more; + return &_dirent; + } + + if (!dir->prev_res) { + return NULL; + } +} \ No newline at end of file