grep: -q match yields exit 0 even after a file error

GNU grep documents that with -q the exit status is 0 as soon as a line
is selected, even if an error (such as a missing file) occurred. uu_grep
let had_error take precedence, so 'grep -q PAT missing -' returned 2
instead of 0 when stdin matched. Give quiet+match priority in finish();
without -q, or with -q and no match, a file error still yields exit 2.
Fixes the GNU testsuite 'status' test.
This commit is contained in:
Sylvestre Ledru
2026-05-30 18:55:04 +02:00
parent 98e6bb6f53
commit 399d2d1192
2 changed files with 28 additions and 1 deletions
+6 -1
View File
@@ -117,7 +117,12 @@ impl<'a> Searcher<'a> {
.flush()
.map_err_context(|| "(standard output)".to_string())?;
if self.had_error {
// With -q, a match yields exit status 0 even if an error (e.g. a
// missing file) occurred earlier: GNU exits as soon as a line is
// selected, so the error never affects the status.
if self.config.quiet && self.any_match {
Ok(())
} else if self.had_error {
Err(ExitCode::new(2))
} else if self.any_match {
Ok(())