From 3f7290f5fd7b5c441b35896dfa3333e2f4989ea4 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 18 Jun 2015 23:25:24 -0400 Subject: Add -hidden/-nohidden flags. --- bfs.c | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/bfs.c b/bfs.c index e4296e6..7a7b789 100644 --- a/bfs.c +++ b/bfs.c @@ -10,22 +10,61 @@ *********************************************************************/ #include "bftw.h" +#include #include #include +#include + +typedef struct { + const char *path; + bool hidden; +} options; static int callback(const char *fpath, int typeflag, void *ptr) { + const options *opts = ptr; + + const char *filename = strrchr(fpath, '/'); + if (filename) { + ++filename; + } else { + filename = fpath + strlen(fpath); + } + + if (!opts->hidden && filename[0] == '.') { + return BFTW_SKIP_SUBTREE; + } + printf("%s\n", fpath); return BFTW_CONTINUE; } int main(int argc, char* argv[]) { - const char* path = "."; - if (argc > 1) { - path = argv[1]; + options opts; + opts.path = NULL; + opts.hidden = true; + + for (int i = 1; i < argc; ++i) { + const char *arg = argv[i]; + + if (strcmp(arg, "-hidden") == 0) { + opts.hidden = true; + } else if (strcmp(arg, "-nohidden") == 0) { + opts.hidden = false; + } else { + if (opts.path) { + fprintf(stderr, "Duplicate path `%s` on command line.", arg); + return EXIT_FAILURE; + } + opts.path = arg; + } + } + + if (!opts.path) { + opts.path = "."; } // TODO: getrlimit(RLIMIT_NOFILE) - if (bftw(path, callback, 1024, NULL) != 0) { + if (bftw(opts.path, callback, 1024, &opts) != 0) { perror("bftw()"); return EXIT_FAILURE; } -- cgit v1.2.3