From 1e726e662d1e90157029d77534ee6ebea3f90212 Mon Sep 17 00:00:00 2001 From: Rory Bradford Date: Sat, 22 Feb 2020 16:15:05 +0000 Subject: Add dirname option Signed-off-by: Rory Bradford --- rf.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'rf.c') diff --git a/rf.c b/rf.c index 9663bbc..a115b7a 100644 --- a/rf.c +++ b/rf.c @@ -14,11 +14,9 @@ #include "config.h" -#define NAME "rf" -#define VERSION "0.0.1" - struct switches { int basename; + int dirname; int invert; int limit; int count; @@ -37,6 +35,7 @@ static int usage(char *error) { fprintf(stderr, "Usage: %s [OPTIONS]\n", NAME); fprintf(stderr, "\nOptions:\n"); fprintf(stderr, " --basename, -b only show basename in results\n"); + fprintf(stderr, " --dirname, -d only show dirname in results\n"); fprintf(stderr, " --invert, -v invert matching\n"); fprintf(stderr, " --limit=n limit to [n] results, all if 0 [default=0]\n"); @@ -115,8 +114,12 @@ static void print_result(char *path, struct switches *switches, char full_path[MAXPATHLEN]; full_path[0] = '\0'; strcat(full_path, path); - strcat(full_path, "/"); - strcat(full_path, entry->d_name); + + if (!switches->dirname) { + strcat(full_path, "/"); + strcat(full_path, entry->d_name); + } + printf("%s\n", full_path); } } @@ -245,12 +248,16 @@ static int recurse_find(char **patterns, int *pattern_count, char *dirname, } int main(int argc, char **argv) { - static struct option options[] = { - {"basename", no_argument, 0, 0}, {"invert", no_argument, 0, 0}, - {"limit", required_argument, 0, 0}, {"version", no_argument, 0, 0}, - {"help", no_argument, 0, 0}, {0, 0, 0, 0}}; + static struct option options[] = {{"basename", no_argument, 0, 0}, + {"dirname", no_argument, 0, 0}, + {"invert", no_argument, 0, 0}, + {"limit", required_argument, 0, 0}, + {"version", no_argument, 0, 0}, + {"help", no_argument, 0, 0}, + {0, 0, 0, 0}}; int basename = 0; + int dirname = 0; int invert = 0; int limit = 0; int index = 0; @@ -268,6 +275,8 @@ int main(int argc, char **argv) { return usage(NULL); } else if (strcmp("basename", options[index].name) == 0) { basename = 1; + } else if (strcmp("dirname", options[index].name) == 0) { + dirname = 1; } else if (strcmp("invert", options[index].name) == 0) { invert = 1; } else if (strcmp("limit", options[index].name) == 0) { @@ -315,6 +324,7 @@ int main(int argc, char **argv) { } switches.basename = basename; + switches.dirname = dirname; switches.invert = invert; switches.limit = limit; switches.count = count; -- cgit 1.4.1-2-gfad0