str(1): edit out goto

This commit is contained in:
dtb 2024-07-19 19:03:23 -06:00
parent 71f4a411b6
commit c8b4f7a8b3
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -20,11 +20,11 @@
#include <ctype.h>
#include <stddef.h> /* NULL */
#include <stdio.h> /* fprintf(3) */
#include <stdlib.h> /* EXIT_FAILURE */
#include <stdlib.h> /* size_t, EXIT_FAILURE */
#include <string.h> /* strcmp(3) */
#include <sysexits.h>
#include <sysexits.h> /* EX_USAGE */
static char *program_name = "str";
char *program_name = "str";
static struct {
char *name;
@ -41,41 +41,41 @@ static struct {
{ "isprint", isprint },
{ "ispunct", ispunct },
{ "isspace", isspace },
{ "isupper", isupper }
{ "isupper", isupper },
{ NULL, NULL } /* marks end */
};
int usage(char *s){
fprintf(stderr, "Usage: %s type string...\n", s);
return EX_USAGE;
}
int main(int argc, char *argv[]){
int ctype;
int i;
int r;
size_t ctype; /* selected from ctypes.h; index of ctype */
int retval;
if (argc >= 3) {
for (ctype = 0; ctype < (sizeof ctypes) / (sizeof *ctypes); ++ctype) {
if(strcmp(argv[1], ctypes[ctype].name) == 0) {
goto pass;
}
}
}
if (argc < 3) { return usage(argv[0] == NULL ? program_name : argv[0]); }
fprintf(
stderr,
"Usage: %s type string...\n",
argv[0] == NULL ? program_name : argv[0]
for ( /* iterate ctypes */
ctype = 0;
ctypes[ctype].f != NULL /* break at the end of ctypes */
&& strcmp(argv[1], ctypes[ctype].name) != 0; /* break at match */
++ctype
);
return EX_USAGE;
if (ctypes[ctype].f == NULL) { return usage(argv[0]); }
pass: for (argv += 2, r = 1; *argv != NULL; ++argv) {
for (i = 0; argv[0][i] != '\0'; ++i) {
/* First checks if argv[0][i] is valid ASCII; ctypes(3)
* don't handle non-ASCII.
* This is bad. */
/* iterate args */
for (argv += 2, retval = EXIT_FAILURE; *argv != NULL; ++argv) {
for (size_t i = 0; argv[0][i] != '\0'; ++i) { /* iterate arg bytes */
/* First checks if argv[0][i] is valid ASCII; ctypes(3) don't
* handle non-ASCII. This is bad. */
if(
(unsigned char)argv[0][i] < 0x80 && !ctypes[ctype].f(argv[0][i])
) { return 1; }
else { r = 0; }
) { return EXIT_FAILURE; }
else { retval = EXIT_SUCCESS; }
}
}
return r;
return retval;
}