Debian Bug report logs -
#352139
getopt optional arg does not work as documented
Reported by: Troy Heber <troyh@debian.org>
Date: Fri, 10 Feb 2006 00:03:01 UTC
Severity: normal
Tags: fixed-upstream
Fixed in version manpages/2.44-1
Done: Florian Ernst <florian_ernst@gmx.net>
Bug is archived. No further changes may be made.
Toggle useless messages
Report forwarded to debian-bugs-dist@lists.debian.org, GNU Libc Maintainers <debian-glibc@lists.debian.org>:
Bug#352139; Package glibc.
(full text, mbox, link).
Acknowledgement sent to Troy Heber <troyh@debian.org>:
New Bug report received and forwarded. Copy sent to GNU Libc Maintainers <debian-glibc@lists.debian.org>.
(full text, mbox, link).
Message #5 received at submit@bugs.debian.org (full text, mbox, reply):
[Message part 1 (text/plain, inline)]
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
[Message part 2 (application/pgp-signature, inline)]
Information forwarded to debian-bugs-dist@lists.debian.org, GNU Libc Maintainers <debian-glibc@lists.debian.org>:
Bug#352139; Package glibc.
(full text, mbox, link).
Acknowledgement sent to Helmut Grohne <helmut@subdivi.de>:
Extra info received and forwarded to list. Copy sent to GNU Libc Maintainers <debian-glibc@lists.debian.org>.
(full text, mbox, link).
Message #10 received at 352139@bugs.debian.org (full text, mbox, reply):
[Message part 1 (text/plain, inline)]
tag 352139 wontfix
thanks
> However, this does not seem to be the case.
Actually it works quite similar.
> When run:
> troyh@me:/tmp$ a.out -a
> a arg is: (null)
Correct behaviour.
$ ./a.out -afoo
a arg is: foo
> troyh@me:/tmp$ a.out -a hello
> a arg is: (null)
Now. This case is difficult. The string "hello" could also be just a
normal filename argument. I think the glibc handles this case correctly
and thus mark the bug as wontfix.
Helmut
[signature.asc (application/pgp-signature, inline)]
Tags added: wontfix
Request was from Helmut Grohne <helmut@subdivi.de>
to control@bugs.debian.org.
(full text, mbox, link).
Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139.
(full text, mbox, link).
Information forwarded to debian-bugs-dist@lists.debian.org, GNU Libc Maintainers <debian-glibc@lists.debian.org>:
Bug#352139; Package glibc.
(full text, mbox, link).
Acknowledgement sent to Helmut Grohne <helmut@subdivi.de>:
Extra info received and forwarded to list. Copy sent to GNU Libc Maintainers <debian-glibc@lists.debian.org>.
(full text, mbox, link).
Message #20 received at 352139@bugs.debian.org (full text, mbox, reply):
[Message part 1 (text/plain, inline)]
tag 352139 -wontfix
reassign 352139 manpages-dev
thanks
> Now. This case is difficult. The string "hello" could also be just a
> normal filename argument. I think the glibc handles this case correctly
> and thus mark the bug as wontfix.
Actually it would be even better if the documentation could be adapted.
Thanks to Aurelien Jarno for pointing this out.
Helmut Grohne
[signature.asc (application/pgp-signature, inline)]
Tags removed: wontfix
Request was from Helmut Grohne <helmut@subdivi.de>
to control@bugs.debian.org.
(full text, mbox, link).
Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139.
(full text, mbox, link).
Information forwarded to debian-bugs-dist@lists.debian.org, Martin Schulze <joey@debian.org>:
Bug#352139; Package manpages-dev.
(full text, mbox, link).
Acknowledgement sent to Michael Kerrisk <mtk-manpages@gmx.net>:
Extra info received and forwarded to list. Copy sent to Martin Schulze <joey@debian.org>.
(full text, mbox, link).
Message #32 received at 352139@bugs.debian.org (full text, mbox, reply):
tags 352139 fixed-upstream
thanks
Helmut Grohne wrote:
> tag 352139 -wontfix
> reassign 352139 manpages-dev
> thanks
>
>> Now. This case is difficult. The string "hello" could also be just a
>> normal filename argument. I think the glibc handles this case correctly
>> and thus mark the bug as wontfix.
>
> Actually it would be even better if the documentation could be adapted.
> Thanks to Aurelien Jarno for pointing this out.
>
> Helmut Grohne
Gidday from the upstream man-pages maintainer...
The upstream man page says:
Two colons mean an option takes an
optional arg; if there is text in the current argv-ele-
ment, it is returned in optarg, otherwise optarg is set
to zero.
This seems reasonably clear to me: "the *current* argv-element". So the
man page seems to me to clearly say that /-afoo/ will work, but /-a foo/
will not, because in the latter case "foo" is not in the "current" argv
element.
Nevertheless, I've added a few words to make the point crystal clear:
Two colons mean an option takes an
optional arg; if there is text in the current argv-ele-
ment (i.e., in the same word as the option name itself,
for example, "-oarg"), then it is returned in optarg,
otherwise optarg is set to zero.
Cheers,
Michael
--
Michael Kerrisk
maintainer of Linux man pages Sections 2, 3, 4, 5, and 7
Want to help with man page maintenance? Grab the latest tarball at
http://www.kernel.org/pub/linux/docs/manpages/
read the HOWTOHELP file and grep the source files for 'FIXME'.
Tags added: fixed-upstream
Request was from Michael Kerrisk <mtk-manpages@gmx.net>
to control@bugs.debian.org.
(Sun, 18 Mar 2007 06:45:03 GMT) (full text, mbox, link).
Acknowledgement sent to Michael Kerrisk <mtk-manpages@gmx.net>:
Extra info received and filed, but not forwarded.
(full text, mbox, link).
Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139.
(full text, mbox, link).
Information forwarded to debian-bugs-dist@lists.debian.org, Martin Schulze <joey@debian.org>:
Bug#352139; Package manpages-dev.
(full text, mbox, link).
Acknowledgement sent to Michael Kerrisk <mtk-manpages@gmx.net>:
Extra info received and forwarded to list. Copy sent to Martin Schulze <joey@debian.org>.
(full text, mbox, link).
Message #47 received at 352139@bugs.debian.org (full text, mbox, reply):
I should also have mentioned that the fix will be in upstream 2.44.
Acknowledgement sent to Michael Kerrisk <mtk-manpages@gmx.net>:
Extra info received and filed, but not forwarded.
(full text, mbox, link).
Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139.
(full text, mbox, link).
Reply sent to Florian Ernst <florian_ernst@gmx.net>:
You have taken responsibility.
(full text, mbox, link).
Notification sent to Troy Heber <troyh@debian.org>:
Bug acknowledged by developer.
(full text, mbox, link).
Message #60 received at 352139-done@bugs.debian.org (full text, mbox, reply):
[Message part 1 (text/plain, inline)]
Package: manpages-dev
Version: 2.44-1
Hello Troy, hello Joey,
On Sun, Mar 18, 2007 at 07:42:28AM +0100, Michael Kerrisk wrote:
> Helmut Grohne wrote:
> > tag 352139 -wontfix
> > reassign 352139 manpages-dev
> > thanks
> >
> >> Now. This case is difficult. The string "hello" could also be just a
> >> normal filename argument. I think the glibc handles this case correctly
> >> and thus mark the bug as wontfix.
> >
> > Actually it would be even better if the documentation could be adapted.
> > Thanks to Aurelien Jarno for pointing this out.
> >
> > Helmut Grohne
>
> Gidday from the upstream man-pages maintainer...
>
> The upstream man page says:
>
> Two colons mean an option takes an
> optional arg; if there is text in the current argv-ele-
> ment, it is returned in optarg, otherwise optarg is set
> to zero.
>
> This seems reasonably clear to me: "the *current* argv-element". So the
> man page seems to me to clearly say that /-afoo/ will work, but /-a foo/
> will not, because in the latter case "foo" is not in the "current" argv
> element.
>
> Nevertheless, I've added a few words to make the point crystal clear:
>
> Two colons mean an option takes an
> optional arg; if there is text in the current argv-ele-
> ment (i.e., in the same word as the option name itself,
> for example, "-oarg"), then it is returned in optarg,
> otherwise optarg is set to zero.
Confirmed for upstream version 2.44, so closing this bug now. For
reference please see
<http://cvs.infodrom.org/manpages/man3/getopt.3.diff?r1=1.1.1.14;r2=1.1.1.15;cvsroot=debian>.
Cheers,
Flo
[signature.asc (application/pgp-signature, inline)]
Bug archived.
Request was from Debbugs Internal Request <owner@bugs.debian.org>
to internal_control@bugs.debian.org.
(Fri, 13 Jul 2007 07:44:29 GMT) (full text, mbox, link).
Send a report that this bug log contains spam.
Debian bug tracking system administrator <owner@bugs.debian.org>.
Last modified:
Thu Mar 9 10:03:19 2023;
Machine Name:
bembo
Debian Bug tracking system
Debbugs is free software and licensed under the terms of the GNU
Public License version 2. The current version can be obtained
from https://bugs.debian.org/debbugs-source/.
Copyright © 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson,
2005-2017 Don Armstrong, and many other contributors.