intcmp(1): add comments
This commit is contained in:
parent
2f2270322a
commit
27ff64dffa
|
@ -34,45 +34,45 @@ fn usage(s: &str) -> ExitCode {
|
|||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
let mut can_eq = false;
|
||||
let mut can_gt = false;
|
||||
let mut can_lt = false;
|
||||
let mut e = false; /* args can be == */
|
||||
let mut g = false; /* args can be > */
|
||||
let mut l = false; /* args can be < */
|
||||
let mut optind = 0;
|
||||
|
||||
if argv.len() < 3 { return usage(&argv[0]); }
|
||||
|
||||
while let Some(opt) = argv.getopt("egl") {
|
||||
match opt.opt() {
|
||||
Ok("e") => can_eq = true,
|
||||
Ok("g") => can_gt = true,
|
||||
Ok("l") => can_lt = true,
|
||||
Ok("e") => e = true,
|
||||
Ok("g") => g = true,
|
||||
Ok("l") => l = true,
|
||||
_ => { return usage(&argv[0]); },
|
||||
}
|
||||
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 cmpn: usize;
|
||||
let mut prev: Option<usize> = None; /* no previous operand */
|
||||
let mut currn: usize;
|
||||
|
||||
for arg in argv.iter().skip(optind) {
|
||||
match arg.parse::<usize>() {
|
||||
Ok(n) => cmpn = n,
|
||||
for arg in argv.iter().skip(optind) { /* iterate operands */
|
||||
match arg.parse::<usize>() { /* parse current operand */
|
||||
Ok(n) => currn = n,
|
||||
_ => {
|
||||
eprintln!("{}: {}: Invalid integer", &argv[0], arg);
|
||||
return ExitCode::from(EX_USAGE as u8);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(refn) = reference {
|
||||
if (!can_eq && refn == cmpn)
|
||||
|| (!can_gt && refn > cmpn)
|
||||
|| (!can_lt && refn < cmpn)
|
||||
if let Some(prevn) = prev { /* if there was a previous opr., test */
|
||||
if (!e && prevn == currn)
|
||||
|| (!g && prevn > currn)
|
||||
|| (!l && prevn < currn)
|
||||
{ return ExitCode::FAILURE; }
|
||||
}
|
||||
|
||||
reference = Some(cmpn);
|
||||
prev = Some(currn); /* there is a previous operand */
|
||||
}
|
||||
|
||||
ExitCode::SUCCESS
|
||||
|
|
Loading…
Reference in New Issue
Block a user