diff options
author | dzwdz | 2023-06-17 23:41:41 +0200 |
---|---|---|
committer | dzwdz | 2023-06-17 23:41:41 +0200 |
commit | 895138e65b90c3f20712b9c31690d84a6aa956ac (patch) | |
tree | c3a840f5e41ffdc0fc7725eb839f502f55b01b64 /src/user/app/find | |
parent | b528a54a708c4cd2149c8e6884af2063c2b272cd (diff) |
libc: implement dirent.h
Diffstat (limited to 'src/user/app/find')
-rw-r--r-- | src/user/app/find/find.c | 60 |
1 files changed, 24 insertions, 36 deletions
diff --git a/src/user/app/find/find.c b/src/user/app/find/find.c index 529b54e..d473b82 100644 --- a/src/user/app/find/find.c +++ b/src/user/app/find/find.c @@ -1,55 +1,43 @@ #include <camellia/path.h> +#include <dirent.h> +#include <err.h> +#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#define eprintf(fmt, ...) fprintf(stderr, "find: "fmt"\n" __VA_OPT__(,) __VA_ARGS__) - void recurse(char *path) { - FILE *f = fopen(path, "r"); - const int buf_len = 4096; - char *buf; - - if (!f) { - eprintf("couldn't open %s", path); + DIR *d = opendir(path); + if (!d) { + warn("couldn't open %s", path); return; } - - buf = malloc(buf_len); for (;;) { - int len = fread(buf, 1, buf_len, f); - int pos = 0; - if (len <= 0) break; - while (pos < len) { - if (buf[pos] == '\0') { - eprintf("%s has an empty entry, bailing", path); - break; - } - const char *end = memchr(buf + pos, 0, len - pos); - if (!end) { - eprintf("buf overflowed, unimplemented"); // TODO - break; + struct dirent *dent; + errno = 0; + dent = readdir(d); + if (!dent) { + if (errno) { + warn("when reading %s", path); } - printf("%s%s\n", path, buf + pos); - - size_t entrylen = end - (buf + pos) + 1; - if (end[-1] == '/') { - // append onto end of the current path - // TODO no overflow check - char *pend = path + strlen(path); - memcpy(pend, buf + pos, entrylen); - recurse(path); - *pend = '\0'; - } - pos += entrylen; + break; + } + printf("%s%s\n", path, dent->d_name); + /* if the string ends with '/' */ + if (strchr(dent->d_name, '\0')[-1] == '/') { + // TODO no overflow check + char *pend = strchr(path, '\0'); + strcpy(pend, dent->d_name); + recurse(path); + *pend = '\0'; } } - fclose(f); - free(buf); + closedir(d); } void find(const char *path) { // TODO bound checking + // TODO or just implement asprintf() char *buf = malloc(PATH_MAX); memcpy(buf, path, strlen(path)+1); recurse(buf); |