Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
};

let matcher = Matcher::compile(&config)?;

// grep with -m 0 should not open any file
if config.max_count == Some(0) {
return Err(ExitCode::new(1));
}

// An empty pattern matches every line; with `-v`, GNU grep selects no lines
// and exits as "no match" without reading any input files.
if invert_match && patterns.iter().any(|pattern| pattern.is_empty()) {
Expand Down
15 changes: 15 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,21 @@ fn max_count() {
.fails_with_code(1)
.no_stdout();

let (_s, mut c) = ucmd();
c.args(&["-m", "0", "a", "missing"])
.fails_with_code(1)
.no_output();

let (_s, mut c) = ucmd();
c.args(&["-m", "1", "a", "missing"])
.fails_with_code(2)
.stderr_contains("grep: missing:");

let (_s, mut c) = ucmd();
c.args(&["-m", "0", "-e", "["])
.fails_with_code(2)
.stderr_contains("invalid pattern");

// -A trailing context still printed after the cutoff.
let (_s, mut c) = ucmd();
c.args(&["-m", "1", "-A", "2", "match"])
Expand Down
Loading