---
 tests/grep.test   |  1 +
 toys/posix/grep.c | 13 ++++++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)
From 74d9194f92b7cec3bfbc523da8b289dc51aeda31 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <[email protected]>
Date: Tue, 21 Sep 2021 15:04:30 -0700
Subject: [PATCH] grep: Implement -L.

---
 tests/grep.test   |  1 +
 toys/posix/grep.c | 13 ++++++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/tests/grep.test b/tests/grep.test
index 45db0842..434c4a95 100755
--- a/tests/grep.test
+++ b/tests/grep.test
@@ -13,6 +13,7 @@ echo -e "this is test" > foo
 echo -e "this is test2" > foo2
 echo -e "this is foo3" > foo3
 testing "-l" "grep -l test foo foo2 foo3" "foo\nfoo2\n" "" ""
+testing "-L" "grep -L test foo foo2 foo3" "foo3\n" "" ""
 rm foo foo2 foo3
 
 testing "-q" "grep -q test input && echo yes" "yes\n" "this is a test\n" ""
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index 8d8d63eb..c3179fde 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -10,7 +10,7 @@
 * echo hello | grep -f </dev/null
 *
 
-USE_GREP(NEWTOY(grep, "(line-buffered)(color):;(exclude-dir)*S(exclude)*M(include)*ZzEFHIab(byte-offset)h(no-filename)ino(only-matching)rRsvwcl(files-with-matches)q(quiet)(silent)e*f*C#B#A#m#x[!wx][!EFw]", TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)|TOYFLAG_LINEBUF))
+USE_GREP(NEWTOY(grep, "(line-buffered)(color):;(exclude-dir)*S(exclude)*M(include)*ZzEFHIab(byte-offset)h(no-filename)ino(only-matching)rRsvwcL(files-without-match)l(files-with-matches)q(quiet)(silent)e*f*C#B#A#m#x[!wx][!EFw]", TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)|TOYFLAG_LINEBUF))
 USE_EGREP(OLDTOY(egrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)|TOYFLAG_LINEBUF))
 USE_FGREP(OLDTOY(fgrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)|TOYFLAG_LINEBUF))
 
@@ -44,6 +44,7 @@ config GREP
     -x  whole line               -z  input NUL terminated
 
     display modes: (default: matched line)
+    -L  show only non-matching filenames
     -c  count of matching lines  -l  show only matching filenames
     -o  only matching part       -q  quiet (errors only)
     -s  silent (no error msg)    -Z  output NUL terminated
@@ -267,8 +268,9 @@ static void do_grep(int fd, char *name)
         toys.exitval = 0;
         xexit();
       }
-      if (FLAG(l)) {
-        xprintf("%s%c", name, TT.outdelim);
+      if (FLAG(L) || FLAG(l)) {
+        if (FLAG(l))
+          xprintf("%s%c", name, TT.outdelim);
         free(line);
         fclose(file);
         return;
@@ -352,6 +354,11 @@ static void do_grep(int fd, char *name)
     if (FLAG(m) && mcount >= TT.m) break;
   }
 
+  if (FLAG(L)) {
+    xprintf("%s%c", name, TT.outdelim);
+    return;
+  }
+
   if (FLAG(c)) outline(0, ':', name, mcount, 0, 1);
 
   // loopfiles will also close the fd, but this frees an (opaque) struct.
-- 
2.33.0.464.g1972c5931b-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to