1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

sort: fix exit codes and error messages

This commit is contained in:
Michael Debertol 2021-07-25 22:24:37 +02:00
parent 12dc3db972
commit 891d25cebd
4 changed files with 26 additions and 13 deletions

View file

@ -175,7 +175,7 @@ pub fn read<T: Read>(
// because it was only temporarily transmuted to a Vec<Line<'static>> to make recycling possible. // because it was only temporarily transmuted to a Vec<Line<'static>> to make recycling possible.
std::mem::transmute::<Vec<Line<'static>>, Vec<Line<'_>>>(lines) std::mem::transmute::<Vec<Line<'static>>, Vec<Line<'_>>>(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 { let mut line_data = LineData {
selections, selections,
num_infos, num_infos,
@ -313,7 +313,7 @@ fn read_to_buffer<T: Read>(
Err(e) if e.kind() == ErrorKind::Interrupted => { Err(e) if e.kind() == ErrorKind::Interrupted => {
// retry // retry
} }
Err(e) => crash!(1, "{}", e), Err(e) => crash!(2, "{}", e),
} }
} }
} }

View file

@ -211,7 +211,7 @@ fn read_write_loop<I: WriteableTmpFile>(
} }
let tmp_dir = crash_if_err!( let tmp_dir = crash_if_err!(
1, 2,
tempfile::Builder::new() tempfile::Builder::new()
.prefix("uutils_sort") .prefix("uutils_sort")
.tempdir_in(tmp_dir_parent) .tempdir_in(tmp_dir_parent)

View file

@ -215,7 +215,7 @@ impl GlobalSettings {
Ok(f) => BufWriter::new(Box::new(f) as Box<dyn Write>), Ok(f) => BufWriter::new(Box::new(f) as Box<dyn Write>),
Err(e) => { Err(e) => {
show_error!("{0}: {1}", filename, e.to_string()); 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<dyn Write>), None => BufWriter::new(Box::new(stdout()) as Box<dyn Write>),
@ -942,7 +942,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let usage = get_usage(); let usage = get_usage();
let mut settings: GlobalSettings = Default::default(); 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); 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 */ /* if no file, default to stdin */
files.push("-".to_owned()); files.push("-".to_owned());
} else if settings.check && files.len() != 1 { } 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) { if let Some(arg) = matches.args.get(options::SEPARATOR) {
let separator = arg.vals[0].to_string_lossy(); let separator = arg.vals[0].to_string_lossy();
let separator = separator; let separator = separator;
if separator.len() != 1 { 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()) settings.separator = Some(separator.chars().next().unwrap())
} }
@ -1338,7 +1340,7 @@ fn exec(files: &[String], settings: &GlobalSettings) -> i32 {
file_merger.write_all(settings); file_merger.write_all(settings);
} else if settings.check { } else if settings.check {
if files.len() > 1 { 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); return check::check(files.first().unwrap(), settings);
} else { } else {
@ -1623,7 +1625,11 @@ fn print_sorted<'a, T: Iterator<Item = &'a Line<'a>>>(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<OsStr>) -> Box<dyn Read + Send> { fn open(path: impl AsRef<OsStr>) -> Box<dyn Read + Send> {
let path = path.as_ref(); let path = path.as_ref();
if path == "-" { if path == "-" {
@ -1631,10 +1637,17 @@ fn open(path: impl AsRef<OsStr>) -> Box<dyn Read + Send> {
return Box::new(stdin) as Box<dyn Read + Send>; return Box::new(stdin) as Box<dyn Read + Send>;
} }
match File::open(Path::new(path)) { let path = Path::new(path);
match File::open(path) {
Ok(f) => Box::new(f) as Box<dyn Read + Send>, Ok(f) => Box::new(f) as Box<dyn Read + Send>,
Err(e) => { 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())
);
} }
} }
} }

View file

@ -839,9 +839,9 @@ fn test_nonexistent_file() {
.status_code(2) .status_code(2)
.stderr_only( .stderr_only(
#[cfg(not(windows))] #[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)] #[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.",
); );
} }