From cba8394d95a76c75801681b929640788ee9f90b3 Mon Sep 17 00:00:00 2001 From: DTB Date: Sat, 27 Jul 2024 18:18:29 -0600 Subject: [PATCH] str(1): fix argv parsing and add comments --- src/str.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/str.c b/src/str.c index 646e4c7..e503470 100644 --- a/src/str.c +++ b/src/str.c @@ -51,11 +51,10 @@ int usage(char *s){ } int main(int argc, char *argv[]){ - size_t ctype; /* selected from ctypes.h; index of ctype */ - int retval; - char *s = (argv[0] == NULL ? program_name : argv[0]); + size_t ctype; // selected from ctypes.h; index of ctype + int retval; // initially fail but becomes success on the first valid char - if (argc < 3) { return usage(s); } + if (argc < 3) { return usage(argv[0] == NULL ? program_name : argv[0]); } for ( /* iterate ctypes */ ctype = 0; @@ -64,15 +63,16 @@ int main(int argc, char *argv[]){ ++ctype ); - if (ctypes[ctype].f == NULL) { return usage(s); } + if (ctypes[ctype].f == NULL) { return usage(argv[0]); } /* iterate args */ for (argv += 2, retval = EXIT_FAILURE; *argv != NULL; ++argv) { - for (size_t i = 0; s[i] != '\0'; ++i) { /* iterate arg bytes */ - /* First checks if s[i] is valid ASCII; ctypes(3) don't + 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)s[i] < 0x80 && !ctypes[ctype].f(s[i]) + (unsigned char)argv[0][i] < 0x80 // argv[0][i] is ASCII, + && !ctypes[ctype].f(argv[0][i]) // so use ctypes(3) ) { return EXIT_FAILURE; } else { retval = EXIT_SUCCESS; } }