From 3d8b96d58d74d775e34de8d19a03db5380eb8f2c Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 23 Aug 2010 02:39:47 +0200 Subject: grep: fix -o match with empty string (suggested by Colin Watson ) function old new delta grep_file 1216 1251 +35 Signed-off-by: Denys Vlasenko --- findutils/grep.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'findutils/grep.c') diff --git a/findutils/grep.c b/findutils/grep.c index 688ea6ab2..024f27609 100644 --- a/findutils/grep.c +++ b/findutils/grep.c @@ -461,15 +461,19 @@ static int grep_file(FILE *file) if (found) print_line(gl->pattern, strlen(gl->pattern), linenum, ':'); } else while (1) { + unsigned start = gl->matched_range.rm_so; unsigned end = gl->matched_range.rm_eo; + unsigned len = end - start; char old = line[end]; line[end] = '\0'; - print_line(line + gl->matched_range.rm_so, - end - gl->matched_range.rm_so, - linenum, ':'); + /* Empty match is not printed: try "echo test | grep -o ''" */ + if (len != 0) + print_line(line + start, len, linenum, ':'); if (old == '\0') break; line[end] = old; + if (len == 0) + end++; #if !ENABLE_EXTRA_COMPAT if (regexec(&gl->compiled_regex, line + end, 1, &gl->matched_range, REG_NOTBOL) != 0) -- cgit v1.2.3