str(1): fix argv parsing and add comments
This commit is contained in:
parent
199d48d85b
commit
cba8394d95
16
src/str.c
16
src/str.c
@ -51,11 +51,10 @@ int usage(char *s){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int main(int argc, char *argv[]){
|
||||||
size_t ctype; /* selected from ctypes.h; index of ctype */
|
size_t ctype; // selected from ctypes.h; index of ctype
|
||||||
int retval;
|
int retval; // initially fail but becomes success on the first valid char
|
||||||
char *s = (argv[0] == NULL ? program_name : argv[0]);
|
|
||||||
|
|
||||||
if (argc < 3) { return usage(s); }
|
if (argc < 3) { return usage(argv[0] == NULL ? program_name : argv[0]); }
|
||||||
|
|
||||||
for ( /* iterate ctypes */
|
for ( /* iterate ctypes */
|
||||||
ctype = 0;
|
ctype = 0;
|
||||||
@ -64,15 +63,16 @@ int main(int argc, char *argv[]){
|
|||||||
++ctype
|
++ctype
|
||||||
);
|
);
|
||||||
|
|
||||||
if (ctypes[ctype].f == NULL) { return usage(s); }
|
if (ctypes[ctype].f == NULL) { return usage(argv[0]); }
|
||||||
|
|
||||||
/* iterate args */
|
/* iterate args */
|
||||||
for (argv += 2, retval = EXIT_FAILURE; *argv != NULL; ++argv) {
|
for (argv += 2, retval = EXIT_FAILURE; *argv != NULL; ++argv) {
|
||||||
for (size_t i = 0; s[i] != '\0'; ++i) { /* iterate arg bytes */
|
for (size_t i = 0; argv[0][i] != '\0'; ++i) { /* iterate arg bytes */
|
||||||
/* First checks if s[i] is valid ASCII; ctypes(3) don't
|
/* First checks if argv[0][i] is valid ASCII; ctypes(3) don't
|
||||||
* handle non-ASCII. This is bad. */
|
* handle non-ASCII. This is bad. */
|
||||||
if(
|
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; }
|
) { return EXIT_FAILURE; }
|
||||||
else { retval = EXIT_SUCCESS; }
|
else { retval = EXIT_SUCCESS; }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user