From 75cc884573b5a5b501fb3a40b6131a93e8aa26e2 Mon Sep 17 00:00:00 2001 From: turbocat Date: Fri, 12 Jan 2024 23:03:49 +0000 Subject: [PATCH] NewLib: Added missing dirent impl git-svn-id: svn://kolibrios.org@9955 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../sdk/sources/newlib/libc/posix/closedir.c | 25 ++++++++++ .../sdk/sources/newlib/libc/posix/opendir.c | 42 +++++++++++++++++ .../sdk/sources/newlib/libc/posix/readdir.c | 47 +++++++++++++++++++ .../sdk/sources/newlib/libc/posix/rewinddir.c | 13 +++++ .../sdk/sources/newlib/libc/posix/seekdir.c | 14 ++++++ .../sdk/sources/newlib/libc/posix/telldir.c | 13 +++++ 6 files changed, 154 insertions(+) create mode 100644 contrib/sdk/sources/newlib/libc/posix/closedir.c create mode 100644 contrib/sdk/sources/newlib/libc/posix/opendir.c create mode 100644 contrib/sdk/sources/newlib/libc/posix/readdir.c create mode 100644 contrib/sdk/sources/newlib/libc/posix/rewinddir.c create mode 100644 contrib/sdk/sources/newlib/libc/posix/seekdir.c create mode 100644 contrib/sdk/sources/newlib/libc/posix/telldir.c diff --git a/contrib/sdk/sources/newlib/libc/posix/closedir.c b/contrib/sdk/sources/newlib/libc/posix/closedir.c new file mode 100644 index 0000000000..73c69d63f5 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/posix/closedir.c @@ -0,0 +1,25 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#include +#include +#include + +int +_DEFUN(closedir, (dirp), + register DIR *dirp) +{ + if (!dirp) + { + errno = EBADF; + return -1; + } + + if (dirp->path) + free(dirp->path); + + free(dirp); + return 0; +} diff --git a/contrib/sdk/sources/newlib/libc/posix/opendir.c b/contrib/sdk/sources/newlib/libc/posix/opendir.c new file mode 100644 index 0000000000..1b044193d4 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/posix/opendir.c @@ -0,0 +1,42 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#include +#include +#include +#include +#include + +/* TODO: Add thread safety */ + +DIR * +_DEFUN(opendir, (name), + const char *name) +{ + ksys_file_info_t info; + if (_ksys_file_info(name, &info)) + { + errno = ENOENT; + return NULL; + } + + if (!(info.attr & (KSYS_FILE_ATTR_DIR | KSYS_FILE_ATTR_VOL_LABEL))) + { + errno = ENOTDIR; + return NULL; + } + + DIR* dir = malloc(sizeof(DIR)); + if (!dir) + return NULL; + + dir->path = strdup(name); + if (!dir->path) + return NULL; + + dir->pos = 0; + + return dir; +} diff --git a/contrib/sdk/sources/newlib/libc/posix/readdir.c b/contrib/sdk/sources/newlib/libc/posix/readdir.c new file mode 100644 index 0000000000..9482390a62 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/posix/readdir.c @@ -0,0 +1,47 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#include +#include +#include +#include +#include + +struct dirent * +_DEFUN(readdir, (dirp), + register DIR *dirp) +{ + if (!dirp || !dirp->path) + { + errno = EBADF; + return NULL; + } + + ksys_dir_entry_t *entry = calloc(sizeof(ksys_dir_entry_t) + + KSYS_FNAME_UTF8_SIZE, 1); + + int status = _ksys_file_read_dir(dirp->path, dirp->pos, + KSYS_FILE_UTF8, 1, entry).status; + if (status == KSYS_FS_ERR_EOF) + return NULL; + + if (status) + { + errno = ENOENT; + return NULL; + } + + dirp->last_entry.d_ino = dirp->pos; + strncpy(dirp->last_entry.d_name, entry->info.name, KSYS_FNAME_UTF8_SIZE-1); + + if (entry->info.attr & (KSYS_FILE_ATTR_DIR | KSYS_FILE_ATTR_VOL_LABEL)) + dirp->last_entry.d_type = DT_DIR; + else + dirp->last_entry.d_type = DT_REG; + + dirp->pos++; + + return &dirp->last_entry; +} diff --git a/contrib/sdk/sources/newlib/libc/posix/rewinddir.c b/contrib/sdk/sources/newlib/libc/posix/rewinddir.c new file mode 100644 index 0000000000..da049ce068 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/posix/rewinddir.c @@ -0,0 +1,13 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#include + +void +_DEFUN(rewinddir, (dirp), + DIR *dirp) +{ + dirp->pos = 0; +} diff --git a/contrib/sdk/sources/newlib/libc/posix/seekdir.c b/contrib/sdk/sources/newlib/libc/posix/seekdir.c new file mode 100644 index 0000000000..425fdd6cc1 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/posix/seekdir.c @@ -0,0 +1,14 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#include + +void +_DEFUN(seekdir, (dirp, loc), + DIR *dirp _AND + long loc) +{ + dirp->pos = loc; +} diff --git a/contrib/sdk/sources/newlib/libc/posix/telldir.c b/contrib/sdk/sources/newlib/libc/posix/telldir.c new file mode 100644 index 0000000000..19c5a20272 --- /dev/null +++ b/contrib/sdk/sources/newlib/libc/posix/telldir.c @@ -0,0 +1,13 @@ +/* + * Copyright (C) KolibriOS team 2024. All rights reserved. + * Distributed under terms of the GNU General Public License +*/ + +#include + +long +_DEFUN(telldir, (dirp), + DIR *dirp) +{ + return dirp->pos; +}