diff options
author | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-18 23:04:12 +0200 |
---|---|---|
committer | Lieuwe Rooijakkers <lieuwerooijakkers@gmail.com> | 2024-08-18 23:04:12 +0200 |
commit | bf8990a43e8374d98d37590f2a07c09c514a7127 (patch) | |
tree | d0440bf10cabedc6d32775f73dfefeacda4d4521 | |
parent | b25cea52a0502e2841b3f3ccff1b48028754cdad (diff) |
vind: glob searching
-rw-r--r-- | src/vind.c | 13 |
1 files changed, 12 insertions, 1 deletions
@@ -1,6 +1,7 @@ #include <assert.h> #include <ftw.h> #include <getopt.h> +#include <fnmatch.h> #include <limits.h> #include <pwd.h> #include <stdbool.h> @@ -18,6 +19,8 @@ static int max_depth = -1; static ssize_t min_size = 0; static ssize_t max_size = SSIZE_MAX; +static char *nameglob = NULL; + static void usage(FILE *f) { fprintf(f, "Gebruik: vind [-thV] [STARTPUNTEN]...\n" @@ -27,6 +30,7 @@ static void usage(FILE *f) { " -t Bestandtypes om weer te geven, kan zijn b(estand), m(apje) of een combinatie gescheiden door komma's\n" " -d Maximale diepte\n" " -o Omvang van n beten (+, - voor meer of minder)\n" + " -n Filter bestanden die voldoen aan het gegeven klodder-gebasseerde draadje\n" " -h Toon deze hulptekst\n" " -V Toon versienummer\n"); } @@ -56,7 +60,7 @@ static int parse_filetypes(char *s) { // Returns pointer to argument array containing the starting points static char** parse_options(int argc, char **argv) { int opt; - while ((opt = getopt(argc, argv, "t:d:o:hV")) != -1) { + while ((opt = getopt(argc, argv, "t:d:o:n:hV")) != -1) { switch (opt) { case 't': ftmap = parse_filetypes(optarg); @@ -76,6 +80,10 @@ static char** parse_options(int argc, char **argv) { } break; + case 'n': + nameglob = optarg; + break; + case 'h': usage(stdout); exit(0); @@ -115,6 +123,9 @@ static int f(const char *fpath, const struct stat *sb, int typeflag, struct FTW if (sb->st_size < min_size) return 0; if (sb->st_size > max_size) return 0; + // check filename + if (!(nameglob == NULL || fnmatch(nameglob, basename(fpath), 0) == 0)) return 0; + puts(fpath); return 0; } |