From 74ca76a58fca114140d0d0cc13a1216d6dffbee0 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 8 Jul 2023 02:30:31 +0200 Subject: cftw: Allow directories to call the user callback This provides a few benefits: - This will allow searching for directories by name. - Future commits will allow to remove files and directories, so this change was also required for cftw. --- cftw.c | 7 ++++++- main.c | 3 +++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/cftw.c b/cftw.c index 2990888..8fb1d67 100644 --- a/cftw.c +++ b/cftw.c @@ -57,7 +57,12 @@ int cftw(const char *const dirpath, int (*const fn)(const char *, fprintf(stderr, "%s: stat(2) %s: %s\n", __func__, path, strerror(errno)); else if (S_ISDIR(sb.st_mode)) - ret = cftw(d.str, fn, user); + { + if ((ret = cftw(d.str, fn, user))) + ; + else if ((ret = fn(d.str, &sb, user))) + ; + } else if (S_ISREG(sb.st_mode)) ret = fn(d.str, &sb, user); else diff --git a/main.c b/main.c index 048b1ba..82af0e6 100644 --- a/main.c +++ b/main.c @@ -768,6 +768,9 @@ end: static int add_length(const char *const fpath, const struct stat *const sb, void *const user) { + if (!S_ISREG(sb->st_mode)) + return 0; + unsigned long long *const l = user; *l += sb->st_size; -- cgit v1.2.3