G
Gregor H.
In K&R (2nd edition) the authors published the following code for
dealing with command line arguments (p. 117):
while (--argc > 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
But this solution raised some criticism:
"The sample program increments argv[0]. Although argv is
modifiable, and argv[0][0] is modifiable (if argv[0] is
not a null pointer), argv[0] is not modifiable. (At least,
not always.)"
(Peter Seebacher, http://www.plethora.net/~seebs/c/knr.html)
"p.117 (§5.10): In the find example, the program increments
argv[0]. This is not specifically forbidden, but not
specifically allowed either."
(Source:
Errata for The C Programming Language, Second Edition
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)
Now my question is: would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:
while (--argc > 0 && (*++argv)[0] == '-') {
char *arg = *argv;
while (c = *++arg)
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
}
G.
dealing with command line arguments (p. 117):
while (--argc > 0 && (*++argv)[0] == '-')
while (c = *++argv[0])
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
But this solution raised some criticism:
"The sample program increments argv[0]. Although argv is
modifiable, and argv[0][0] is modifiable (if argv[0] is
not a null pointer), argv[0] is not modifiable. (At least,
not always.)"
(Peter Seebacher, http://www.plethora.net/~seebs/c/knr.html)
"p.117 (§5.10): In the find example, the program increments
argv[0]. This is not specifically forbidden, but not
specifically allowed either."
(Source:
Errata for The C Programming Language, Second Edition
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html)
Now my question is: would the following (slight modification of the
original code) be a reasonable (or at least correct) solution?:
while (--argc > 0 && (*++argv)[0] == '-') {
char *arg = *argv;
while (c = *++arg)
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
}
G.