From dd39aeff02bfdc71293906f2464181413f461247 Mon Sep 17 00:00:00 2001 From: emma Date: Thu, 22 Aug 2024 00:22:05 -0600 Subject: [PATCH] mm(1): adds ioerr(), usage(), and oser() functions --- src/mm.rs | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/mm.rs b/src/mm.rs index 10c6ef2..025b0f6 100644 --- a/src/mm.rs +++ b/src/mm.rs @@ -20,9 +20,9 @@ use std::{ env::args, fs::File, - io::{ stdin, stdout, stderr, BufWriter, Read, Write }, + io::{ Error, BufWriter, Read, Write, stderr, stdin, stdout }, os::fd::{ AsRawFd, FromRawFd }, - process::{ exit, ExitCode }, + process::{ ExitCode, exit}, }; extern crate getopt; @@ -47,15 +47,29 @@ use ArgMode::*; enum ArgMode { In, Out } +fn ioerr(argv0: &String, e: Error) -> ExitCode { + eprintln!("{}: {}", argv0, e.strerror()); + ExitCode::from(EX_IOERR as u8) +} + +#[cfg(target_os="openbsd")] +fn oserr(argv0: &String, e: Error) -> ExitCode { + eprintln!("{}: {}", argv0, e.strerror()); + ExitCode::from(EX_OSERR as u8) +} + +fn usage(argv0: &String) -> ExitCode { + eprintln!("Usage: {} [-aetu] [-i input] [-o output]", argv0); + ExitCode::from(EX_USAGE as u8) +} + fn main() -> ExitCode { let argv = args().collect::>(); - let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]); #[cfg(target_os="openbsd")] { let promises = Promises::new("cpath rpath stdio unveil wpath"); if let Err(e) = pledge(Some(promises), None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } @@ -85,8 +99,7 @@ fn main() -> ExitCode { mode = Some(Out); /* latest argument == -o */ }, Err(_) | Ok(_) => { - eprintln!("{}", usage); - return ExitCode::from(EX_USAGE as u8); + return usage(&argv[0]); }, }; @@ -111,8 +124,7 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['r']); if let Err(e) = unveil(Some(&input), Some(perms)) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } @@ -120,20 +132,17 @@ fn main() -> ExitCode { let perms = UnveilPerms::new(vec!['c', 'w']); if let Err(e) = unveil(Some(&output), Some(perms)) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } if let Err(e) = unveil(None, None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return oserr(&argv[0], e); } } if ins.is_empty() && outs.is_empty() && argv.len() > optind { - eprintln!("Usage: {}", usage); - return ExitCode::from(EX_USAGE as u8); + return usage(&argv[0]); } /* use stdin if no inputs are specified */ @@ -153,7 +162,7 @@ fn main() -> ExitCode { match File::open(file) { Ok(f) => f, Err(e) => { - eprintln!("{}: {}: {}", argv[0], file, e.strerror()); + let _ = ioerr(&argv[0], e); exit(EX_IOERR); }, } @@ -180,7 +189,7 @@ fn main() -> ExitCode { match options { Ok(f) => return f, Err(e) => { - eprintln!("{}: {}: {}", argv[0], file, e.strerror()); + let _ = ioerr(&argv[0], e); exit(EX_IOERR); }, }; @@ -205,21 +214,19 @@ fn main() -> ExitCode { for file in inputs { for byte in file.bytes().map(|b| { b.unwrap_or_else(|e| { - eprintln!("{}: {}", argv[0], e.strerror()); + let _ = ioerr(&argv[0], e); exit(EX_IOERR); }) }) { for out in &mut outputs { if let Err(e) = out.write(&[byte]) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_IOERR as u8); + return ioerr(&argv[0], e); } if u { /* immediately flush the output for -u */ if let Err(e) = out.flush() { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_IOERR as u8); + return ioerr(&argv[0], e); } } }