From f19b20f57e5354bcae72f0348bd9e503af79f073 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 14 Jun 2017 11:23:01 -0500 Subject: Grep exits with 2 for errors, which can happen at any time ( > /dev/full). --- toys/posix/grep.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'toys/posix/grep.c') diff --git a/toys/posix/grep.c b/toys/posix/grep.c index c0506845..d1452cec 100644 --- a/toys/posix/grep.c +++ b/toys/posix/grep.c @@ -73,6 +73,7 @@ GLOBALS( struct arg_list *S; char indelim, outdelim; + int found; ) // Emit line with various potential prefixes and delimiter @@ -194,8 +195,11 @@ static void do_grep(int fd, char *name) bars = 0; } mmatch++; - toys.exitval = 0; - if (toys.optflags & FLAG_q) xexit(); + TT.found = 1; + if (toys.optflags & FLAG_q) { + toys.exitval = 0; + xexit(); + } if (toys.optflags & FLAG_l) { xprintf("%s%c", name, TT.outdelim); free(line); @@ -361,6 +365,9 @@ void grep_main(void) { char **ss = toys.optargs; + // Grep exits with 2 for errors + toys.exitval = 2; + if (!TT.a) TT.a = TT.c; if (!TT.b) TT.b = TT.c; @@ -382,7 +389,6 @@ void grep_main(void) if (!(toys.optflags & FLAG_h) && toys.optc>1) toys.optflags |= FLAG_H; - toys.exitval = 1; if (toys.optflags & FLAG_s) { close(2); xopen_stdio("/dev/null", O_RDWR); @@ -395,4 +401,5 @@ void grep_main(void) else dirtree_read(*ss, do_grep_r); } } else loopfiles_rw(ss, O_RDONLY|WARN_ONLY, 0, do_grep); + toys.exitval = !TT.found; } -- cgit v1.2.3