Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/uu/csplit/src/csplit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,29 @@ 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(Into::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))?)
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);

// Print 0 second
let mut handle = io::stdout();
let _ = writeln!(handle, "0");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GNU catches write error of stdout and ignore write error of stderr (with few expections).
Also lock() is not useful since write is done just once at here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so if that, that was a bit complicated, not all errors gnu print 0, that was the issue making it harder, what type of error it do that, we need known


uucore::error::set_exit_code(1);
return Ok(());
}

res
}

pub fn uu_app() -> Command {
Expand Down
Loading