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.
This commit is contained in:
Xavier Del Campo Romero 2023-07-08 02:30:31 +02:00
parent 8bff49d9c8
commit 74ca76a58f
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
2 changed files with 9 additions and 1 deletions

7
cftw.c
View File

@ -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

3
main.c
View File

@ -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;