From 2447dcd1eab85d21de4f17c21ff74c291d721af1 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 25 Apr 2021 09:41:45 +0200 Subject: [PATCH] LibC: Implement the rewinddir() function --- Userland/Libraries/LibC/dirent.cpp | 12 ++++++++++-- Userland/Libraries/LibC/dirent.h | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibC/dirent.cpp b/Userland/Libraries/LibC/dirent.cpp index 460d88c2ac..241c7a1113 100644 --- a/Userland/Libraries/LibC/dirent.cpp +++ b/Userland/Libraries/LibC/dirent.cpp @@ -35,8 +35,7 @@ int closedir(DIR* dirp) { if (!dirp || dirp->fd == -1) return -EBADF; - if (dirp->buffer) - free(dirp->buffer); + free(dirp->buffer); int rc = close(dirp->fd); if (rc == 0) dirp->fd = -1; @@ -44,6 +43,15 @@ int closedir(DIR* dirp) return rc; } +void rewinddir(DIR* dirp) +{ + free(dirp->buffer); + dirp->buffer = nullptr; + dirp->buffer_size = 0; + dirp->nextptr = nullptr; + lseek(dirp->fd, 0, SEEK_SET); +} + struct [[gnu::packed]] sys_dirent { ino_t ino; u8 file_type; diff --git a/Userland/Libraries/LibC/dirent.h b/Userland/Libraries/LibC/dirent.h index 821fbdcd74..d468e33e6c 100644 --- a/Userland/Libraries/LibC/dirent.h +++ b/Userland/Libraries/LibC/dirent.h @@ -50,6 +50,7 @@ typedef struct __DIR DIR; DIR* opendir(const char* name); int closedir(DIR*); +void rewinddir(DIR*); struct dirent* readdir(DIR*); int readdir_r(DIR*, struct dirent*, struct dirent**); int dirfd(DIR*);