summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2015-06-18 23:25:24 -0400
committerTavian Barnes <tavianator@tavianator.com>2015-06-18 23:26:05 -0400
commit3f7290f5fd7b5c441b35896dfa3333e2f4989ea4 (patch)
tree2f11a520b0d27a7fc955efb5aaeda6cf6c1fd98d
parent864bdf1b3414ff5402c5b1dbf94319f465626885 (diff)
downloadbfs-3f7290f5fd7b5c441b35896dfa3333e2f4989ea4.tar.xz
Add -hidden/-nohidden flags.
-rw-r--r--bfs.c47
1 files 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 <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+
+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;
}