diff options
Diffstat (limited to 'userland/libc/dirent')
-rw-r--r-- | userland/libc/dirent/alphasort.c | 7 | ||||
-rw-r--r-- | userland/libc/dirent/closedir.c | 7 | ||||
-rw-r--r-- | userland/libc/dirent/opendir.c | 11 | ||||
-rw-r--r-- | userland/libc/dirent/readdir.c | 14 | ||||
-rw-r--r-- | userland/libc/dirent/scandir.c | 43 |
5 files changed, 82 insertions, 0 deletions
diff --git a/userland/libc/dirent/alphasort.c b/userland/libc/dirent/alphasort.c new file mode 100644 index 0000000..43a4b5f --- /dev/null +++ b/userland/libc/dirent/alphasort.c @@ -0,0 +1,7 @@ +#include <dirent.h> + +int alphasort(const struct dirent **d1, const struct dirent **d2) { + // TODO: Actually sort it + *d2 = *d1; + return 0; +} diff --git a/userland/libc/dirent/closedir.c b/userland/libc/dirent/closedir.c new file mode 100644 index 0000000..e216d7c --- /dev/null +++ b/userland/libc/dirent/closedir.c @@ -0,0 +1,7 @@ +#include <dirent.h> + +int closedir(DIR *dir) { + close(dir->fd); + free(dir); + return 0; +} diff --git a/userland/libc/dirent/opendir.c b/userland/libc/dirent/opendir.c new file mode 100644 index 0000000..7bfa562 --- /dev/null +++ b/userland/libc/dirent/opendir.c @@ -0,0 +1,11 @@ +#include <dirent.h> + +DIR *opendir(const char *dirname) { + int fd = open(dirname, O_RDONLY, 0); + if (-1 == fd) + return NULL; + DIR *rc = malloc(sizeof(DIR)); + rc->fd = fd; + rc->dir_num = 0; + return rc; +} diff --git a/userland/libc/dirent/readdir.c b/userland/libc/dirent/readdir.c new file mode 100644 index 0000000..88aff48 --- /dev/null +++ b/userland/libc/dirent/readdir.c @@ -0,0 +1,14 @@ +#include <dirent.h> + +struct dirent *readdir(DIR *dir) { + size_t offset = dir->dir_num * sizeof(struct dirent); + int rc; + if (-1 == (rc = pread(dir->fd, &dir->internal_direntry, sizeof(struct dirent), + offset))) + return NULL; + if (rc < sizeof(struct dirent)) + return NULL; + + dir->dir_num++; + return &(dir->internal_direntry); +} diff --git a/userland/libc/dirent/scandir.c b/userland/libc/dirent/scandir.c new file mode 100644 index 0000000..1520140 --- /dev/null +++ b/userland/libc/dirent/scandir.c @@ -0,0 +1,43 @@ +#include <dirent.h> +#include <stdlib.h> +#include <string.h> + +int nop_sel(const struct dirent *unused) { + (void)unused; + return 1; +} + +int nop_compar(const struct dirent **d1, const struct dirent **d2) { + *d2 = *d1; + return 0; +} + +int scandir(const char *dir, struct dirent ***namelist, + int (*sel)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)) { + if (!sel) + sel = nop_sel; + + if (!compar) + compar = nop_compar; + + DIR *d = opendir(dir); + struct dirent **list = NULL; + struct dirent *e; + int rc = 0; + for (; (e = readdir(d));) { + if (!sel(e)) + continue; + struct dirent *p = malloc(sizeof(struct dirent)); + memcpy(p, e, sizeof(struct dirent)); + list = realloc(list, (rc + 1) * sizeof(struct dirent *)); + list[rc] = p; + rc++; + } + // struct dirent **new_list; + // compar((const struct dirent **)list, (const struct dirent **)new_list); + // *namelist = new_list; + *namelist = list; + // closedir(d); + return rc; +} |