Bug#192834: Bug 192834 - Glibc's method of resetting getopt different, causes interoperability problems
Seems to be fixed (in Lenny at least)
$ dpkg -l libc6
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name Version Description
+++-==============-==============-============================================
ii libc6 2.7-18 GNU C Library: Shared libraries
$ ./a.out
optind before loop 1
Got a optind=2 optopt=0 opterr=1
got b optind=4 optopt=0 opterr=1 optarg=first
optind after loop 4
optind before loop 1
Got a optind=2 optopt=0 opterr=1
got b optind=4 optopt=0 opterr=1 optarg=second
optind after loop 4
#include <stdio.h>
#include <unistd.h>
void test (int argc, char **argv) {
int ch;
optind = 1;
printf ("optind before loop %d\n", optind);
while ((ch = getopt (argc, argv, "ab:")) != -1) {
switch (ch) {
case 'a':
printf ("Got a optind=%d optopt=%d opterr=%d\n",
optind, optopt, opterr);
break;
case 'b':
printf ("got b optind=%d optopt=%d opterr=%d "
"optarg=%s\n", optind, optopt, opterr,
optarg);
break;
default:
printf ("bad ch=%d optind=%d optopt=%d opterr=%d\n",
ch, optind, optopt, opterr);
}
}
printf ("optind after loop %d\n", optind);
}
#define LEN(arr) ((sizeof arr) / sizeof arr[0])
char *first[] = { "optind", "-a", "-b", "first", };
char *second [] = { "optind", "-a", "-b", "second", };
int main (int argc, char **argv) {
test (LEN(first), first);
test (LEN(second), second);
return 0;
}
Reply to: