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,49 +91,44 @@ 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) {
if buf.is_empty() { return ExitCode::SUCCESS; } return err(&argv[0], e.strerror(), Some(EX_IOERR));
}
let n: u128 = match buf.trim().parse() { if buf.is_empty() { return ExitCode::SUCCESS; }
Ok(f) => {
buf.clear();
f
},
Err(e) => {
eprintln!("{}: {}", argv[0], e);
return ExitCode::from(EX_DATAERR);
},
};
let (number, prefix) = convert(n).unwrap_or_else(|e| { let n: u128 = match buf.trim().parse() {
eprintln!("{}: {}", argv[0], e); Ok(f) => {
exit(EX_SOFTWARE.into()); buf.clear();
}); f
},
Err(e) => return err(&argv[0], e.to_string(), Some(EX_DATAERR)),
};
let si_prefix = format!("{}B", prefix.1); let (number, prefix) = convert(n).unwrap_or_else(|e| {
let _ = err(&argv[0], e.to_string(), None);
exit(EX_SOFTWARE.into());
});
/* round output number to one decimal place */ let si_prefix = prefix.1.to_owned() + "B";
let out = ((number * 10.0).round() / 10.0).to_string();
if let Err(e) = stdout().write_all(format!("{} {}\n", out, si_prefix).as_bytes()) { /* round output number to one decimal place */
eprintln!("{}: {}", argv[0], e.strerror()); let rounded = (number * 10.0).round() / 10.0;
return ExitCode::from(EX_IOERR); let out = rounded.to_string() + " " + &si_prefix + &'\n'.to_string();
}
if let Err(e) = stdout().write_all(out.as_bytes()) {
return err(&argv[0], e.strerror(), Some(EX_IOERR));
} }
ExitCode::SUCCESS ExitCode::SUCCESS