diff --git a/src/str.c b/src/str.c index 73bb911..d6a89c3 100644 --- a/src/str.c +++ b/src/str.c @@ -20,11 +20,11 @@ #include #include /* NULL */ #include /* fprintf(3) */ -#include /* EXIT_FAILURE */ +#include /* size_t, EXIT_FAILURE */ #include /* strcmp(3) */ -#include +#include /* 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; }