diff options
author | Bartosz Golaszewski <bartekgola@gmail.com> | 2013-05-15 03:53:26 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-05-15 09:20:40 +0200 |
commit | 414db791d0dc79905e5afe20e503941b8f9778cc (patch) | |
tree | 4a58cd4e72f9744ea57e549f393b2a07891d2e64 | |
parent | e0a6ab698fb5d44ec7944278a69fdcebaf773ba2 (diff) | |
download | busybox-w32-414db791d0dc79905e5afe20e503941b8f9778cc.tar.gz busybox-w32-414db791d0dc79905e5afe20e503941b8f9778cc.tar.bz2 busybox-w32-414db791d0dc79905e5afe20e503941b8f9778cc.zip |
grep: don't bail out on first mismatch if '-w' option is set
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | findutils/grep.c | 22 | ||||
-rwxr-xr-x | testsuite/grep.tests | 6 |
2 files changed, 20 insertions, 8 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 70f3516a5..b808ad92b 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -373,6 +373,8 @@ static int grep_file(FILE *file) | |||
373 | opt_f_not_found: ; | 373 | opt_f_not_found: ; |
374 | } | 374 | } |
375 | } else { | 375 | } else { |
376 | char *match_at; | ||
377 | |||
376 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { | 378 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { |
377 | gl->flg_mem_alocated_compiled |= COMPILED; | 379 | gl->flg_mem_alocated_compiled |= COMPILED; |
378 | #if !ENABLE_EXTRA_COMPAT | 380 | #if !ENABLE_EXTRA_COMPAT |
@@ -388,32 +390,36 @@ static int grep_file(FILE *file) | |||
388 | gl->matched_range.rm_so = 0; | 390 | gl->matched_range.rm_so = 0; |
389 | gl->matched_range.rm_eo = 0; | 391 | gl->matched_range.rm_eo = 0; |
390 | #endif | 392 | #endif |
393 | match_at = line; | ||
394 | opt_w_again: | ||
391 | if ( | 395 | if ( |
392 | #if !ENABLE_EXTRA_COMPAT | 396 | #if !ENABLE_EXTRA_COMPAT |
393 | regexec(&gl->compiled_regex, line, 1, &gl->matched_range, 0) == 0 | 397 | regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, 0) == 0 |
394 | #else | 398 | #else |
395 | re_search(&gl->compiled_regex, line, line_len, | 399 | re_search(&gl->compiled_regex, match_at, line_len, |
396 | /*start:*/ 0, /*range:*/ line_len, | 400 | /*start:*/ 0, /*range:*/ line_len, |
397 | &gl->matched_range) >= 0 | 401 | &gl->matched_range) >= 0 |
398 | #endif | 402 | #endif |
399 | ) { | 403 | ) { |
400 | if (option_mask32 & OPT_x) { | 404 | if (option_mask32 & OPT_x) { |
401 | found = (gl->matched_range.rm_so == 0 | 405 | found = (gl->matched_range.rm_so == 0 |
402 | && line[gl->matched_range.rm_eo] == '\0'); | 406 | && match_at[gl->matched_range.rm_eo] == '\0'); |
403 | } else | 407 | } else |
404 | if (!(option_mask32 & OPT_w)) { | 408 | if (!(option_mask32 & OPT_w)) { |
405 | found = 1; | 409 | found = 1; |
406 | } else { | 410 | } else { |
407 | char c = ' '; | 411 | char c = ' '; |
408 | if (gl->matched_range.rm_so) | 412 | if (gl->matched_range.rm_so) |
409 | c = line[gl->matched_range.rm_so - 1]; | 413 | c = match_at[gl->matched_range.rm_so - 1]; |
410 | if (!isalnum(c) && c != '_') { | 414 | if (!isalnum(c) && c != '_') { |
411 | c = line[gl->matched_range.rm_eo]; | 415 | c = match_at[gl->matched_range.rm_eo]; |
412 | if (!c || (!isalnum(c) && c != '_')) | 416 | if (!c || (!isalnum(c) && c != '_')) { |
413 | found = 1; | 417 | found = 1; |
418 | } else { | ||
419 | match_at += gl->matched_range.rm_eo; | ||
420 | goto opt_w_again; | ||
421 | } | ||
414 | } | 422 | } |
415 | //BUG: "echo foop foo | grep -w foo" should match, but doesn't: | ||
416 | //we bail out on first "mismatch" because it's not a word. | ||
417 | } | 423 | } |
418 | } | 424 | } |
419 | } | 425 | } |
diff --git a/testsuite/grep.tests b/testsuite/grep.tests index 4781f2284..5696fa7e1 100755 --- a/testsuite/grep.tests +++ b/testsuite/grep.tests | |||
@@ -127,6 +127,12 @@ testing "grep -Fw doesn't stop on 1st mismatch" \ | |||
127 | "foop foo\n" \ | 127 | "foop foo\n" \ |
128 | "" | 128 | "" |
129 | 129 | ||
130 | testing "grep -w doesn't stop on 1st mismatch" \ | ||
131 | "grep -w foo input" \ | ||
132 | "foop foo\n" \ | ||
133 | "foop foo\n" \ | ||
134 | "" | ||
135 | |||
130 | # testing "test name" "commands" "expected result" "file input" "stdin" | 136 | # testing "test name" "commands" "expected result" "file input" "stdin" |
131 | # file input will be file called "input" | 137 | # file input will be file called "input" |
132 | # test can create a file "actual" instead of writing to stdout | 138 | # test can create a file "actual" instead of writing to stdout |