GNU grep treats -m 0 / --max-count=0 as "stop before reading any line": it
never opens the input file at all, so a nonexistent file produces no error and
the run exits 1 (no match). uu_grep opens the file first and only then
applies the max-count, so a missing file yields a No such file or directory
error and exit 2.
Rust (incorrect - errors on the missing file)
$ ./target/release/grep -e x -m 0 /no/such/file
grep: /no/such/file: No such file or directory (os error 2)
# Exit code: 2
GNU (correct - short-circuits, never opens the file)
$ LC_ALL=C /usr/bin/grep -e x -m 0 /no/such/file
# (no output)
# Exit code: 1
Scope
- The divergence is specific to
-m 0. With -m 1 on the same missing file
both implementations error and exit 2.
- On an existing file,
-m 0 already behaves correctly in both (no output,
exit 1) — only the file-opening side effect differs.
GNU grep treats
-m 0/--max-count=0as "stop before reading any line": itnever opens the input file at all, so a nonexistent file produces no error and
the run exits
1(no match).uu_grepopens the file first and only thenapplies the max-count, so a missing file yields a
No such file or directoryerror and exit
2.Rust (incorrect - errors on the missing file)
$ ./target/release/grep -e x -m 0 /no/such/file grep: /no/such/file: No such file or directory (os error 2) # Exit code: 2GNU (correct - short-circuits, never opens the file)
Scope
-m 0. With-m 1on the same missing fileboth implementations error and exit
2.-m 0already behaves correctly in both (no output,exit 1) — only the file-opening side effect differs.