2023-12-24 17:13:17 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023 DTB <trinity@trinity.moe>
|
2024-07-12 15:54:30 -06:00
|
|
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
2023-12-24 17:13:17 -07:00
|
|
|
* 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 */
|
2023-12-25 15:56:38 -07:00
|
|
|
#include <stdlib.h> /* strtol(3), size_t, EXIT_FAILURE */
|
2023-12-24 17:13:17 -07:00
|
|
|
#include <unistd.h> /* getopt(3), optind */
|
2024-07-13 16:57:52 -06:00
|
|
|
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
2023-12-24 17:13:17 -07:00
|
|
|
|
2024-07-28 21:06:53 -06:00
|
|
|
#define /* 0b00? */ EQUAL 0x01 /* | -e | 0b001 | 1 */
|
|
|
|
#define /* 0b0?0 */ GREATER 0x02 /* | -g | 0b010 | 2 */
|
|
|
|
/* Equal | Greater 0x03 | -ge | 0b011 | 3 */
|
|
|
|
#define /* 0b?00 */ LESSER 0x04 /* | -l | 0b100 | 4 */
|
|
|
|
/* Equal | Lesser 0x05 | -le | 0b101 | 5 */
|
|
|
|
/* (Inequal) Greater | Lesser 0x06 | -gl | 0b110 | 6 */
|
2023-12-24 17:13:17 -07:00
|
|
|
|
2024-07-24 19:37:07 -06:00
|
|
|
char *program_name = "intcmp";
|
2023-12-24 17:13:17 -07:00
|
|
|
|
2024-07-27 18:32:04 -06:00
|
|
|
static int
|
|
|
|
usage(char *argv0) {
|
|
|
|
(void)fprintf(stderr, "Usage: %s [-egl] integer integer...\n", argv0);
|
2024-07-13 17:01:53 -06:00
|
|
|
|
2024-07-12 15:54:30 -06:00
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2023-12-24 17:13:17 -07:00
|
|
|
int c;
|
|
|
|
size_t i;
|
|
|
|
unsigned char mode;
|
|
|
|
int r; /* reference integer */
|
|
|
|
mode = 0;
|
|
|
|
|
2024-07-28 18:31:01 -06:00
|
|
|
if (argc < 3) {
|
|
|
|
return usage(argv[0] == NULL ? program_name : argv[0]);
|
|
|
|
}
|
2023-12-24 17:13:17 -07:00
|
|
|
|
2024-07-12 15:54:30 -06:00
|
|
|
while ((c = getopt(argc, argv, "egl")) != -1) {
|
2024-07-15 13:38:08 -06:00
|
|
|
switch (c) {
|
2024-07-12 15:54:30 -06:00
|
|
|
case 'e': mode |= EQUAL; break;
|
|
|
|
case 'g': mode |= GREATER; break;
|
|
|
|
case 'l': mode |= LESSER; break;
|
2024-07-28 18:31:01 -06:00
|
|
|
default: return usage(argv[0]);
|
2023-12-24 17:13:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-28 18:31:01 -06:00
|
|
|
if (optind + 2 /* ref cmp */ > argc) { return usage(argv[0]); }
|
2024-07-12 15:54:30 -06:00
|
|
|
|
2023-12-24 17:13:17 -07:00
|
|
|
i = optind;
|
|
|
|
|
2024-07-13 17:01:53 -06:00
|
|
|
do {
|
2024-07-12 15:54:30 -06:00
|
|
|
r = c;
|
2023-12-24 17:13:17 -07:00
|
|
|
c = strtol(argv[i], &argv[i], 10);
|
2024-07-12 15:54:30 -06:00
|
|
|
|
|
|
|
if (*argv[i] != '\0' || errno != 0) {
|
|
|
|
fprintf(
|
2024-07-25 21:17:30 -06:00
|
|
|
stderr,
|
|
|
|
"%s: argument #%d: Invalid integer\n",
|
2024-07-28 18:31:01 -06:00
|
|
|
argv[0],
|
2024-07-25 21:17:30 -06:00
|
|
|
(int)i
|
2024-07-12 15:54:30 -06:00
|
|
|
);
|
2023-12-24 17:13:17 -07:00
|
|
|
return EX_USAGE;
|
|
|
|
}
|
|
|
|
|
2024-07-12 15:54:30 -06:00
|
|
|
if (i == optind) { continue; }
|
2023-12-24 17:13:17 -07:00
|
|
|
|
|
|
|
/* rule enforcement; if a mode isn't permitted and the numbers
|
|
|
|
* correspond to it, return 1 */
|
2024-07-12 15:54:30 -06:00
|
|
|
if ( (!(mode & EQUAL) && r == c)
|
|
|
|
|| (!(mode & GREATER) && r > c)
|
|
|
|
|| (!(mode & LESSER) && r < c)
|
|
|
|
) { return 1; }
|
2024-07-13 17:01:53 -06:00
|
|
|
} while (++i < argc);
|
2023-12-24 17:13:17 -07:00
|
|
|
|
2024-07-12 15:54:30 -06:00
|
|
|
return EX_OK;
|
2023-12-24 17:13:17 -07:00
|
|
|
}
|