aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGray Wolf <wolf@wolfsden.cz>2020-04-29 15:49:17 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2020-04-29 16:01:42 +0200
commitc3295d233b6a7d924814eec9a5c5999a876daf9e (patch)
tree2281617cafa2ec0e9def7bf0ee5da3280e724c77
parent1c462d47a0bc92f9f57af223456df53169acf3fe (diff)
downloadbusybox-w32-c3295d233b6a7d924814eec9a5c5999a876daf9e.tar.gz
busybox-w32-c3295d233b6a7d924814eec9a5c5999a876daf9e.tar.bz2
busybox-w32-c3295d233b6a7d924814eec9a5c5999a876daf9e.zip
grep: Fix -f FILE when FILE is empty and -x provided
Grep currently special-cased empty pattern file to be the same as pattern file with one empty line (empty pattern). That does mirror how GNU grep behaves, except when -x is provided. In that case .* pattern needs to be used instead. Signed-off-by: Gray Wolf <wolf@wolfsden.cz> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--findutils/grep.c5
-rwxr-xr-xtestsuite/grep.tests6
2 files changed, 9 insertions, 2 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 5b8644c36..84a6f7b1c 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -762,8 +762,9 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
762 if (option_mask32 & OPT_f) { 762 if (option_mask32 & OPT_f) {
763 load_regexes_from_file(fopt); 763 load_regexes_from_file(fopt);
764 if (!pattern_head) { /* -f EMPTY_FILE? */ 764 if (!pattern_head) { /* -f EMPTY_FILE? */
765 /* GNU grep treats it as "nothing matches" */ 765 /* GNU grep treats it as "nothing matches" except when -x */
766 llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0)); 766 const char *data = (option_mask32 & OPT_x) ? ".*" : "";
767 llist_add_to(&pattern_head, new_grep_list_data((char*)data, 0));
767 invert_search ^= 1; 768 invert_search ^= 1;
768 } 769 }
769 } 770 }
diff --git a/testsuite/grep.tests b/testsuite/grep.tests
index 26f8e69cf..9c1f35499 100755
--- a/testsuite/grep.tests
+++ b/testsuite/grep.tests
@@ -129,6 +129,12 @@ testing "grep -v -f EMPTY_FILE" \
129 "" \ 129 "" \
130 "test\n" 130 "test\n"
131 131
132testing "grep -vxf EMPTY_FILE" \
133 "grep -vxf input" \
134 "test\n" \
135 "" \
136 "test\n"
137
132testing "grep -Fw matches only words" \ 138testing "grep -Fw matches only words" \
133 "grep -Fw foo input" \ 139 "grep -Fw foo input" \
134 "" \ 140 "" \