diff --git a/src/lib.rs b/src/lib.rs index 7fd4174..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,7 +360,8 @@ 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 (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 75961f7..ff79638 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(all(unix, not(target_os = "cygwin")))] #[test] fn dereference_recursive_follows_symlinks() {