This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug libc/11039] New: getopt mishandles optstring of "+:"
- From: "ebb9 at byu dot net" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sources dot redhat dot com
- Date: 1 Dec 2009 16:27:51 -0000
- Subject: [Bug libc/11039] New: getopt mishandles optstring of "+:"
- Reply-to: sourceware-bugzilla at sourceware dot org
According to the getopt man pages, for example:
http://www.kernel.org/doc/man-pages/online/pages/man3/getopt.3.html
If the first character (following any optional '+' or '-' described above) of
optstring is a colon (':'), then getopt() returns ':' instead of '?' to indicate
a missing option argument.
However, the code in getopt.c does not consistently follow this rule.
$ cat foo.c
#include <unistd.h>
#include <stdio.h>
int
main (int argc, char **argv)
{
int c = getopt (argc, argv, "+:a:b");
if (c == -1)
puts ("got -1");
else
printf ("got %c\n", c);
c = getopt (argc, argv, "+:a:b");
if (c == -1)
puts ("got -1");
else
printf ("got %c\n", c);
return 0;
}
$ ./foo -a
./foo2: option requires an argument -- a
got :
got -1
$ ./foo -b -a
got b
./foo2: option requires an argument -- a
got ?
Expected behavior - stderr should be silent, and the -a without argument should
return ':', not '?'.
Workaround: code can set opterr=0 before the first getopt{,_long} call (to
silence the spurious warning if argv[1] is missing an argument), then leave off
the leading '-' or '+' of opstring for all subsequent calls for a given
argc/argv parse (to silence the warning and return ':' instead of '?' if any
subsequent argv is missing an argument).
--
Summary: getopt mishandles optstring of "+:"
Product: glibc
Version: 2.11
Status: NEW
Severity: normal
Priority: P2
Component: libc
AssignedTo: drepper at redhat dot com
ReportedBy: ebb9 at byu dot net
CC: glibc-bugs at sources dot redhat dot com
http://sourceware.org/bugzilla/show_bug.cgi?id=11039
------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.