[PATCH] grep: Fix -f FILE when FILE is empty and -x provided

Denys Vlasenko vda.linux at googlemail.com
Wed Apr 29 12:45:27 UTC 2020


Thank you.

There is a git config problem, though.
"Author name is not set properly (needs to be first and last name, and
it is 'Wolf' instead)"

Please choose a name which can pass our git configuration. Sorry.

On Mon, Apr 27, 2020 at 2:21 PM Wolf <wolf at wolfsden.cz> wrote:
>
> 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.
> ---
>  findutils/grep.c     | 5 +++--
>  testsuite/grep.tests | 6 ++++++
>  2 files changed, 9 insertions(+), 2 deletions(-)
>
> diff --git a/findutils/grep.c b/findutils/grep.c
> index 5b8644c36..252d467dc 100644
> --- a/findutils/grep.c
> +++ b/findutils/grep.c
> @@ -762,8 +762,9 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
>         if (option_mask32 & OPT_f) {
>                 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));
> +                       /* GNU grep treats it as "nothing matches" except when -x */
> +                       const char *data = (option_mask32 & OPT_x) == 0 ? "" : ".*";
> +                       llist_add_to(&pattern_head, new_grep_list_data((char*)data, 0));
>                         invert_search ^= 1;
>                 }
>         }
> 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" \
>         "" \
>         "test\n"
>
> +testing "grep -vxf EMPTY_FILE" \
> +       "grep -vxf input" \
> +       "test\n" \
> +       "" \
> +       "test\n"
> +
>  testing "grep -Fw matches only words" \
>         "grep -Fw foo input" \
>         "" \
> --
> 2.26.2
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox


More information about the busybox mailing list