From 86fdc702984732745240a06634cc7e1ba965dce4 Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Mon, 9 Nov 2026 13:33:00 +0000 Subject: [PATCH 1/2] grep: read --exclude-from=- from standard input GNU grep treats `-` as standard input for --exclude-from, as it already does for -f. uu_grep read the operand as a path, so `printf '*.log\n' | grep -rl --exclude-from=- PAT dir` failed with "grep: -: No such file or directory" (exit 2), and silently used a file literally named `-` when one happened to exist. Use the same `-` branch as the -f loop: read stdin and label read errors "(standard input)". Fixes #167 Signed-off-by: bfs2021 <101915083+bfs2021@users.noreply.github.com> --- src/lib.rs | 10 +++++++++- tests/test_grep.rs | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 7fd4174..9da30f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -353,7 +353,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { matches.indices_of("exclude_from"), ) { for (i, path) in indices.zip(paths) { - let contents = std::fs::read_to_string(path).map_err(|err| read_error(path, &err))?; + let contents = if path == "-" { + let mut buf = String::new(); + std::io::stdin() + .read_to_string(&mut buf) + .map_err(|err| read_error("(standard input)", &err))?; + buf + } else { + std::fs::read_to_string(path).map_err(|err| read_error(path, &err))? + }; for line in contents.lines() { let trimmed = line.trim(); if !trimmed.is_empty() { diff --git a/tests/test_grep.rs b/tests/test_grep.rs index cafce7c..e47554c 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1659,6 +1659,19 @@ fn recursive_exclude_from_file() { .stdout_does_not_contain("b.log"); } +#[test] +fn exclude_from_dash_reads_standard_input() { + // GNU grep reads the exclude list from standard input when the operand is + // `-`; uu_grep used to look for a file literally named `-`. + let (scene, mut c) = ucmd(); + build_tree(&scene); + c.args(&["-r", "-l", "--exclude-from=-", "grep", "tree"]) + .pipe_in("*.log\n") + .succeeds() + .stdout_contains("a.txt") + .stdout_does_not_contain("b.log"); +} + #[cfg(unix)] #[test] fn dereference_recursive_follows_symlinks() { From 37443be11f3548a742a46d63f879997f3499c53c Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Tue, 10 Nov 2026 08:40:47 +0800 Subject: [PATCH 2/2] grep: read both pattern-file operands through one helper `-f` and `--exclude-from` each carried their own `-` branch; a shared helper returns the error label and the contents, so the operand is tested once. --- src/lib.rs | 41 +++++++++++++++++++---------------------- tests/test_grep.rs | 5 +---- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5efdbd9..95080c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -137,6 +137,21 @@ fn read_error(label: impl std::fmt::Display, err: &std::io::Error) -> Box (&str, std::io::Result) { + if path == "-" { + let mut buf = String::new(); + ( + "(standard input)", + std::io::stdin().read_to_string(&mut buf).map(|_| buf), + ) + } else { + (path, std::fs::read_to_string(path)) + } +} + #[uucore::main(no_signals)] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let args = expand_num_shorthand(args); @@ -241,16 +256,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } for path in file_pattern { - let contents = if *path == "-" { - let mut buf = String::new(); - std::io::stdin() - .read_to_string(&mut buf) - .map_err(|err| read_error("(standard input)", &err))?; - buf - } else { - std::fs::read_to_string(path).map_err(|err| read_error(path, &err))? - }; - pattern_strings.push(contents); + let (label, contents) = read_pattern_file(path); + pattern_strings.push(contents.map_err(|err| read_error(label, &err))?); } for contents in &pattern_strings { if !contents.is_empty() { @@ -353,18 +360,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { matches.indices_of("exclude_from"), ) { for (i, path) in indices.zip(paths) { - let label = if path == "-" { - "(standard input)" - } else { - path.as_str() - }; - let contents = if path == "-" { - let mut buf = String::new(); - std::io::stdin().read_to_string(&mut buf).map(|_| buf) - } else { - std::fs::read_to_string(path) - } - .map_err(|err| read_error(label, &err))?; + let (label, contents) = read_pattern_file(path); + let contents = contents.map_err(|err| read_error(label, &err))?; for line in contents.lines() { let trimmed = line.trim(); if !trimmed.is_empty() { diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 51cb22e..ff79638 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1666,10 +1666,7 @@ fn exclude_from_dash_reads_standard_input() { let (scene, mut c) = ucmd(); build_tree(&scene); c.args(&["-r", "-l", "--exclude-from=-", "grep", "tree"]) - .pipe_in( - "*.log -", - ) + .pipe_in("*.log\n") .succeeds() .stdout_contains("a.txt") .stdout_does_not_contain("b.log");