about summary refs log tree commit diff stats
path: root/rf.c
diff options
context:
space:
mode:
authorRory Bradford <roryrjb@gmail.com>2020-02-22 16:15:05 +0000
committerRory Bradford <roryrjb@gmail.com>2020-02-22 16:15:05 +0000
commit1e726e662d1e90157029d77534ee6ebea3f90212 (patch)
tree13c94eef5efd551886f892ffc7ce0a96a17f6fe2 /rf.c
parentc4fc1439a88916edba6fd1f5ead05105d9ad1299 (diff)
downloadrf-1e726e662d1e90157029d77534ee6ebea3f90212.tar.gz
Add dirname option
Signed-off-by: Rory Bradford <roryrjb@gmail.com>
Diffstat (limited to 'rf.c')
-rw-r--r--rf.c28
1 files changed, 19 insertions, 9 deletions
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;