aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLauri Kasanen <curaga@operamail.com>2011-08-28 12:39:04 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2011-08-28 12:39:04 +0200
commit7b46220d922d7c6267a8442ff8c3a6d1ab106727 (patch)
tree599a640d94d61d38350d127c36cb234871e213ba
parent2390109dcbc7769c8cc373db32805206522ca9a6 (diff)
downloadbusybox-w32-7b46220d922d7c6267a8442ff8c3a6d1ab106727.tar.gz
busybox-w32-7b46220d922d7c6267a8442ff8c3a6d1ab106727.tar.bz2
busybox-w32-7b46220d922d7c6267a8442ff8c3a6d1ab106727.zip
grep: be GNU compatible with -f EMPTY_FILE
Signed-off-by: Lauri Kasanen <curaga@operamail.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--findutils/grep.c22
-rwxr-xr-xtestsuite/grep.tests18
2 files changed, 30 insertions, 10 deletions
diff --git a/findutils/grep.c b/findutils/grep.c
index 3acfa9197..5f4224203 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -562,20 +562,20 @@ static char *add_grep_list_data(char *pattern)
562 562
563static void load_regexes_from_file(llist_t *fopt) 563static void load_regexes_from_file(llist_t *fopt)
564{ 564{
565 char *line;
566 FILE *f;
567
568 while (fopt) { 565 while (fopt) {
566 char *line;
567 FILE *fp;
569 llist_t *cur = fopt; 568 llist_t *cur = fopt;
570 char *ffile = cur->data; 569 char *ffile = cur->data;
571 570
572 fopt = cur->link; 571 fopt = cur->link;
573 free(cur); 572 free(cur);
574 f = xfopen_stdin(ffile); 573 fp = xfopen_stdin(ffile);
575 while ((line = xmalloc_fgetline(f)) != NULL) { 574 while ((line = xmalloc_fgetline(fp)) != NULL) {
576 llist_add_to(&pattern_head, 575 llist_add_to(&pattern_head,
577 new_grep_list_data(line, ALLOCATED)); 576 new_grep_list_data(line, ALLOCATED));
578 } 577 }
578 fclose_if_not_stdin(fp);
579 } 579 }
580} 580}
581 581
@@ -659,15 +659,19 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
659#endif 659#endif
660 invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */ 660 invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
661 661
662 if (pattern_head != NULL) { 662 { /* convert char **argv to grep_list_data_t */
663 /* convert char **argv to grep_list_data_t */
664 llist_t *cur; 663 llist_t *cur;
665
666 for (cur = pattern_head; cur; cur = cur->link) 664 for (cur = pattern_head; cur; cur = cur->link)
667 cur->data = new_grep_list_data(cur->data, 0); 665 cur->data = new_grep_list_data(cur->data, 0);
668 } 666 }
669 if (option_mask32 & OPT_f) 667 if (option_mask32 & OPT_f) {
670 load_regexes_from_file(fopt); 668 load_regexes_from_file(fopt);
669 if (!pattern_head) { /* -f EMPTY_FILE? */
670 /* GNU grep treats it as "nothing matches" */
671 llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0));
672 invert_search ^= 1;
673 }
674 }
671 675
672 if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') 676 if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
673 option_mask32 |= OPT_F; 677 option_mask32 |= OPT_F;
diff --git a/testsuite/grep.tests b/testsuite/grep.tests
index ffce033e6..006a215e1 100755
--- a/testsuite/grep.tests
+++ b/testsuite/grep.tests
@@ -7,7 +7,7 @@
7 7
8. ./testing.sh 8. ./testing.sh
9 9
10# testing "test name" "options" "expected result" "file input" "stdin" 10# testing "test name" "commands" "expected result" "file input" "stdin"
11# file input will be file called "input" 11# file input will be file called "input"
12# test can create a file "actual" instead of writing to stdout 12# test can create a file "actual" instead of writing to stdout
13 13
@@ -103,4 +103,20 @@ testing "grep -o does not loop forever on zero-length match" \
103 "" \ 103 "" \
104 "" "test\n" 104 "" "test\n"
105 105
106testing "grep -f EMPTY_FILE" \
107 "grep -f input" \
108 "" \
109 "" \
110 "test\n"
111
112testing "grep -v -f EMPTY_FILE" \
113 "grep -v -f input" \
114 "test\n" \
115 "" \
116 "test\n"
117
118# testing "test name" "commands" "expected result" "file input" "stdin"
119# file input will be file called "input"
120# test can create a file "actual" instead of writing to stdout
121
106exit $FAILCOUNT 122exit $FAILCOUNT