swab(1): changes disparate error handling functions to one function

This commit is contained in:
Emma Tebibyte 2024-08-24 17:26:26 -06:00
parent 5eb71e90a3
commit ff4ff825bd
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -35,15 +35,9 @@ use strerror::StrError;
#[cfg(target_os="openbsd")] extern crate openbsd; #[cfg(target_os="openbsd")] extern crate openbsd;
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; #[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
fn err(argv0: &str, e: Error, code: u8) -> ExitCode {
fn oserr(argv0: &str, e: Error) -> ExitCode {
eprintln!("{}: {}", argv0, e.strerror()); eprintln!("{}: {}", argv0, e.strerror());
ExitCode::from(EX_OSERR) ExitCode::from(code)
}
fn ioerr(argv0: &str, e: Error) -> ExitCode {
eprintln!("{}: {}", argv0, e.strerror());
ExitCode::from(EX_IOERR)
} }
fn usage(s: &str) -> ExitCode { fn usage(s: &str) -> ExitCode {
@ -57,7 +51,7 @@ fn main() -> ExitCode {
#[cfg(target_os="openbsd")] { #[cfg(target_os="openbsd")] {
let promises = Promises::new("stdio"); let promises = Promises::new("stdio");
if let Err(e) = pledge(Some(promises), None) { if let Err(e) = pledge(Some(promises), None) {
return oserr(&argv[0], e); return err(&argv[0], e, EX_OSERR);
} }
} }
@ -94,16 +88,16 @@ fn main() -> ExitCode {
if let Err(e) = output.write(&right) if let Err(e) = output.write(&right)
.and_then(|_| output.write(&left)) { .and_then(|_| output.write(&left)) {
break ioerr(&argv[0], e); break err(&argv[0], e, EX_IOERR);
} }
}, },
Ok(v) => { // partial read; partially write Ok(v) => { // partial read; partially write
if let Err(e) = output.write(&buf[..v]) { if let Err(e) = output.write(&buf[..v]) {
break ioerr(&argv[0], e); break err(&argv[0], e, EX_IOERR);
} }
}, },
Err(e) => break oserr(&argv[0], e) Err(e) => break err(&argv[0], e, EX_OSERR)
} }
} }
} }