Compare commits
17 Commits
20692d581a
...
e90d25e30f
Author | SHA1 | Date | |
---|---|---|---|
e90d25e30f | |||
71f372d2c2 | |||
27ff64dffa | |||
becb3bac4e | |||
e7a6632b41 | |||
48dbea0228 | |||
64f3f73d96 | |||
eda5385058 | |||
7a0ad78000 | |||
8dc763f05e | |||
efb3ce626d | |||
16f23e11c0 | |||
d87c278be5 | |||
5caefbb465 | |||
8d743dab7a | |||
2f2270322a | |||
699893af89 |
4
Makefile
4
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
|
||||
|
10
docs/mm.1
10
docs/mm.1
@ -3,7 +3,7 @@
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
|
||||
.\"
|
||||
.TH MM 1 2024-06-17 "Harakit X.X.X"
|
||||
.TH MM 1 2024-07-14 "Harakit X.X.X"
|
||||
.SH NAME
|
||||
mm \(en middleman
|
||||
.\"
|
||||
@ -30,12 +30,16 @@ Causes outputs to be overwritten instead of being truncated.
|
||||
Ensures neither input or output will be buffered.
|
||||
.IP \fB-i\fP\ \fIinput\fP
|
||||
Opens a path as an input. If one or more of the input files is \(lq-\(rq or if
|
||||
no inputs are specified, the standard input shall be used.
|
||||
no inputs are specified, the standard input shall be used. If specified as the
|
||||
last option and if there are trailing arguments to the program, they shall be
|
||||
appended to the list of files to use as inputs.
|
||||
.IP \fB-o\fP\ \fIoutput\fP
|
||||
Opens a path as an output. If one or more of the output files is \(lq-\(rq or if
|
||||
no outputs are specified and the
|
||||
.B -e
|
||||
option is not specified, the standard output shall be used.
|
||||
option is not specified, the standard output shall be used. If specified as the
|
||||
last option and if there are trailing arguments to the program, they shall be
|
||||
appended to the list of files to use as outputs.
|
||||
.\"
|
||||
.SH DIAGNOSTICS
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
|
||||
.\"
|
||||
.TH STRCMP 1 2024-06-17 "Harakit X.X.X"
|
||||
.TH STRCMP 1 2024-07-15 "Harakit X.X.X"
|
||||
.SH NAME
|
||||
strcmp \(en compare strings
|
||||
.\"
|
||||
@ -20,15 +20,15 @@ Check whether string arguments are the same.
|
||||
.SH DIAGNOSTICS
|
||||
|
||||
The program will exit successfully if the strings are identical. Otherwise, it
|
||||
will exit with an error code of 1 if a string passed has a lesser byte value
|
||||
than one of the prior strings:
|
||||
will exit with an error code less than 128 if a string passed has a lesser byte
|
||||
value than one of the prior strings:
|
||||
|
||||
.RS
|
||||
strcmp b a
|
||||
.RE
|
||||
|
||||
or with an error code of 255 if it has a greater byte value than one of the
|
||||
prior strings:
|
||||
or with an error code greater than 128 if it has a greater byte value than one
|
||||
of the prior strings:
|
||||
|
||||
.RS
|
||||
strcmp a b
|
||||
|
82
src/intcmp.c
82
src/intcmp.c
@ -1,82 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2023 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/.
|
||||
*/
|
||||
|
||||
#include <errno.h> /* errno */
|
||||
#include <stdio.h> /* fprintf(3), stderr */
|
||||
#include <stdlib.h> /* strtol(3), size_t, EXIT_FAILURE */
|
||||
#include <unistd.h> /* getopt(3), optind */
|
||||
#include <sysexits.h>
|
||||
|
||||
/* 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;
|
||||
}
|
79
src/intcmp.rs
Normal file
79
src/intcmp.rs
Normal file
@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
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") => e = true,
|
||||
Ok("g") => g = true,
|
||||
Ok("l") => l = true,
|
||||
_ => { return usage(&argv[0]); },
|
||||
}
|
||||
optind = opt.ind();
|
||||
}
|
||||
|
||||
if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); }
|
||||
|
||||
let mut prev: Option<usize> = None; /* no previous operand */
|
||||
let mut currn: usize;
|
||||
|
||||
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(prevn) = prev { /* if there was a previous opr., test */
|
||||
if (!e && prevn == currn)
|
||||
|| (!g && prevn > currn)
|
||||
|| (!l && prevn < currn)
|
||||
{ return ExitCode::FAILURE; }
|
||||
}
|
||||
|
||||
prev = Some(currn); /* there is a previous operand */
|
||||
}
|
||||
|
||||
ExitCode::SUCCESS
|
||||
}
|
29
src/mm.rs
29
src/mm.rs
@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||
* Copyright (c) 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
|
||||
@ -32,6 +33,10 @@ use getopt::GetOpt;
|
||||
use strerror::StrError;
|
||||
use sysexits::{ EX_IOERR, EX_USAGE };
|
||||
|
||||
use ArgMode::*;
|
||||
|
||||
enum ArgMode { In, Out }
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<_>>();
|
||||
let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]);
|
||||
@ -42,6 +47,8 @@ fn main() -> ExitCode {
|
||||
let mut u = false; /* unbuffer i/o */
|
||||
let mut ins = Vec::new(); /* initial input file path vector */
|
||||
let mut outs = Vec::new(); /* initial output file path vector */
|
||||
let mut mode: Option<ArgMode> = None; /* mode set by last-used option */
|
||||
let mut optind = 0;
|
||||
|
||||
while let Some(opt) = argv.getopt("aei:o:tu") {
|
||||
match opt.opt() {
|
||||
@ -49,19 +56,39 @@ fn main() -> ExitCode {
|
||||
Ok("e") => e = true,
|
||||
Ok("u") => u = true,
|
||||
Ok("t") => t = false,
|
||||
Ok("i") => { /* add input */
|
||||
Ok("i") => { /* add inputs */
|
||||
let input = opt.arg().unwrap();
|
||||
ins.push(input);
|
||||
mode = Some(In); /* latest argument == -i */
|
||||
},
|
||||
Ok("o") => { /* add output */
|
||||
let output = opt.arg().unwrap();
|
||||
outs.push(output);
|
||||
mode = Some(Out); /* latest argument == -o */
|
||||
},
|
||||
Err(_) | Ok(_) => {
|
||||
eprintln!("{}", usage);
|
||||
return ExitCode::from(EX_USAGE as u8);
|
||||
},
|
||||
};
|
||||
|
||||
optind = opt.ind();
|
||||
}
|
||||
|
||||
let remaining = argv.iter().skip(optind);
|
||||
|
||||
/* check the last flag specified */
|
||||
if let Some(m) = mode {
|
||||
for arg in remaining {
|
||||
/* move the subsequent arguments to the list of inputs or outputs */
|
||||
match m {
|
||||
In => ins.push(arg.to_string()),
|
||||
Out => outs.push(arg.to_string()),
|
||||
};
|
||||
}
|
||||
} else {
|
||||
eprintln!("{}", usage);
|
||||
return ExitCode::from(EX_USAGE as u8);
|
||||
}
|
||||
|
||||
/* use stdin if no inputs are specified */
|
||||
|
47
src/strcmp.c
47
src/strcmp.c
@ -1,24 +1,43 @@
|
||||
#include <stdio.h> /* fprintf(3), stderr */
|
||||
#include <stdlib.h> /* EXIT_FAILURE */
|
||||
#include <sysexits.h>
|
||||
/*
|
||||
* Copyright (c) 2022–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/.
|
||||
*/
|
||||
|
||||
static char *program_name = "strcmp";
|
||||
#include <stdio.h> /* fprintf(3), stderr */
|
||||
#include <stdlib.h> /* size_t */
|
||||
#include <sysexits.h> /* EX_USAGE */
|
||||
|
||||
char *program_name = "strcmp";
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int i;
|
||||
|
||||
if(argc < 3){
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "Usage: %s string string...\n",
|
||||
argv[0] == NULL ? program_name : argv[0]);
|
||||
argv[0] == NULL ? program_name : argv[0]
|
||||
);
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
for(; *argv[1] != '\0'; ++argv[1])
|
||||
for(i = 2; i < argc; ++i)
|
||||
if(*argv[i-1] > *argv[i])
|
||||
return 1;
|
||||
else if(*argv[i-1] < *argv[i]++)
|
||||
return -1; /* actually 255 */
|
||||
/* This compares the Nth character of arg[2] onward with argv[1]'s Nth
|
||||
* character, rather than comparing each arg with argv[1] sequentially. */
|
||||
for (; *argv[1] != '\0'; ++argv[1]) { /* iterate chars in argv[1] */
|
||||
for (size_t i = 2; i < argc; ++argv[i], ++i) { /* iterate &argv[2] */
|
||||
/* this never overruns because of nul termination */
|
||||
if (*argv[i-1] != *argv[i]) { return *argv[i-1] - *argv[i]; }
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user