Package: glibc
Severity: normal
On sid with libc 2.3.5-13 the getopt man page states:
Two colons mean an option takes an optional arg; if there is text in
the current argv-element, it is returned in optarg, otherwise optarg
is set to zero. This is a GNU extension.
However, this does not seem to be the case.
tst1.c
-------------------------------------
int
main (int argc, char **argv)
{
int a = 0;
int opt;
opterr = 0;
while ((opt = getopt (argc, argv, "a::")) != -1)
switch (opt)
{
case 'a':
a = 1;
printf("a arg is: %s\n", optarg);
break;
default:
printf("bad arg\n");
exit(1);
}
return 0;
}
When run:
troyh@me:/tmp$ a.out -a
a arg is: (null)
troyh@me:/tmp$ a.out -a hello
a arg is: (null)
According to the man page, I would expect to see the second example to
output hello.
I have been able to get it to work like one would expect, by using
argv[optind] instead of optarg, i.e.:
printf("a arg is: %s\n", argv[optind]);
When run with the change:
troyh@me:/tmp$ a.out -a
a arg is: (null)
troyh@me:/tmp$ a.out -a hello
a arg is: hello
This behavior is specifically different that what's documented in the
man page, and in /usr/include/getopt.h. Both say the argv-element
should be in optarg not in argv. I have also compiling using
-std=gnu9x to ensure that I'm getting all of the GNU extensions, with
the same results.
Thanks,
Troy
Attachment:
pgpl541Jmt5FP.pgp
Description: PGP signature