From 78c1c323a865c68ae266cec63353264f4034a5ec Mon Sep 17 00:00:00 2001 From: R4ken Date: Wed, 13 Aug 2025 13:04:38 +0200 Subject: [PATCH 1/2] posix: implement missing directory functions Implement missing POSIX functions (fdopendir,seekdir, telldir) working on DIR structs JIRA: RTOS-1088 --- include/dirent.h | 5 ++++ unistd/dir.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/include/dirent.h b/include/dirent.h index a305e5b6..41e07a2b 100644 --- a/include/dirent.h +++ b/include/dirent.h @@ -53,6 +53,11 @@ extern struct dirent *readdir(DIR *s); extern DIR *opendir(const char *dirname); +extern DIR *fdopendir(int fd); + +extern void seekdir(DIR *dirp, long loc); + +extern long telldir(DIR *dirp); extern void rewinddir(DIR *dirp); diff --git a/unistd/dir.c b/unistd/dir.c index aef2fbf3..ba511b2d 100644 --- a/unistd/dir.c +++ b/unistd/dir.c @@ -26,6 +26,7 @@ #include #include #include +#include static struct { @@ -440,6 +441,69 @@ DIR *opendir(const char *dirname) } +DIR *fdopendir(int fd) +{ + DIR *s; + struct stat statbuf; + int fd_flags; + + fd_flags = fcntl(fd, F_GETFL); + if (fd_flags < 0) { + return NULL; + } + if ((fd_flags & O_RDONLY) == 0) { + errno = EBADF; + return NULL; /* EBADF */ + } + + if (fstat(fd, &statbuf) < 0) { + return NULL; + } + + msg_t msg = { + .type = mtGetAttr, + .oid = { .port = statbuf.st_dev, .id = statbuf.st_ino }, + .i.attr.type = atType, + }; + + if ((msgSend(statbuf.st_dev, &msg) < 0) || (msg.o.err < 0)) { + errno = EIO; + return NULL; /* EIO */ + } + + if (msg.o.attr.val != otDir) { + errno = ENOTDIR; + return NULL; /* ENOTDIR */ + } + + if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) { + return NULL; + } + + s = calloc(1, sizeof(DIR)); + if (s == NULL) { + errno = ENOMEM; + return NULL; + } + s->oid = msg.oid; + s->dirent = NULL; + + return s; +} + + +void seekdir(DIR *dirp, long loc) +{ + dirp->pos = loc; +} + + +long telldir(DIR *dirp) +{ + return dirp->pos; +} + + void rewinddir(DIR *dirp) { dirp->pos = 0; From ca26bd46dc8199ff3dbee6b966ccb33d2fd910aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Lach?= Date: Wed, 26 Nov 2025 15:04:49 +0100 Subject: [PATCH 2/2] posix: introduce NULL checks in *dir routines --- unistd/dir.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/unistd/dir.c b/unistd/dir.c index ba511b2d..863bf3ed 100644 --- a/unistd/dir.c +++ b/unistd/dir.c @@ -350,6 +350,11 @@ char *realpath(const char *path, char *resolved_path) struct dirent *readdir(DIR *s) { + if (s == NULL) { + errno = EBADF; + return NULL; + } + if (s->dirent == NULL) { if ((s->dirent = calloc(1, sizeof(struct dirent) + NAME_MAX + 1)) == NULL) return NULL; @@ -494,18 +499,26 @@ DIR *fdopendir(int fd) void seekdir(DIR *dirp, long loc) { + assert(dirp != NULL); + dirp->pos = loc; } long telldir(DIR *dirp) { + if (dirp != NULL) { + return -EBADF; + } + return dirp->pos; } void rewinddir(DIR *dirp) { + assert(dirp != NULL); + dirp->pos = 0; } @@ -514,6 +527,10 @@ int closedir(DIR *dirp) { int ret = 0; + if (dirp != NULL) { + return -EBADF; + } + msg_t msg = { .type = mtClose, .oid = dirp->oid