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>
This commit is contained in:
Lauri Kasanen 2011-08-28 12:39:04 +02:00 committed by Denys Vlasenko
parent 2390109dcb
commit 7b46220d92
2 changed files with 30 additions and 10 deletions

View File

@ -562,20 +562,20 @@ static char *add_grep_list_data(char *pattern)
static void load_regexes_from_file(llist_t *fopt) static void load_regexes_from_file(llist_t *fopt)
{ {
char *line;
FILE *f;
while (fopt) { while (fopt) {
char *line;
FILE *fp;
llist_t *cur = fopt; llist_t *cur = fopt;
char *ffile = cur->data; char *ffile = cur->data;
fopt = cur->link; fopt = cur->link;
free(cur); free(cur);
f = xfopen_stdin(ffile); fp = xfopen_stdin(ffile);
while ((line = xmalloc_fgetline(f)) != NULL) { while ((line = xmalloc_fgetline(fp)) != NULL) {
llist_add_to(&pattern_head, llist_add_to(&pattern_head,
new_grep_list_data(line, ALLOCATED)); new_grep_list_data(line, ALLOCATED));
} }
fclose_if_not_stdin(fp);
} }
} }
@ -659,15 +659,19 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
#endif #endif
invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */ invert_search = ((option_mask32 & OPT_v) != 0); /* 0 | 1 */
if (pattern_head != NULL) { { /* convert char **argv to grep_list_data_t */
/* convert char **argv to grep_list_data_t */
llist_t *cur; llist_t *cur;
for (cur = pattern_head; cur; cur = cur->link) for (cur = pattern_head; cur; cur = cur->link)
cur->data = new_grep_list_data(cur->data, 0); cur->data = new_grep_list_data(cur->data, 0);
} }
if (option_mask32 & OPT_f) if (option_mask32 & OPT_f) {
load_regexes_from_file(fopt); load_regexes_from_file(fopt);
if (!pattern_head) { /* -f EMPTY_FILE? */
/* GNU grep treats it as "nothing matches" */
llist_add_to(&pattern_head, new_grep_list_data((char*) "", 0));
invert_search ^= 1;
}
}
if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f')
option_mask32 |= OPT_F; option_mask32 |= OPT_F;

View File

@ -7,7 +7,7 @@
. ./testing.sh . ./testing.sh
# testing "test name" "options" "expected result" "file input" "stdin" # testing "test name" "commands" "expected result" "file input" "stdin"
# file input will be file called "input" # file input will be file called "input"
# test can create a file "actual" instead of writing to stdout # test can create a file "actual" instead of writing to stdout
@ -103,4 +103,20 @@ testing "grep -o does not loop forever on zero-length match" \
"" \ "" \
"" "test\n" "" "test\n"
testing "grep -f EMPTY_FILE" \
"grep -f input" \
"" \
"" \
"test\n"
testing "grep -v -f EMPTY_FILE" \
"grep -v -f input" \
"test\n" \
"" \
"test\n"
# testing "test name" "commands" "expected result" "file input" "stdin"
# file input will be file called "input"
# test can create a file "actual" instead of writing to stdout
exit $FAILCOUNT exit $FAILCOUNT