From 699893af892788d10f8c4664ae6b08a3f7d4017d Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 03:09:00 -0600 Subject: [PATCH 1/3] intcmp(1): initial rust impl --- src/intcmp.rs | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/intcmp.rs diff --git a/src/intcmp.rs b/src/intcmp.rs new file mode 100644 index 0000000..470e751 --- /dev/null +++ b/src/intcmp.rs @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023–2024 DTB + * 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::>(); + 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::() { + 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 +} From 2f2270322adb2839c4a18298090624a4db12f36b Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 03:14:57 -0600 Subject: [PATCH 2/3] intcmp(1): rewrite in rust --- Makefile | 4 +-- src/intcmp.c | 82 --------------------------------------------------- src/intcmp.rs | 17 ++++++----- 3 files changed, 11 insertions(+), 92 deletions(-) delete mode 100644 src/intcmp.c diff --git a/Makefile b/Makefile index 5e2c70d..8f1f6d9 100644 --- a/Makefile +++ b/Makefile @@ -109,8 +109,8 @@ build/bin/hru: src/hru.rs build rustlibs .PHONY: intcmp intcmp: build/bin/intcmp -build/bin/intcmp: src/intcmp.c build - $(CC) $(CFLAGS) -o $@ src/intcmp.c +build/bin/intcmp: src/intcmp.rs build rustlibs + $(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/intcmp.rs .PHONY: mm mm: build/bin/mm diff --git a/src/intcmp.c b/src/intcmp.c deleted file mode 100644 index d6dff0d..0000000 --- a/src/intcmp.c +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2023 DTB - * 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/. - */ - -#include /* errno */ -#include /* fprintf(3), stderr */ -#include /* strtol(3), size_t, EXIT_FAILURE */ -#include /* getopt(3), optind */ -#include - -/* 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 */ - -static char *program_name = "intcmp"; - -int main(int argc, char *argv[]){ - int c; - size_t i; - unsigned char mode; - int r; /* reference integer */ - - mode = 0; - - if(argc < 3) - goto usage; - - while((c = getopt(argc, argv, "egl")) != -1) - switch(c){ - case 'e': mode |= EQUAL; break; - case 'g': mode |= GREATER; break; - case 'l': mode |= LESS; break; - default: goto usage; - } - - if(optind + 2 /* ref cmp */ > argc){ -usage: fprintf(stderr, - "Usage: %s [-egl] integer integer...\n", - argv[0] == NULL ? program_name : argv[0]); - return EX_USAGE; - } - - i = optind; - - do{ r = c; - c = strtol(argv[i], &argv[i], 10); - if(*argv[i] != '\0' || errno != 0){ - fprintf(stderr, "%s: argument #%d: Invalid integer\n", - argv[0], (int)i); - return EX_USAGE; - } - - if(i == optind) - continue; - - /* rule enforcement; if a mode isn't permitted and the numbers - * correspond to it, return 1 */ - if( (!(mode & EQUAL) && r == c) - || (!(mode & GREATER) && r > c) - || (!(mode & LESS) && r < c)) - return 1; - }while(++i < argc); - - return 0; -} diff --git a/src/intcmp.rs b/src/intcmp.rs index 470e751..88be72c 100644 --- a/src/intcmp.rs +++ b/src/intcmp.rs @@ -16,18 +16,17 @@ * 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 }; +extern crate getopt; +use getopt::GetOpt; + +extern crate sysexits; +use sysexits::EX_USAGE; + fn usage(s: &str) -> ExitCode { eprintln!("Usage: {} [-egl] integer integer...", s); ExitCode::from(EX_USAGE as u8) @@ -54,7 +53,7 @@ fn main() -> ExitCode { if argv.len() - optind < 2 /* ref cmp */ { return usage(&argv[0]); } - let mut reference = None; + let mut reference: Option = None; let mut cmpn: usize; for arg in argv.iter().skip(optind) { @@ -72,6 +71,8 @@ fn main() -> ExitCode { || (!can_lt && refn < cmpn) { return ExitCode::FAILURE; } } + + reference = Some(cmpn); } ExitCode::SUCCESS From 27ff64dffabd38555524fdd5b2838af7ec7cc5a4 Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 14:51:54 -0600 Subject: [PATCH 3/3] intcmp(1): add comments --- src/intcmp.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/intcmp.rs b/src/intcmp.rs index 88be72c..f504b50 100644 --- a/src/intcmp.rs +++ b/src/intcmp.rs @@ -34,45 +34,45 @@ fn usage(s: &str) -> ExitCode { fn main() -> ExitCode { let argv = args().collect::>(); - 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 = None; - let mut cmpn: usize; + let mut prev: Option = None; /* no previous operand */ + let mut currn: usize; - for arg in argv.iter().skip(optind) { - match arg.parse::() { - Ok(n) => cmpn = n, + for arg in argv.iter().skip(optind) { /* iterate operands */ + match arg.parse::() { /* 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