Skip to content

Instantly share code, notes, and snippets.

@siburu
Last active November 26, 2020 18:36
Show Gist options
  • Save siburu/786b4d7326006e1f63c9017367cb59d3 to your computer and use it in GitHub Desktop.
Save siburu/786b4d7326006e1f63c9017367cb59d3 to your computer and use it in GitHub Desktop.
#define _GNU_SOURCE
#include <stdio.h>
#include <ftw.h>
int walk(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
static const char *flag2msg[] = {
[FTW_F] = "Regular file.",
[FTW_D] = "Directory.",
[FTW_DNR] = "Unreadable directory.",
[FTW_NS] = "Unstatable file.",
[FTW_SL] = "Symbolic link.",
[FTW_DP] = "Directory, all subdirs have been visited.",
[FTW_SLN] = "Symbolic link naming non-existing file.",
};
for (int i = 0; i < ftwbuf->level; i++) {
fputs(" ", stdout);
}
if (typeflag == FTW_F) {
printf("%s (%ldB): %s\n", fpath + ftwbuf->base, sb->st_size, flag2msg[typeflag]);
} else {
printf("%s: %s\n", fpath + ftwbuf->base, flag2msg[typeflag]);
}
return 0;
}
int main(int argc, const char *argv[])
{
const char *root = ".";
if (argc >= 2) {
root = argv[1];
}
int ret = nftw(root, walk, 10, 0);
printf("result: %d\n", ret);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment