aboutsummaryrefslogtreecommitdiff
path: root/cftw.c
blob: bb05843c13caf61737293a32559f470a3d88a021 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "cftw.h"
#include <dynstr.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>

int cftw(const char *const dirpath, int (*const fn)(const char *,
    const struct stat *, void *), void *const user)
{
    int ret = -1;
    DIR *const d = opendir(dirpath);
    struct dirent *de;

    if (!d)
    {
        fprintf(stderr, "%s: opendir(2): %s\n", __func__, strerror(errno));
        goto end;
    }

    while ((de = readdir(d)))
    {
        const char *const path = de->d_name;

        if (!strcmp(path, ".") || !strcmp(path, ".."))
            continue;

        struct stat sb;
        struct dynstr d;

        dynstr_init(&d);

        if (dynstr_append(&d, "%s/%s", dirpath, path))
        {
            fprintf(stderr, "%s: dynstr_append failed\n", __func__);
            return -1;
        }

        const int r = stat(d.str, &sb);

        if (r)
            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);
        else if (S_ISREG(sb.st_mode))
            ret = fn(d.str, &sb, user);
        else
            fprintf(stderr, "%s: unexpected st_mode %ju\n",
                __func__, (uintmax_t)sb.st_mode);

        dynstr_free(&d);

        if (ret)
            goto end;
    }

    ret = 0;

end:

    if (d && closedir(d))
    {
        fprintf(stderr, "%s: closedir(2): %s\n", __func__, strerror(errno));
        ret = -1;
    }

    return ret;
}