intcmp(1): formatting, removed gotos

This commit is contained in:
Emma Tebibyte 2024-07-12 15:54:30 -06:00
parent acc3cf3e90
commit 9cfc48c960
Signed by: emma
GPG Key ID: 06FA419A1698C270

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2023 DTB <trinity@trinity.moe> * Copyright (c) 2023 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,16 +23,24 @@
#include <unistd.h> /* getopt(3), optind */ #include <unistd.h> /* getopt(3), optind */
#include <sysexits.h> #include <sysexits.h>
/* 0b00? */ /* Equal | -e | 0b001 | 1 */ /* 0b00? */ /* Equal | -e | 0b001 | 1 */
#define EQUAL 0x01 /* Greater | -g | 0b010 | 2 */ #define EQUAL 0x01 /* Greater | -g | 0b010 | 2 */
/* 0b0?0 */ /* Greater or Equal | -ge | 0b011 | 3 */ /* 0b0?0 */ /* Greater or Equal | -ge | 0b011 | 3 */
#define GREATER 0x02 /* Less | -l | 0b100 | 4 */ #define GREATER 0x02 /* Lesser | -l | 0b100 | 4 */
/* 0b?00 */ /* Less or Equal | -le | 0b101 | 5 */ /* 0b?00 */ /* Lesser or Equal | -le | 0b101 | 5 */
#define LESS 0x04 /* Inequal (Greater or Less) | -gl | 0b110 | 6 */ #define LESSER 0x04 /* Inequal (Greater or Lesser) | -gl | 0b110 | 6 */
static char *program_name = "intcmp"; static char *program_name = "intcmp";
int main(int argc, char *argv[]){ int usage(char *s) {
fprintf(
stderr, "Usage: %s [-egl] integer integer...\n",
s == NULL ? program_name : s
);
return EX_USAGE;
}
int main(int argc, char *argv[]) {
int c; int c;
size_t i; size_t i;
unsigned char mode; unsigned char mode;
@ -39,44 +48,41 @@ int main(int argc, char *argv[]){
mode = 0; mode = 0;
if(argc < 3) if(argc < 3) { return usage(argv[0]); }
goto usage;
while((c = getopt(argc, argv, "egl")) != -1) while ((c = getopt(argc, argv, "egl")) != -1) {
switch(c){ switch(c){
case 'e': mode |= EQUAL; break; case 'e': mode |= EQUAL; break;
case 'g': mode |= GREATER; break; case 'g': mode |= GREATER; break;
case 'l': mode |= LESS; break; case 'l': mode |= LESSER; break;
default: goto usage; default: return usage(argv[0]);
} }
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;
} }
if(optind + 2 /* ref cmp */ > argc){ return usage(argv[0]); }
i = optind; i = optind;
do{ r = c; do{
r = c;
c = strtol(argv[i], &argv[i], 10); c = strtol(argv[i], &argv[i], 10);
if(*argv[i] != '\0' || errno != 0){
fprintf(stderr, "%s: argument #%d: Invalid integer\n", if (*argv[i] != '\0' || errno != 0) {
argv[0], (int)i); fprintf(
stderr, "%s: argument #%d: Invalid integer\n", argv[0], (int)i
);
return EX_USAGE; return EX_USAGE;
} }
if(i == optind) if (i == optind) { continue; }
continue;
/* rule enforcement; if a mode isn't permitted and the numbers /* rule enforcement; if a mode isn't permitted and the numbers
* correspond to it, return 1 */ * correspond to it, return 1 */
if( (!(mode & EQUAL) && r == c) if ( (!(mode & EQUAL) && r == c)
|| (!(mode & GREATER) && r > c) || (!(mode & GREATER) && r > c)
|| (!(mode & LESS) && r < c)) || (!(mode & LESSER) && r < c)
return 1; ) { return 1; }
}while(++i < argc); } while(++i < argc);
return 0; return EX_OK;
} }