diff --git a/src/uu/sort/src/chunks.rs b/src/uu/sort/src/chunks.rs index 9e9d212c2..5ab98392d 100644 --- a/src/uu/sort/src/chunks.rs +++ b/src/uu/sort/src/chunks.rs @@ -175,7 +175,7 @@ pub fn read( // because it was only temporarily transmuted to a Vec> to make recycling possible. std::mem::transmute::>, Vec>>(lines) }; - let read = crash_if_err!(1, std::str::from_utf8(&buffer[..read])); + let read = crash_if_err!(2, std::str::from_utf8(&buffer[..read])); let mut line_data = LineData { selections, num_infos, @@ -313,7 +313,7 @@ fn read_to_buffer( Err(e) if e.kind() == ErrorKind::Interrupted => { // retry } - Err(e) => crash!(1, "{}", e), + Err(e) => crash!(2, "{}", e), } } } diff --git a/src/uu/sort/src/ext_sort.rs b/src/uu/sort/src/ext_sort.rs index e0814b7a2..201dda8bb 100644 --- a/src/uu/sort/src/ext_sort.rs +++ b/src/uu/sort/src/ext_sort.rs @@ -211,7 +211,7 @@ fn read_write_loop( } let tmp_dir = crash_if_err!( - 1, + 2, tempfile::Builder::new() .prefix("uutils_sort") .tempdir_in(tmp_dir_parent) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 91365b603..3d7e890b9 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -215,7 +215,7 @@ impl GlobalSettings { Ok(f) => BufWriter::new(Box::new(f) as Box), Err(e) => { show_error!("{0}: {1}", filename, e.to_string()); - panic!("Could not open output file"); + crash!(2, "Could not open output file"); } }, None => BufWriter::new(Box::new(stdout()) as Box), @@ -942,7 +942,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 { let usage = get_usage(); let mut settings: GlobalSettings = Default::default(); - let matches = uu_app().usage(&usage[..]).get_matches_from(args); + let matches = uu_app().usage(&usage[..]).get_matches_from_safe(args); + + let matches = crash_if_err!(2, matches); settings.debug = matches.is_present(options::DEBUG); @@ -1060,14 +1062,14 @@ pub fn uumain(args: impl uucore::Args) -> i32 { /* if no file, default to stdin */ files.push("-".to_owned()); } else if settings.check && files.len() != 1 { - crash!(1, "extra operand `{}' not allowed with -c", files[1]) + crash!(2, "extra operand `{}' not allowed with -c", files[1]) } if let Some(arg) = matches.args.get(options::SEPARATOR) { let separator = arg.vals[0].to_string_lossy(); let separator = separator; if separator.len() != 1 { - crash!(1, "separator must be exactly one character long"); + crash!(2, "separator must be exactly one character long"); } settings.separator = Some(separator.chars().next().unwrap()) } @@ -1338,7 +1340,7 @@ fn exec(files: &[String], settings: &GlobalSettings) -> i32 { file_merger.write_all(settings); } else if settings.check { if files.len() > 1 { - crash!(1, "only one file allowed with -c"); + crash!(2, "only one file allowed with -c"); } return check::check(files.first().unwrap(), settings); } else { @@ -1623,7 +1625,11 @@ fn print_sorted<'a, T: Iterator>>(iter: T, settings: &Global } } -// from cat.rs +/// Strips the trailing " (os error XX)" from io error strings. +fn strip_errno(err: &str) -> &str { + &err[..err.find(" (os error ").unwrap_or(err.len())] +} + fn open(path: impl AsRef) -> Box { let path = path.as_ref(); if path == "-" { @@ -1631,10 +1637,17 @@ fn open(path: impl AsRef) -> Box { return Box::new(stdin) as Box; } - match File::open(Path::new(path)) { + let path = Path::new(path); + + match File::open(path) { Ok(f) => Box::new(f) as Box, Err(e) => { - crash!(2, "cannot read: {0:?}: {1}", path, e); + crash!( + 2, + "cannot read: {0}: {1}", + path.to_string_lossy(), + strip_errno(&e.to_string()) + ); } } } diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 4a1cc3fa9..e594de0a7 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -839,9 +839,9 @@ fn test_nonexistent_file() { .status_code(2) .stderr_only( #[cfg(not(windows))] - "sort: cannot read: \"nonexistent.txt\": No such file or directory (os error 2)", + "sort: cannot read: nonexistent.txt: No such file or directory", #[cfg(windows)] - "sort: cannot read: \"nonexistent.txt\": The system cannot find the file specified. (os error 2)", + "sort: cannot read: nonexistent.txt: The system cannot find the file specified.", ); }