public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: "Dan Tsafrir" <dan.tsafrir@gmail.com>
To: cygwin@cygwin.com
Subject: bug in cygwin's gcc/g++ getopt_long(): erroneous "ambiguous option"
Date: Tue, 13 Jan 2009 06:14:00 -0000	[thread overview]
Message-ID: <b2c39fcb0901122149j1f3057adhaa600ea21fe7156c@mail.gmail.com> (raw)

Hi,

When given certain long options, Cygwin's getopt_long() function
erroneously fails on "ambiguous option". The short program below
illustrates the problem: getopt_long() wrongfully reports the "--xy"
option as ambiguous.

Strangely, if flipping the order of the 2nd and 3rd entries in the
options-array (by compiling the program with -DNO_BUG) the bug goes
away, clearly demonstrating inconsistent Cygwin/getopt_long behavior.

The bug is triggered when compiling the program with Cygwin's default
gcc/g++ (3.4.4) or with gcc-4/g++-4 (4.3.2). The bug does not occur
when compiling the program with gcc/g++ (ver 3 or 4) on other OSes
(e.g., Linux).

A fix would be appreciated.
Best,
    --Dan

// start program:

#include <stdio.h>
#include <getopt.h>

const struct option lopts[] = {
#ifdef NO_BUG
    {"xy1" , required_argument, NULL, 'a' },
    {"xy"  , required_argument, NULL, 'b' },
    {"xy2" , required_argument, NULL, 'c' },
    {NULL  , 0                , NULL,  0  },
#else /* swapping order of 2nd and 3rd entries generates the bug */
    {"xy1" , required_argument, NULL, 'a' },
    {"xy2" , required_argument, NULL, 'c' },
    {"xy"  , required_argument, NULL, 'b' },
    {NULL  , 0                , NULL,  0  },
#endif
};

int main()
{
    const char* argv[] = {"./a", "--xy=1", NULL};
    int argc = 2;
    int index, c;

    // if NO_BUG defined, will print: "got opt=b" [OK]
    // otherwise: "a: ambiguous option -- xy" [BUG]
    if( (c = getopt_long(argc, (char**)argv, "A:B:C", lopts, &index)) != EOF )
        printf("got opt=%c\n", c);

    return 0;
}

// end program

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

             reply	other threads:[~2009-01-13  5:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-13  6:14 Dan Tsafrir [this message]
2009-01-13 13:06 ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b2c39fcb0901122149j1f3057adhaa600ea21fe7156c@mail.gmail.com \
    --to=dan.tsafrir@gmail.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).