intcmp(1): fixes intcmp without options falling through to integer parsing

This commit is contained in:
Emma Tebibyte 2024-08-10 14:23:53 -06:00
parent d89707a47c
commit e253cdf79c
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 20232024 DTB <trinity@trinity.moe> * Copyright (c) 20232024 DTB <trinity@trinity.moe>
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
* SPDX-License-Identifier: AGPL-3.0-or-later * SPDX-License-Identifier: AGPL-3.0-or-later
* *
* This program is free software: you can redistribute it and/or modify it under * This program is free software: you can redistribute it and/or modify it under
@ -22,11 +23,18 @@ use std::{
}; };
extern crate getopt; extern crate getopt;
use getopt::GetOpt;
extern crate sysexits; extern crate sysexits;
use getopt::GetOpt;
use sysexits::EX_USAGE; 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 { fn usage(s: &str) -> ExitCode {
eprintln!("Usage: {} [-egl] integer integer...", s); eprintln!("Usage: {} [-egl] integer integer...", s);
ExitCode::from(EX_USAGE as u8) ExitCode::from(EX_USAGE as u8)
@ -34,6 +42,15 @@ fn usage(s: &str) -> ExitCode {
fn main() -> ExitCode { fn main() -> ExitCode {
let argv = args().collect::<Vec<String>>(); 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 e = false; /* args can be == */
let mut g = false; /* args can be > */ let mut g = false; /* args can be > */
let mut l = false; /* args can be < */ let mut l = false; /* args can be < */
@ -46,11 +63,13 @@ fn main() -> ExitCode {
Ok("e") => e = true, Ok("e") => e = true,
Ok("g") => g = true, Ok("g") => g = true,
Ok("l") => l = true, Ok("l") => l = true,
_ => { return usage(&argv[0]); }, _ => return usage(&argv[0]),
} }
optind = opt.ind(); optind = opt.ind();
} }
if !e & !g & !l { return usage(&argv[0]); }
if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); } if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); }
let mut prev: Option<usize> = None; /* no previous operand */ 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 */ for arg in argv.iter().skip(optind) { /* iterate operands */
match arg.parse::<usize>() { /* parse current operand */ match arg.parse::<usize>() { /* parse current operand */
Ok(n) => currn = n, Ok(n) => currn = n,
_ => { Err(e) => {
eprintln!("{}: {}: Invalid integer", &argv[0], arg); eprintln!("{}: {}: {}", &argv[0], arg, e);
return ExitCode::from(EX_USAGE as u8); return ExitCode::from(EX_USAGE as u8);
} }
} }