summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-10-03 14:20:51 +0200
committerAnton Kling <anton@kling.gg>2024-10-03 14:20:51 +0200
commita8226de3e53937b579705586a16ccc884b8efb5d (patch)
tree63b2967b19772dd4ca3b44c486d5923948438a5d
parent9902f9a2b6c5030000e220841b5868158a0bc05d (diff)
libc: Add fdopendir()
-rw-r--r--include/dirent.h1
-rw-r--r--userland/libc/dirent/opendir.c16
2 files changed, 16 insertions, 1 deletions
diff --git a/include/dirent.h b/include/dirent.h
index b986ddb..dcd7f59 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -22,6 +22,7 @@ typedef struct {
#ifndef KERNEL
DIR *opendir(const char *dirname);
+DIR *fdopendir(int fd);
struct dirent *readdir(DIR *dir);
int closedir(DIR *dirp);
int alphasort(const struct dirent **d1, const struct dirent **d2);
diff --git a/userland/libc/dirent/opendir.c b/userland/libc/dirent/opendir.c
index 7bfa562..efe8b6d 100644
--- a/userland/libc/dirent/opendir.c
+++ b/userland/libc/dirent/opendir.c
@@ -1,10 +1,24 @@
#include <dirent.h>
+DIR *fdopendir(int fd) {
+ DIR *rc = malloc(sizeof(DIR));
+ if (!rc) {
+ return NULL;
+ }
+ rc->fd = fd;
+ rc->dir_num = 0;
+ return rc;
+}
+
DIR *opendir(const char *dirname) {
int fd = open(dirname, O_RDONLY, 0);
- if (-1 == fd)
+ if (-1 == fd) {
return NULL;
+ }
DIR *rc = malloc(sizeof(DIR));
+ if (!rc) {
+ return NULL;
+ }
rc->fd = fd;
rc->dir_num = 0;
return rc;