summaryrefslogtreecommitdiff
path: root/src/cmd/find.c
diff options
context:
space:
mode:
authordzwdz2024-05-05 14:58:44 +0200
committerdzwdz2024-05-05 14:58:58 +0200
commitcb518ecd55cd7a45d0368fb9d68a1981c6c91adf (patch)
treec25ecffeeaf4f1f7d33b4ced91515d43a79c2dd5 /src/cmd/find.c
parent80839982e9983c6ccadbf57a44e60eeb0e535421 (diff)
libc: implement asprintf
Diffstat (limited to 'src/cmd/find.c')
-rw-r--r--src/cmd/find.c26
1 files changed, 8 insertions, 18 deletions
diff --git a/src/cmd/find.c b/src/cmd/find.c
index d473b82..8f0352c 100644
--- a/src/cmd/find.c
+++ b/src/cmd/find.c
@@ -1,4 +1,3 @@
-#include <camellia/path.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
@@ -6,7 +5,7 @@
#include <stdlib.h>
#include <string.h>
-void recurse(char *path) {
+void recurse(const char *path) {
DIR *d = opendir(path);
if (!d) {
warn("couldn't open %s", path);
@@ -25,31 +24,22 @@ void recurse(char *path) {
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';
+ char *next;
+ if (asprintf(&next, "%s%s", path, dent->d_name) >= 0) {
+ recurse(next);
+ free(next);
+ }
}
}
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("/");
+ recurse("/");
} else {
for (int i = 1; i < argc; i++)
- find(argv[i]);
+ recurse(argv[i]);
}
return 0;
}