From 06384c72fb9ca9742e95a34f944e89f49f7776f4 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 28 Aug 2024 00:09:40 -0600 Subject: [PATCH] intcmp(1): adds null unveil --- src/intcmp.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/intcmp.rs b/src/intcmp.rs index 456f268..9efc8be 100644 --- a/src/intcmp.rs +++ b/src/intcmp.rs @@ -26,27 +26,35 @@ extern crate getopt; extern crate sysexits; use getopt::GetOpt; -use sysexits::EX_USAGE; +use sysexits::{ EX_DATAERR, EX_USAGE }; #[cfg(target_os="openbsd")] use sysexits::EX_OSERR; #[cfg(target_os="openbsd")] extern crate openbsd; #[cfg(target_os="openbsd")] extern crate strerror; -#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge }; +#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge, unveil }; #[cfg(target_os="openbsd")] use strerror::StrError; -fn usage(s: &str) -> ExitCode { - eprintln!("Usage: {} [-egl] integer integer...", s); - ExitCode::from(EX_USAGE as u8) +fn err(argv0: &String, e: String, code: u8) -> ExitCode { + eprintln!("{}: {}", argv0, e); + ExitCode::from(code) +} + +fn usage(argv0: &str) -> ExitCode { + eprintln!("Usage: {} [-egl] integer integer...", argv0); + ExitCode::from(EX_USAGE) } fn main() -> ExitCode { let argv = args().collect::>(); #[cfg(target_os="openbsd")] { - let promises = Promises::new("stdio"); + let promises = Promises::new("stdio unveil"); if let Err(e) = pledge(Some(promises), None) { - eprintln!("{}: {}", argv[0], e.strerror()); - return ExitCode::from(EX_OSERR as u8); + return err(&argv[0], e.strerror(), EX_OSERR); + } + + if let Err(e) = unveil(None, None) { + return err(&argv[0], e.strerror(), EX_OSERR); } } @@ -78,8 +86,8 @@ fn main() -> ExitCode { match arg.parse::() { /* parse current operand */ Ok(n) => currn = n, Err(e) => { - eprintln!("{}: {}: {}", &argv[0], arg, e); - return ExitCode::from(EX_USAGE as u8); + let error = arg.to_owned() + ": " + &e.to_string(); + return err(&argv[0], error, EX_DATAERR); } }