From 4e09bca9e34c226b6d7e34b4fa11248405fd988e Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sun, 22 Oct 2023 19:50:38 +0200 Subject: Move everything into a new repo. --- userland/libc/dirent/scandir.c | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 userland/libc/dirent/scandir.c (limited to 'userland/libc/dirent/scandir.c') 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 +#include +#include + +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; +} -- cgit v1.2.3