about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--rf.17
-rw-r--r--rf.c28
3 files changed, 30 insertions, 11 deletions
diff --git a/Makefile b/Makefile
index 208c034..9f8b7ba 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,7 @@
 .SUFFIXES: .c .o
 
 BIN = rf
+VERSION = 0.0.2
 OBJS = rf.o
 MANPAGE = rf.1
 CC = cc
@@ -17,7 +18,10 @@ CFLAGS += -ansi \
 	  -O2 \
 	  -fstack-protector-strong \
 	  -fpie \
-	  -D_FORTIFY_SOURCE=2 $(INC) $(LIBS)
+	  -D_FORTIFY_SOURCE=2 \
+	  -DVERSION='"$(VERSION)"' \
+	  -DNAME='"$(BIN)"' \
+	  $(INC) $(LIBS)
 PREFIX ?= /usr/local
 
 build: $(BIN)
diff --git a/rf.1 b/rf.1
index f1ed3e6..1d5001e 100644
--- a/rf.1
+++ b/rf.1
@@ -28,6 +28,11 @@ Show version.
 Only show the basename of results.
 
 .TP
+.B "\-b, \-\-dirname"
+.br
+Only show the dirname of results.
+
+.TP
 .B "\-v, \-\-invert"
 .br
 Invert matches.
@@ -76,4 +81,4 @@ only match the file "bar".
 .BR rg (1)
 
 .SH COPYRIGHT
-Copyright \(co 2019 Rory Bradford <roryrjb@gmail.com>.
+Copyright \(co 2020 Rory Bradford <roryrjb@gmail.com>.
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;