Give an error if -p or -a is used with an undefined option letter

This commit is contained in:
Eric Fischer
2016-01-11 10:47:06 -08:00
parent 502d269143
commit 404bf69847
3 changed files with 57 additions and 11 deletions
+22 -2
View File
@@ -1826,6 +1826,16 @@ int read_json(int argc, char **argv, char *fname, const char *layername, int max
return ret;
}
int int_in(int v, int *a, int len) {
for (int i = 0; i < len; i++) {
if (a[i] == v) {
return 1;
}
}
return 0;
}
int main(int argc, char **argv) {
#ifdef MTRACE
mtrace();
@@ -1961,14 +1971,24 @@ int main(int argc, char **argv) {
case 'p': {
char *cp;
for (cp = optarg; *cp != '\0'; cp++) {
prevent[*cp & 0xFF] = 1;
if (int_in(*cp, prevent_options, sizeof(prevent_options) / sizeof(prevent_options[0]))) {
prevent[*cp & 0xFF] = 1;
} else {
fprintf(stderr, "%s: Unknown option -p%c\n", argv[0], *cp);
exit(EXIT_FAILURE);
}
}
} break;
case 'a': {
char *cp;
for (cp = optarg; *cp != '\0'; cp++) {
additional[*cp & 0xFF] = 1;
if (int_in(*cp, additional_options, sizeof(additional_options) / sizeof(additional_options[0]))) {
additional[*cp & 0xFF] = 1;
} else {
fprintf(stderr, "%s: Unknown option -a%c\n", argv[0], *cp);
exit(EXIT_FAILURE);
}
}
} break;