From 78eba1271f194b4ce2bd57d886a483702c38ee51 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Wed, 11 Aug 2021 19:10:05 +0200 Subject: [PATCH] LibC: Add function fdopendir This adds the function fdopendir and refactors opendir so that opendir just opens a file descriptor and passes the file descriptor onto fdopendir. --- Userland/Libraries/LibC/dirent.cpp | 7 +++++++ Userland/Libraries/LibC/dirent.h | 1 + 2 files changed, 8 insertions(+) diff --git a/Userland/Libraries/LibC/dirent.cpp b/Userland/Libraries/LibC/dirent.cpp index 7475d22519..3447b9d370 100644 --- a/Userland/Libraries/LibC/dirent.cpp +++ b/Userland/Libraries/LibC/dirent.cpp @@ -24,6 +24,13 @@ extern "C" { DIR* opendir(const char* name) { int fd = open(name, O_RDONLY | O_DIRECTORY); + if (fd == -1) + return nullptr; + return fdopendir(fd); +} + +DIR* fdopendir(int fd) +{ if (fd == -1) return nullptr; DIR* dirp = (DIR*)malloc(sizeof(DIR)); diff --git a/Userland/Libraries/LibC/dirent.h b/Userland/Libraries/LibC/dirent.h index e78114bb6d..1e3f8534cc 100644 --- a/Userland/Libraries/LibC/dirent.h +++ b/Userland/Libraries/LibC/dirent.h @@ -27,6 +27,7 @@ struct __DIR { }; typedef struct __DIR DIR; +DIR* fdopendir(int fd); DIR* opendir(const char* name); int closedir(DIR*); void rewinddir(DIR*);