From 379fbbb77e76e7cdc85eb025b8045b2a9bd31c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20P=C3=A9ron?= Date: Thu, 30 Apr 2026 17:47:08 +0200 Subject: [PATCH] uucore: make the automatic flush at the end of binaries opt-outable ... To avoid duplicate error message when the util already does the job --- src/uu/cksum/src/main.rs | 2 +- src/uucore/src/lib/lib.rs | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/uu/cksum/src/main.rs b/src/uu/cksum/src/main.rs index 7b9fdbe76..acce8d94a 100644 --- a/src/uu/cksum/src/main.rs +++ b/src/uu/cksum/src/main.rs @@ -3,4 +3,4 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code.uucore::bin!(uu_cksum); -uucore::bin!(uu_cksum); +uucore::bin!(uu_cksum, no_flush); diff --git a/src/uucore/src/lib/lib.rs b/src/uucore/src/lib/lib.rs index dec7dc61a..e136c4d8e 100644 --- a/src/uucore/src/lib/lib.rs +++ b/src/uucore/src/lib/lib.rs @@ -183,13 +183,9 @@ pub fn get_canonical_util_name(util_name: &str) -> &str { } } -/// Execute utility code for `util`. -/// -/// This macro expands to a main function that invokes the `uumain` function in `util` -/// Exits with code returned by `uumain`. #[macro_export] -macro_rules! bin { - ($util:ident) => { +macro_rules! bin_inner { + ($util:ident, $post:expr) => { pub fn main() { use std::io::Write; use uucore::locale; @@ -213,13 +209,28 @@ macro_rules! bin { // execute utility code let code = $util::uumain(uucore::args_os()); + $post + + std::process::exit(code); + } + }; +} +/// Execute utility code for `util`. +/// +/// This macro expands to a main function that invokes the `uumain` function in `util` +/// Exits with code returned by `uumain`. +#[macro_export] +macro_rules! bin { + ($util:ident, no_flush) => { + ::uucore::bin_inner! {$util, {}} + }; + ($util:ident) => { + ::uucore::bin_inner! {$util, { // (defensively) flush stdout for utility prior to exit; see if let Err(e) = std::io::stdout().flush() { eprintln!("Error flushing stdout: {e}"); } - - std::process::exit(code); - } + }} }; }