summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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;
}