1
0
forked from bonsai/harakit

dj(1), mm(1), npc(1), scrut(1), str(1): consistent argv[0] handling

This commit is contained in:
2024-07-20 07:18:59 -06:00
parent f96ed9c1f3
commit 0282b60e65
5 changed files with 29 additions and 22 deletions

View File

@@ -53,8 +53,9 @@ 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]);
if (argc < 3) { return usage(argv[0] == NULL ? program_name : argv[0]); }
if (argc < 3) { return usage(s); }
for ( /* iterate ctypes */
ctype = 0;
@@ -63,15 +64,15 @@ int main(int argc, char *argv[]){
++ctype
);
if (ctypes[ctype].f == NULL) { return usage(argv[0]); }
if (ctypes[ctype].f == NULL) { return usage(s); }
/* 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
for (size_t i = 0; s[i] != '\0'; ++i) { /* iterate arg bytes */
/* First checks if s[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])
(unsigned char)s[i] < 0x80 && !ctypes[ctype].f(s[i])
) { return EXIT_FAILURE; }
else { retval = EXIT_SUCCESS; }
}