From 865018e7f837c275cf0c67364c5e3fb108eeae28 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 23 Oct 2022 14:59:06 +0200 Subject: [PATCH] libc: Implement dirfd, rewinddir, telldir and seekdir --- libs/libc/include/dirent.h | 12 ++++++++++++ libs/libc/src/dirent.cpp | 26 +++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/libs/libc/include/dirent.h b/libs/libc/include/dirent.h index c4192d5e..c8c2ed67 100644 --- a/libs/libc/include/dirent.h +++ b/libs/libc/include/dirent.h @@ -36,6 +36,18 @@ extern "C" * calls to readdir(). */ struct dirent* readdir(DIR* stream); + /* Returns the file descriptor associated with stream. */ + int dirfd(DIR* stream); + + /* Positions stream's offset at the start of the directory. */ + void rewinddir(DIR* stream); + + /* Returns the current offset position for stream. */ + long telldir(DIR* stream); + + /* Moves stream's read offset to offset, which should be the return value of a previous call to telldir(). */ + void seekdir(DIR* stream, long offset); + #ifdef __cplusplus } #endif diff --git a/libs/libc/src/dirent.cpp b/libs/libc/src/dirent.cpp index 5447647c..221f7cca 100644 --- a/libs/libc/src/dirent.cpp +++ b/libs/libc/src/dirent.cpp @@ -10,9 +10,9 @@ extern "C" { DIR* opendir(const char* path) { - int dirfd = open(path, O_RDONLY | O_DIRECTORY); - if (dirfd < 0) return NULL; - return fdopendir(dirfd); + int fd = open(path, O_RDONLY | O_DIRECTORY); + if (fd < 0) return NULL; + return fdopendir(fd); } DIR* fdopendir(int fd) @@ -50,4 +50,24 @@ extern "C" strlcpy(result.d_name, ent.name, NAME_MAX); return &result; } + + int dirfd(DIR* stream) + { + return stream->d_dirfd; + } + + void rewinddir(DIR* stream) + { + lseek(stream->d_dirfd, 0, SEEK_SET); + } + + long telldir(DIR* stream) + { + return lseek(stream->d_dirfd, 0, SEEK_CUR); + } + + void seekdir(DIR* stream, long offset) + { + lseek(stream->d_dirfd, offset, SEEK_SET); + } } \ No newline at end of file