From 6817e114bb320f197727740d7905a5cb2849bdfa Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 21 Feb 2021 11:27:16 -0600 Subject: Teach -o to print ranges that produce zero length matches. And fix one test for NUL that should be a length test for -z support --- toys/posix/grep.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'toys/posix') diff --git a/toys/posix/grep.c b/toys/posix/grep.c index 2ddb6b93..cd8928a1 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -218,8 +218,12 @@ static void do_grep(int fd, char *name) } } - if (!rc && FLAG(x)) - if (mm->rm_so || line[mm->rm_eo]) rc = 1; + if (!rc && FLAG(o) && !mm->rm_eo && ulen>start-line) { + start++; + continue; + } + + if (!rc && FLAG(x) && (mm->rm_so || ulen-(start-line)!=mm->rm_eo)) rc = 1; if (!rc && FLAG(w)) { char c = 0; @@ -240,10 +244,8 @@ static void do_grep(int fd, char *name) if (FLAG(v)) { if (FLAG(o)) { - if (rc) { - mm->rm_so = 0; - mm->rm_eo = ulen-(start-line); - } else if (!mm->rm_so) { + if (rc) mm->rm_eo = ulen-(start-line); + else if (!mm->rm_so) { start += mm->rm_eo; continue; } else mm->rm_eo = mm->rm_so; -- cgit v1.2.3