intcmp(1): add comments

This commit is contained in:
dtb 2024-07-15 14:51:54 -06:00
parent 2f2270322a
commit 27ff64dffa
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -34,45 +34,45 @@ fn usage(s: &str) -> ExitCode {
fn main() -> ExitCode { fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); let argv = args().collect::<Vec<String>>();
let mut can_eq = false; let mut e = false; /* args can be == */
let mut can_gt = false; let mut g = false; /* args can be > */
let mut can_lt = false; let mut l = false; /* args can be < */
let mut optind = 0; let mut optind = 0;
if argv.len() < 3 { return usage(&argv[0]); } if argv.len() < 3 { return usage(&argv[0]); }
while let Some(opt) = argv.getopt("egl") { while let Some(opt) = argv.getopt("egl") {
match opt.opt() { match opt.opt() {
Ok("e") => can_eq = true, Ok("e") => e = true,
Ok("g") => can_gt = true, Ok("g") => g = true,
Ok("l") => can_lt = true, Ok("l") => l = true,
_ => { return usage(&argv[0]); }, _ => { return usage(&argv[0]); },
} }
optind = opt.ind(); optind = opt.ind();
} }
if argv.len() - optind < 2 /* ref cmp */ { return usage(&argv[0]); } if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); }
let mut reference: Option<usize> = None; let mut prev: Option<usize> = None; /* no previous operand */
let mut cmpn: usize; let mut currn: usize;
for arg in argv.iter().skip(optind) { for arg in argv.iter().skip(optind) { /* iterate operands */
match arg.parse::<usize>() { match arg.parse::<usize>() { /* parse current operand */
Ok(n) => cmpn = n, Ok(n) => currn = n,
_ => { _ => {
eprintln!("{}: {}: Invalid integer", &argv[0], arg); eprintln!("{}: {}: Invalid integer", &argv[0], arg);
return ExitCode::from(EX_USAGE as u8); return ExitCode::from(EX_USAGE as u8);
} }
} }
if let Some(refn) = reference { if let Some(prevn) = prev { /* if there was a previous opr., test */
if (!can_eq && refn == cmpn) if (!e && prevn == currn)
|| (!can_gt && refn > cmpn) || (!g && prevn > currn)
|| (!can_lt && refn < cmpn) || (!l && prevn < currn)
{ return ExitCode::FAILURE; } { return ExitCode::FAILURE; }
} }
reference = Some(cmpn); prev = Some(currn); /* there is a previous operand */
} }
ExitCode::SUCCESS ExitCode::SUCCESS