aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xtests/grep.test9
-rw-r--r--toys/posix/grep.c7
2 files changed, 14 insertions, 2 deletions
diff --git a/tests/grep.test b/tests/grep.test
index dee59924..68c8dd85 100755
--- a/tests/grep.test
+++ b/tests/grep.test
@@ -175,3 +175,12 @@ mkdir sub/no
echo "hello world" > sub/no/test
testing "--exclude-dir" 'grep --exclude-dir=no -r world sub' "sub/yes/test:hello world\n" "" ""
rm -rf sub
+
+# -r and -R differ in that -R will dereference symlinks to directories.
+mkdir dir
+echo "hello" > dir/f
+mkdir sub
+ln -s ../dir sub/link
+testing "" "grep -rh hello sub" "" "" ""
+testing "" "grep -Rh hello sub" "hello\n" "" ""
+rm -rf sub real
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index 30a98190..b92294ed 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)rsvwcl(files-with-matches)q(quiet)(silent)e*f*C#B#A#m#x[!wx][!EFw]", TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
+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)))
USE_EGREP(OLDTOY(egrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
USE_FGREP(OLDTOY(fgrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)))
@@ -29,6 +29,7 @@ config GREP
file search:
-r Recurse into subdirectories (defaults FILE to ".")
+ -R Recurse into subdirectories and symlinks to directories
-M Match filename pattern (--include)
-S Skip filename pattern (--exclude)
--exclude-dir=PATTERN Skip directory pattern
@@ -425,7 +426,7 @@ static int do_grep_r(struct dirtree *new)
if (S_ISDIR(new->st.st_mode)) {
for (al = TT.exclude_dir; al; al = al->next)
if (!fnmatch(al->arg, new->name, 0)) return 0;
- return DIRTREE_RECURSE;
+ return DIRTREE_RECURSE|(FLAG(R)?DIRTREE_SYMFOLLOW:0);
}
if (TT.S || TT.M) {
for (al = TT.S; al; al = al->next)
@@ -464,6 +465,8 @@ void grep_main(void)
TT.grey = "\033[0m";
} else TT.purple = TT.cyan = TT.red = TT.green = TT.grey = "";
+ if (FLAG(R)) toys.optflags |= FLAG_r;
+
// Grep exits with 2 for errors
toys.exitval = 2;