hru(1): read stdin until EOF

This commit is contained in:
Emma Tebibyte 2024-02-13 17:48:25 -07:00
parent 1ee668aed6
commit d0205b71da
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -52,10 +52,14 @@ fn convert(input: u64) -> (f64, u32) {
fn main() -> ExitCode { fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
let mut buf = String::new(); let mut buf = String::new();
let _ = stdin().read_line(&mut buf); while let Ok(_) = stdin().read_line(&mut buf) {
if buf.is_empty() { return ExitCode::SUCCESS; }
let n: u64 = match buf.trim().parse() { let n: u64 = match buf.trim().parse() {
Ok(f) => f, Ok(f) => {
buf.clear();
f
},
Err(err) => { Err(err) => {
eprintln!("{}: {}", argv[0], err); eprintln!("{}: {}", argv[0], err);
return ExitCode::from(EX_DATAERR as u8); return ExitCode::from(EX_DATAERR as u8);
@ -81,6 +85,7 @@ fn main() -> ExitCode {
let out = ((number * 10.0).round() / 10.0).to_string(); let out = ((number * 10.0).round() / 10.0).to_string();
println!("{} {}", out, si_prefix); println!("{} {}", out, si_prefix);
}
ExitCode::SUCCESS ExitCode::SUCCESS
} }