hru(1): makes more efficient

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

View File

@ -19,7 +19,7 @@
use std::{ use std::{
cmp::Ordering, cmp::Ordering,
env::args, env::args,
io::{ stdin, stdout, Write }, io::{ Write, stdin, stdout },
process::{ ExitCode, exit }, process::{ ExitCode, exit },
}; };
@ -47,6 +47,16 @@ const LIST: [(u32, &str); 10] = [
(30, "Q"), /* quetta */ (30, "Q"), /* quetta */
]; ];
fn err(argv0: &String, message: String, code: Option<u8>) -> ExitCode {
eprintln!("{}: {}", argv0, message);
ExitCode::from(code.unwrap_or(1 /* unknown error */))
}
fn usage(argv0: &String) -> ExitCode {
eprintln!("Usage: {}", argv0);
ExitCode::from(EX_USAGE)
}
fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> { fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> {
/* preserve decimal places in output by casting to a float */ /* preserve decimal places in output by casting to a float */
let mut out = (input as f64, (0_u32, "")); let mut out = (input as f64, (0_u32, ""));
@ -81,22 +91,21 @@ fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> {
fn main() -> ExitCode { fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
if let Some(_) = argv.get(1) { if let Some(_) = argv.get(1) { return usage(&argv[0]); }
eprintln!("Usage: {}", argv[0]);
return ExitCode::from(EX_USAGE as u8);
}
#[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) {
eprintln!("{}: {}", argv[0], e.strerror()); return err(&argv[0], e.strerror(), Some(EX_OSERR));
return ExitCode::from(EX_OSERR);
} }
} }
let mut buf = String::new(); let mut buf = String::new();
while let Ok(_) = stdin().read_line(&mut buf) { if let Err(e) = stdin().read_line(&mut buf) {
return err(&argv[0], e.strerror(), Some(EX_IOERR));
}
if buf.is_empty() { return ExitCode::SUCCESS; } if buf.is_empty() { return ExitCode::SUCCESS; }
let n: u128 = match buf.trim().parse() { let n: u128 = match buf.trim().parse() {
@ -104,26 +113,22 @@ fn main() -> ExitCode {
buf.clear(); buf.clear();
f f
}, },
Err(e) => { Err(e) => return err(&argv[0], e.to_string(), Some(EX_DATAERR)),
eprintln!("{}: {}", argv[0], e);
return ExitCode::from(EX_DATAERR);
},
}; };
let (number, prefix) = convert(n).unwrap_or_else(|e| { let (number, prefix) = convert(n).unwrap_or_else(|e| {
eprintln!("{}: {}", argv[0], e); let _ = err(&argv[0], e.to_string(), None);
exit(EX_SOFTWARE.into()); exit(EX_SOFTWARE.into());
}); });
let si_prefix = format!("{}B", prefix.1); let si_prefix = prefix.1.to_owned() + "B";
/* round output number to one decimal place */ /* round output number to one decimal place */
let out = ((number * 10.0).round() / 10.0).to_string(); let rounded = (number * 10.0).round() / 10.0;
let out = rounded.to_string() + " " + &si_prefix + &'\n'.to_string();
if let Err(e) = stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) { if let Err(e) = stdout().write_all(out.as_bytes()) {
eprintln!("{}: {}", argv[0], e.strerror()); return err(&argv[0], e.strerror(), Some(EX_IOERR));
return ExitCode::from(EX_IOERR);
}
} }
ExitCode::SUCCESS ExitCode::SUCCESS