From 5f0cb6639138380d700d4801af810e856d4a427e Mon Sep 17 00:00:00 2001 From: RelunSec Date: Mon, 29 Jun 2026 13:29:11 +0000 Subject: [PATCH 1/6] csplit print 0 on an error like gnu does fixes https://github.com/uutils/coreutils/issues/13139 --- src/uu/csplit/src/csplit.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 3a4b43e881e..74a7ccaf59b 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -179,7 +179,7 @@ where return Err(CsplitError::LineOutOfRangeOnRepetition( pattern_as_str, ith - 1, - )); + )); } Err(err) => return Err(err), // continue the splitting process @@ -211,7 +211,7 @@ where return Err(CsplitError::MatchNotFoundOnRepetition( pattern_as_str, ith - 1, - )); + )); } (Err(err), _) => return Err(err), // continue the splitting process @@ -634,14 +634,25 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .map(Borrow::borrow) .collect(); let options = CsplitOptions::new(&matches)?; - if file_name == "-" { + + let res = if file_name == "-" { let stdin = io::stdin(); - Ok(csplit(&options, &patterns, stdin.lock())?) + csplit(&options, &patterns, stdin.lock()).map_err(|e| e.into()) } else { let file = File::open(file_name) - .map_err_context(|| format!("cannot open {} for reading", file_name.quote()))?; - Ok(csplit(&options, &patterns, BufReader::new(file))?) + .map_err_context(|| format!("cannot open {} for reading", file_name.quote())); + + match file { + Ok(f) => csplit(&options, &patterns, BufReader::new(f)).map_err(|e| e.into()), + Err(e) => Err(e), + } + }; + + if res.is_err() { + println!("0"); } + + res } pub fn uu_app() -> Command { From 1833101dccd66ac83d1a5f9f38d334d2cd9c5786 Mon Sep 17 00:00:00 2001 From: RelunSec Date: Mon, 29 Jun 2026 13:41:58 +0000 Subject: [PATCH 2/6] Fix syntax errors in error handling and formatting --- src/uu/csplit/src/csplit.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 74a7ccaf59b..f49de30f5b3 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -179,7 +179,7 @@ where return Err(CsplitError::LineOutOfRangeOnRepetition( pattern_as_str, ith - 1, - )); + )); } Err(err) => return Err(err), // continue the splitting process @@ -211,7 +211,7 @@ where return Err(CsplitError::MatchNotFoundOnRepetition( pattern_as_str, ith - 1, - )); + )); } (Err(err), _) => return Err(err), // continue the splitting process @@ -634,14 +634,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .map(Borrow::borrow) .collect(); let options = CsplitOptions::new(&matches)?; - + let res = if file_name == "-" { let stdin = io::stdin(); csplit(&options, &patterns, stdin.lock()).map_err(|e| e.into()) } else { let file = File::open(file_name) .map_err_context(|| format!("cannot open {} for reading", file_name.quote())); - + match file { Ok(f) => csplit(&options, &patterns, BufReader::new(f)).map_err(|e| e.into()), Err(e) => Err(e), From 8e62d8b1f782be5b9432b0b129408162ea836a8e Mon Sep 17 00:00:00 2001 From: RelunSec Date: Mon, 29 Jun 2026 13:48:02 +0000 Subject: [PATCH 3/6] Refactor error handling in csplit.rs to fix cargo clippy warnings --- src/uu/csplit/src/csplit.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index f49de30f5b3..387b1c77405 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -637,13 +637,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let res = if file_name == "-" { let stdin = io::stdin(); - csplit(&options, &patterns, stdin.lock()).map_err(|e| e.into()) + csplit(&options, &patterns, stdin.lock()).map_err(Into::into) } else { let file = File::open(file_name) .map_err_context(|| format!("cannot open {} for reading", file_name.quote())); match file { - Ok(f) => csplit(&options, &patterns, BufReader::new(f)).map_err(|e| e.into()), + Ok(f) => csplit(&options, &patterns, BufReader::new(f)).map_err(Into::into), Err(e) => Err(e), } }; From 38c7eeeb8112eb62bd13322ec3704ad320c34080 Mon Sep 17 00:00:00 2001 From: RelunSec Date: Mon, 29 Jun 2026 13:53:02 +0000 Subject: [PATCH 4/6] Print 0 after the error not before --- src/uu/csplit/src/csplit.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 387b1c77405..b842e6ad3a8 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -648,8 +648,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } }; - if res.is_err() { + if let Err(ref e) = res { + uucore::show_error!("{}", e); println!("0"); + std::process::exit(1); } res From 5a362e9125486e896f4bd90942c1f20c14270db6 Mon Sep 17 00:00:00 2001 From: RelunSec Date: Mon, 29 Jun 2026 14:15:45 +0000 Subject: [PATCH 5/6] Fix review and avoid std::process::exit --- src/uu/csplit/src/csplit.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index b842e6ad3a8..70987def3e6 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -639,19 +639,22 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let stdin = io::stdin(); csplit(&options, &patterns, stdin.lock()).map_err(Into::into) } else { - let file = File::open(file_name) - .map_err_context(|| format!("cannot open {} for reading", file_name.quote())); - - match file { - Ok(f) => csplit(&options, &patterns, BufReader::new(f)).map_err(Into::into), - Err(e) => Err(e), - } + File::open(file_name) + .map_err_context(|| format!("cannot open {} for reading", file_name.quote())) + .and_then(|file| csplit(&options, &patterns, BufReader::new(file)).map_err(Into::into)) }; if let Err(ref e) = res { + // Print error first uucore::show_error!("{}", e); - println!("0"); - std::process::exit(1); + + // Print 0 second + let stdout = io::stdout(); + let mut handle = stdout.lock(); + let _ = writeln!(handle, "0"); + + uucore::error::set_exit_code(1); + return Ok(()); } res From ec8fb45e3b0c88914665dd049ea182fff329ef33 Mon Sep 17 00:00:00 2001 From: RelunSec Date: Mon, 29 Jun 2026 14:57:03 +0000 Subject: [PATCH 6/6] Simplify stdout handle initialization --- src/uu/csplit/src/csplit.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/uu/csplit/src/csplit.rs b/src/uu/csplit/src/csplit.rs index 70987def3e6..0cdb12c62dc 100644 --- a/src/uu/csplit/src/csplit.rs +++ b/src/uu/csplit/src/csplit.rs @@ -649,8 +649,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { uucore::show_error!("{}", e); // Print 0 second - let stdout = io::stdout(); - let mut handle = stdout.lock(); + let mut handle = io::stdout(); let _ = writeln!(handle, "0"); uucore::error::set_exit_code(1);