proper way to fix reveng getopt bug

This commit is contained in:
Philippe Teuwen 2020-05-07 00:23:19 +02:00
commit 2fcad93efc

View file

@ -97,8 +97,10 @@ int reveng_main(int argc, char *argv[]) {
SETBMP(); SETBMP();
// pos=0; --- not in this ver of getopt // pos=0; --- not in this ver of getopt
int err = 0;
optind = 1; optind = 1;
optreset = 1; // Remember to consume always all the option string till getopt returns -1 !
// else next invocations will be corrupted
do { do {
c = getopt(argc, argv, "?A:BDFGLMP:SVXa:bcdefhi:k:lm:p:q:rstuvw:x:yz"); c = getopt(argc, argv, "?A:BDFGLMP:SVXa:bcdefhi:k:lm:p:q:rstuvw:x:yz");
switch (c) { switch (c) {
@ -106,8 +108,8 @@ int reveng_main(int argc, char *argv[]) {
case 'a': /* a: bits per character */ case 'a': /* a: bits per character */
if ((obperhx = atoi(optarg)) > BMP_BIT) { if ((obperhx = atoi(optarg)) > BMP_BIT) {
fprintf(stderr, "%s: argument to -%c must be between 1 and %d\n", myname, c, BMP_BIT); fprintf(stderr, "%s: argument to -%c must be between 1 and %d\n", myname, c, BMP_BIT);
return 0; err = 1;
//exit(EXIT_FAILURE); break;
} }
if (c == 'a') ibperhx = obperhx; if (c == 'a') ibperhx = obperhx;
break; break;
@ -131,8 +133,8 @@ int reveng_main(int argc, char *argv[]) {
case 'v': /* v calculate reversed CRC */ case 'v': /* v calculate reversed CRC */
if (mode) { if (mode) {
fprintf(stderr, "%s: more than one mode switch specified. Use %s -h for help.\n", myname, myname); fprintf(stderr, "%s: more than one mode switch specified. Use %s -h for help.\n", myname, myname);
return 0; err = 1;
//exit(EXIT_FAILURE); break;
} }
mode = c; mode = c;
break; break;
@ -152,8 +154,7 @@ int reveng_main(int argc, char *argv[]) {
case '?': /* ? get help / usage */ case '?': /* ? get help / usage */
default: default:
usage(); usage();
return 0; err = 1;
//exit(EXIT_FAILURE);
break; break;
case 'i': /* i: Init value */ case 'i': /* i: Init value */
pptr = &model.init; pptr = &model.init;
@ -185,12 +186,13 @@ int reveng_main(int argc, char *argv[]) {
case 'm': /* m: select preset CRC model */ case 'm': /* m: select preset CRC model */
if (!(c = mbynam(&model, optarg))) { if (!(c = mbynam(&model, optarg))) {
fprintf(stderr, "%s: preset model '%s' not found. Use %s -D to list presets.\n", myname, optarg, myname); fprintf(stderr, "%s: preset model '%s' not found. Use %s -D to list presets.\n", myname, optarg, myname);
return 0; err = 1;
//exit(EXIT_FAILURE); break;
} }
if (c < 0) { if (c < 0) {
uerror("no preset models available"); uerror("no preset models available");
return 0; err = 1;
break;
} }
/* must set width so that parameter to -ipx is not zeroed */ /* must set width so that parameter to -ipx is not zeroed */
width = plen(model.spoly); width = plen(model.spoly);
@ -247,6 +249,9 @@ ipqx:
} }
} while (c != -1); } while (c != -1);
if (err)
return 0;
/* canonicalise the model, so the one we dump is the one we /* canonicalise the model, so the one we dump is the one we
* calculate with (not with -s, spoly may be blank which will * calculate with (not with -s, spoly may be blank which will
* normalise to zero and clear init and xorout.) * normalise to zero and clear init and xorout.)