diff --git a/src/uu/cp/src/cp.rs b/src/uu/cp/src/cp.rs index 62b8b7a7bcf..044e85975e0 100644 --- a/src/uu/cp/src/cp.rs +++ b/src/uu/cp/src/cp.rs @@ -39,7 +39,7 @@ use uucore::{backup_control, update_control}; // requires these enum. pub use uucore::{backup_control::BackupMode, update_control::UpdateMode}; use uucore::{ - format_usage, parser::shortcut_value_parser::ShortcutValueParser, prompt_yes, show_error, + format_usage, parser::shortcut_value_parser::ShortcutValueParser, print_error, prompt_yes, show_warning, }; @@ -833,7 +833,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // code should still be EXIT_ERR as does GNU cp CpError::NotAllFilesCopied => {} // Else we caught a fatal bubbled-up error, log it to stderr - _ => show_error!("{error}"), + _ => print_error!("{error}"), } set_exit_code(EXIT_ERR); } @@ -1344,10 +1344,10 @@ fn show_error_if_needed(error: &CpError) { // Format IoErrContext using strip_errno to remove "(os error N)" suffix // for GNU-compatible output CpError::IoErrContext(io_err, context) => { - show_error!("{}: {}", context, uucore::error::strip_errno(io_err)); + print_error!("{}: {}", context, uucore::error::strip_errno(io_err)); } _ => { - show_error!("{error}"); + print_error!("{error}"); } } } diff --git a/src/uucore/src/lib/macros.rs b/src/uucore/src/lib/macros.rs index 068c06519dc..4f466ef4e50 100644 --- a/src/uucore/src/lib/macros.rs +++ b/src/uucore/src/lib/macros.rs @@ -134,20 +134,9 @@ macro_rules! show_if_err( }) ); -/// Show an error to stderr in a similar style to GNU coreutils. -/// -/// Takes a [`format!`]-like input and prints it to stderr. The output is -/// prepended with the current utility's name. -/// -/// # Examples -/// -/// ``` -/// # #[macro_use] -/// # extern crate uucore; -/// # fn main() { -/// show_error!("Couldn't apply {} to {}", "foo", "bar"); -/// # } -/// ``` +// todo: Investigate how to show an error to stderr +// show_error! cause abort with 2>/dev/full +// Maybe, we should switch to print_error! macro ? #[macro_export] macro_rules! show_error( ($($args:tt)+) => ({ @@ -156,6 +145,16 @@ macro_rules! show_error( }) ); +#[macro_export] +macro_rules! print_error( + ($($args:tt)+) => ({ + use std::io::Write as _; + let mut error = std::io::stderr().lock(); + let _ = write!(error, "{}: ", $crate::util_name()); + let _ = writeln!(error, $($args)+); + }) +); + /// Print a warning message to stderr. /// /// Takes [`format!`]-compatible input and prepends it with the current