From 642b5fb0007b64c77d186fcb018d571152ee1d47 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Mon, 14 Aug 2023 18:51:07 +0200 Subject: reorganization: first steps --- src/cmd/find/find.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/cmd/find/find.c (limited to 'src/cmd/find') diff --git a/src/cmd/find/find.c b/src/cmd/find/find.c new file mode 100644 index 0000000..d473b82 --- /dev/null +++ b/src/cmd/find/find.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include + +void recurse(char *path) { + DIR *d = opendir(path); + if (!d) { + warn("couldn't open %s", path); + return; + } + for (;;) { + struct dirent *dent; + errno = 0; + dent = readdir(d); + if (!dent) { + if (errno) { + warn("when reading %s", path); + } + 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'; + } + } + 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); + free(buf); +} + +int main(int argc, char **argv) { + if (argc < 2) { + find("/"); + } else { + for (int i = 1; i < argc; i++) + find(argv[i]); + } + return 0; +} -- cgit v1.2.3