Debian Bug report logs - #352139
getopt optional arg does not work as documented

version graph

Package: manpages-dev; Maintainer for manpages-dev is Martin Schulze <joey@debian.org>; Source for manpages-dev is src:manpages.

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

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to debian-bugs-dist@lists.debian.org, GNU Libc Maintainers <debian-glibc@lists.debian.org>:
Bug#352139; Package glibc. Full text and rfc822 format available.

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 and rfc822 format available.

Message #5 received at submit@bugs.debian.org (full text, mbox):

From: Troy Heber <troyh@debian.org>
To: submit@bugs.debian.org
Subject: getopt optional arg does not work as documented
Date: Thu, 9 Feb 2006 16:35:59 -0700
[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 and rfc822 format available.

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 and rfc822 format available.

Message #10 received at 352139@bugs.debian.org (full text, mbox):

From: Helmut Grohne <helmut@subdivi.de>
To: 352139-submitter@bugs.debian.org
Cc: 352139@bugs.debian.org, control@bugs.debian.org
Subject: Re: getopt optional arg does not work as documented
Date: Tue, 27 Feb 2007 10:59:19 +0100
[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 and rfc822 format available.

Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139. Full text and rfc822 format available.

Information forwarded to debian-bugs-dist@lists.debian.org, GNU Libc Maintainers <debian-glibc@lists.debian.org>:
Bug#352139; Package glibc. Full text and rfc822 format available.

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 and rfc822 format available.

Message #20 received at 352139@bugs.debian.org (full text, mbox):

From: Helmut Grohne <helmut@subdivi.de>
To: 352139-submitter@bugs.debian.org
Cc: 352139@bugs.debian.org, control@bugs.debian.org
Subject: Re: getopt optional arg does not work as documented
Date: Tue, 6 Mar 2007 13:32:32 +0100
[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 and rfc822 format available.

Bug reassigned from package `glibc' to `manpages-dev'. Request was from Helmut Grohne <helmut@subdivi.de> to control@bugs.debian.org. Full text and rfc822 format available.

Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139. Full text and rfc822 format available.

Information forwarded to debian-bugs-dist@lists.debian.org, Martin Schulze <joey@debian.org>:
Bug#352139; Package manpages-dev. Full text and rfc822 format available.

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 and rfc822 format available.

Message #32 received at 352139@bugs.debian.org (full text, mbox):

From: Michael Kerrisk <mtk-manpages@gmx.net>
To: Helmut Grohne <helmut@subdivi.de>
Cc: 352139-submitter@bugs.debian.org, 352139@bugs.debian.org, control@bugs.debian.org, 352139-quiet@bugs.debian.org
Subject: Re: Bug#352139: getopt optional arg does not work as documented
Date: Sun, 18 Mar 2007 07:42:28 +0100
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 and rfc822 format available.

Information stored:
Bug#352139; Package manpages-dev. Full text and rfc822 format available.

Acknowledgement sent to Michael Kerrisk <mtk-manpages@gmx.net>:
Extra info received and filed, but not forwarded. Full text and rfc822 format available.

Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139. Full text and rfc822 format available.

Information forwarded to debian-bugs-dist@lists.debian.org, Martin Schulze <joey@debian.org>:
Bug#352139; Package manpages-dev. Full text and rfc822 format available.

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 and rfc822 format available.

Message #47 received at 352139@bugs.debian.org (full text, mbox):

From: Michael Kerrisk <mtk-manpages@gmx.net>
To: 352139@bugs.debian.org
Cc: Helmut Grohne <helmut@subdivi.de>, 352139-submitter@bugs.debian.org, 352139-quiet@bugs.debian.org
Subject: Re: Bug#352139: getopt optional arg does not work as documented
Date: Sun, 18 Mar 2007 07:44:03 +0100
I should also have mentioned that the fix will be in upstream 2.44.



Information stored:
Bug#352139; Package manpages-dev. Full text and rfc822 format available.

Acknowledgement sent to Michael Kerrisk <mtk-manpages@gmx.net>:
Extra info received and filed, but not forwarded. Full text and rfc822 format available.

Message sent on to Troy Heber <troyh@debian.org>:
Bug#352139. Full text and rfc822 format available.

Reply sent to Florian Ernst <florian_ernst@gmx.net>:
You have taken responsibility. Full text and rfc822 format available.

Notification sent to Troy Heber <troyh@debian.org>:
Bug acknowledged by developer. Full text and rfc822 format available.

Message #60 received at 352139-done@bugs.debian.org (full text, mbox):

From: Florian Ernst <florian_ernst@gmx.net>
To: 352139-done@bugs.debian.org
Subject: Re: Bug#352139: getopt optional arg does not work as documented
Date: Thu, 14 Jun 2007 18:56:33 +0200
[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 and rfc822 format available.

Send a report that this bug log contains spam.


Debian bug tracking system administrator <owner@bugs.debian.org>. Last modified: Fri Apr 18 22:03:22 2014; Machine Name: buxtehude.debian.org

Debian Bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.