diff --git a/src/output.rs b/src/output.rs index dc40bf6..30cdc5f 100644 --- a/src/output.rs +++ b/src/output.rs @@ -8,6 +8,7 @@ use crate::context_buffer::LineView; use std::ffi::OsStr; use std::io::{self, BufWriter, StdoutLock, Write}; use std::path::Path; +use uucore::error::strip_errno; #[cfg(target_pointer_width = "64")] const BUF_SIZE: usize = 128 * 1024; @@ -195,7 +196,12 @@ impl<'a> OutputWriter<'a> { /// Write an IO error to stderr. pub fn report_io_error(&self, label: &OsStr, err: &io::Error) { if !self.config.no_messages && !self.config.quiet { - eprintln!("grep: {label}: {err}", label = label.to_string_lossy()); + // Strip the trailing " (os error XX)" so the message matches GNU grep. + eprintln!( + "grep: {label}: {err}", + label = label.to_string_lossy(), + err = strip_errno(err) + ); } } diff --git a/tests/test_grep.rs b/tests/test_grep.rs index df629f2..a623f46 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1030,6 +1030,16 @@ fn nonexistent_file_is_error() { .stderr_contains("does-not-exist"); } +#[test] +fn nonexistent_file_error_has_no_os_error_suffix() { + // GNU prints "grep: : No such file or directory" with no + // " (os error 2)" suffix; strip_errno keeps us byte-compatible. + let (_s, mut c) = ucmd(); + c.args(&["x", "does-not-exist"]) + .fails_with_code(2) + .stderr_is("grep: does-not-exist: No such file or directory\n"); +} + #[test] fn dash_argument_means_stdin() { let (_s, mut c) = ucmd();