From 5194d4ad66ad130cb730e0b192ba1e2b5181184d Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 11 Jul 2019 12:22:31 -0700 Subject: grep: add -R as well as -r. On BSD these are actually the same, and there's a -S that you need in addition. So strictly this is a behavior change for Android (which is going from BSD grep to toybox grep), but it's a behavior preserving change for the AOSP build (which is going from GNU grep to toybox grep), and the latter actually has a checked-in use of -R where the former doesn't. --- tests/grep.test | 9 +++++++++ toys/posix/grep.c | 7 +++++-- 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 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; -- cgit v1.2.3