intcmp(1): initial rust impl
This commit is contained in:
parent
9addfc9284
commit
699893af89
78
src/intcmp.rs
Normal file
78
src/intcmp.rs
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
|
* the terms of the GNU Affero General Public License as published by the Free
|
||||||
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
||||||
|
* later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// /* 0b00? */ /* Equal | -e | 0b001 | 1 */
|
||||||
|
// #define EQUAL 0x01 /* Greater | -g | 0b010 | 2 */
|
||||||
|
// /* 0b0?0 */ /* Greater or Equal | -ge | 0b011 | 3 */
|
||||||
|
// #define GREATER 0x02 /* Less | -l | 0b100 | 4 */
|
||||||
|
// /* 0b?00 */ /* Less or Equal | -le | 0b101 | 5 */
|
||||||
|
// #define LESS 0x04 /* Inequal (Greater or Less) | -gl | 0b110 | 6 */
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
env::args,
|
||||||
|
process::ExitCode
|
||||||
|
};
|
||||||
|
|
||||||
|
fn usage(s: &str) -> ExitCode {
|
||||||
|
eprintln!("Usage: {} [-egl] integer integer...", s);
|
||||||
|
ExitCode::from(EX_USAGE as u8)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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,
|
||||||
|
_ => { return usage(&argv[0]); },
|
||||||
|
}
|
||||||
|
optind = opt.ind();
|
||||||
|
}
|
||||||
|
|
||||||
|
if argv.len() - optind < 2 /* ref cmp */ { return usage(&argv[0]); }
|
||||||
|
|
||||||
|
let mut reference = None;
|
||||||
|
let mut cmpn: usize;
|
||||||
|
|
||||||
|
for arg in argv.iter().skip(optind) {
|
||||||
|
match arg.parse::<usize>() {
|
||||||
|
Ok(n) => cmpn = 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)
|
||||||
|
{ return ExitCode::FAILURE; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ExitCode::SUCCESS
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user