[PATCH] grep -F/fgrep support for -i

Rob Landley rob at landley.net
Fri Apr 30 03:40:40 UTC 2010


On Wednesday 28 April 2010 16:56:49 Ian Wienand wrote:
> Hi,
>
>  From my reading of POSIX, grep -F should probably support -i
>
> ---
> -F Match using fixed strings. Treat each pattern specified as a string
>     instead of a regular expression.
>
> -i Perform pattern matching in searches without regard to case
> ---
>
> -i doesn't really say it does *not* apply to non-regex patterns.  Most
>   (all?) other greps also seem to respect the -i
>
> Below is a little patch with a simple test-case that fixes it for me.
>
> Thanks,
>
> -i
>
> function                                             old     new   delta
> grep_file                                            907     970     +63
> ---------------------------------------------------------------------------
>--- (add/remove: 0/0 grow/shrink: 1/0 up/down: 63/0)               Total: 63
> bytes text	   data	    bss	    dec	    hex	filename
>   701414	   2093	   9064	 712571	  adf7b	busybox_old
>   701477	   2093	   9064	 712634	  adfba	busybox_unstripped
>
> ianw at jj:~/programs/busybox/testsuite$ ./runtest grep
> ...
> PASS: grep -F handles -i
>
> Signed-off-by: Ian Wienand <ianw at vmware.com>
>
> diff --git a/findutils/grep.c b/findutils/grep.c
> index 40caef4..0e88bc4 100644
> --- a/findutils/grep.c
> +++ b/findutils/grep.c
> @@ -254,7 +254,15 @@ static int grep_file(FILE *file)
>   		while (pattern_ptr) {
>   			gl = (grep_list_data_t *)pattern_ptr->data;
>   			if (FGREP_FLAG) {
> -				found |= (strstr(line, gl->pattern) != NULL);
> +				char *l = line;
> +				if (option_mask32 & OPT_i) {
> +					l = xstrdup(line);
> +					str_tolower(l);
> +					str_tolower(gl->pattern);
> +				}
> +				found |= (strstr(l, gl->pattern) != NULL);
> +				if (l != line)
> +					free(l);
>   			} else {
>   				if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
>   					gl->flg_mem_alocated_compiled |= COMPILED;
> diff --git a/testsuite/grep.tests b/testsuite/grep.tests
> index 8692307..26d77cc 100755
> --- a/testsuite/grep.tests
> +++ b/testsuite/grep.tests
> @@ -75,6 +75,8 @@ testing "grep handles multiple regexps" "grep -e one -e
> two input ; echo \$?" \ "one\ntwo\n0\n" "one\ntwo\n" ""
>   testing "grep -F handles multiple expessions" "grep -F -e one -e two
> input ; echo \$?" \ "one\ntwo\n0\n" "one\ntwo\n" ""
> +testing "grep -F handles -i" "grep -F -i foo input ; echo \$?" \
> +    "FOO\n0\n" "FOO\n" ""
>
>   # -f file/-
>   testing "grep can read regexps from stdin" "grep -f - input ; echo \$?" \

This might be an excuse for using that CONFIG_DESKTOP guard.  Not worth a new 
config symbol, but we got along without these 60 bytes before...

Possibly instead of CONFIG_DESKTOP it's more like CONFIG_FLUFFY.  It really 
means "extra features that embedded systems probably won't need".  It's just 
as much "server", "build environment", "standards pedanticism"...

Rob
-- 
Latency is more important than throughput. It's that simple. - Linus Torvalds


More information about the busybox mailing list