diff options
author | dzwdz | 2023-09-22 23:42:30 +0200 |
---|---|---|
committer | dzwdz | 2023-09-22 23:42:30 +0200 |
commit | 6a4d4a41a664e6a4c406a449ea847abd4a224bcf (patch) | |
tree | 0a637697c8697929beb8f4b7ff69d8b74f9e28bb /src/cmd/find.c | |
parent | a3d6aa9f8d427b86a33dc05bed98a2e88229a285 (diff) |
build: support single file commands
Diffstat (limited to 'src/cmd/find.c')
-rw-r--r-- | src/cmd/find.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/cmd/find.c b/src/cmd/find.c new file mode 100644 index 0000000..d473b82 --- /dev/null +++ b/src/cmd/find.c @@ -0,0 +1,55 @@ +#include <camellia/path.h> +#include <dirent.h> +#include <err.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +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; +} |