intcmp(1): fixes intcmp without options falling through to integer parsing
This commit is contained in:
parent
d89707a47c
commit
e253cdf79c
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under
|
||||
@ -22,11 +23,18 @@ use std::{
|
||||
};
|
||||
|
||||
extern crate getopt;
|
||||
use getopt::GetOpt;
|
||||
|
||||
extern crate sysexits;
|
||||
|
||||
use getopt::GetOpt;
|
||||
use sysexits::EX_USAGE;
|
||||
|
||||
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||
#[cfg(target_os="openbsd")] extern crate strerror;
|
||||
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||
#[cfg(target_os="openbsd")] use strerror::StrError;
|
||||
|
||||
|
||||
fn usage(s: &str) -> ExitCode {
|
||||
eprintln!("Usage: {} [-egl] integer integer...", s);
|
||||
ExitCode::from(EX_USAGE as u8)
|
||||
@ -34,6 +42,15 @@ fn usage(s: &str) -> ExitCode {
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
|
||||
if cfg!(target_os="openbsd") {
|
||||
let promises = Promises::new("stdio");
|
||||
if let Err(e) = pledge(Some(promises), None) {
|
||||
eprintln!("{}: {}", argv[0], e.strerror());
|
||||
return ExitCode::from(EX_OSERR as u8);
|
||||
}
|
||||
}
|
||||
|
||||
let mut e = false; /* args can be == */
|
||||
let mut g = false; /* args can be > */
|
||||
let mut l = false; /* args can be < */
|
||||
@ -46,11 +63,13 @@ fn main() -> ExitCode {
|
||||
Ok("e") => e = true,
|
||||
Ok("g") => g = true,
|
||||
Ok("l") => l = true,
|
||||
_ => { return usage(&argv[0]); },
|
||||
_ => return usage(&argv[0]),
|
||||
}
|
||||
optind = opt.ind();
|
||||
}
|
||||
|
||||
if !e & !g & !l { return usage(&argv[0]); }
|
||||
|
||||
if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); }
|
||||
|
||||
let mut prev: Option<usize> = None; /* no previous operand */
|
||||
@ -59,8 +78,8 @@ fn main() -> ExitCode {
|
||||
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);
|
||||
Err(e) => {
|
||||
eprintln!("{}: {}: {}", &argv[0], arg, e);
|
||||
return ExitCode::from(EX_USAGE as u8);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user