[PATCH] grep: properly handle grep -w "^str"

Bartosz Golaszewski bartekgola at gmail.com
Thu Feb 6 20:34:47 UTC 2014


Pass the REG_NOTBOL flag to subsequent regexec calls.

function                                             old     new   delta
grep_file                                           1995    2008     +13
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 13/0)               Total: 13 bytes
   text    data     bss     dec     hex filename
1224816    2131    9092 1236039  12dc47 busybox_old
1224829    2131    9092 1236052  12dc54 busybox_unstripped

Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
---
 findutils/grep.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/findutils/grep.c b/findutils/grep.c
index a7bc4ca..7cd9c19 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -375,6 +375,8 @@ static int grep_file(FILE *file)
 			} else {
 #if ENABLE_EXTRA_COMPAT
 				unsigned start_pos;
+#else
+				int match_flg;
 #endif
 				char *match_at;
 
@@ -392,15 +394,15 @@ static int grep_file(FILE *file)
 #if !ENABLE_EXTRA_COMPAT
 				gl->matched_range.rm_so = 0;
 				gl->matched_range.rm_eo = 0;
+				match_flg = 0;
 #else
 				start_pos = 0;
 #endif
 				match_at = line;
  opt_w_again:
-//bb_error_msg("'%s' start_pos:%d line_len:%d", match_at, start_pos, line_len);
 				if (
 #if !ENABLE_EXTRA_COMPAT
-					regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, 0) == 0
+					regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, match_flg) == 0
 #else
 					re_search(&gl->compiled_regex, match_at, line_len,
 							start_pos, /*range:*/ line_len,
@@ -432,6 +434,7 @@ static int grep_file(FILE *file)
 #if !ENABLE_EXTRA_COMPAT
 								if (gl->matched_range.rm_eo != 0) {
 									match_at += gl->matched_range.rm_eo;
+									match_flg |= REG_NOTBOL;
 									goto opt_w_again;
 								}
 #else
-- 
1.8.4.5



More information about the busybox mailing list