bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getopt sync


From: Eric Blake
Subject: getopt sync
Date: Mon, 21 Dec 2009 19:20:28 +0000 (UTC)
User-agent: Loom/3.14 (http://gmane.org/)

I will probably commit the following later today, unless there are complaints.  
It enhances the testsuite to ensure that some of the features of getopt_long 
required by coreutils are met by the implementation, as well as giving more 
coverage to extensions like 'W;' in optstring.  So far, I didn't find any BSD-
based implementation that failed the new tests; either they already failed the 
m4 filter so the gnulib replacement was in place, as in NetBSD and cygwin 
1.7.0; or the native version passed with flying colors, as in FreeBSD and 
cygwin 1.7.2.  However, the new tests are weaker than they could be because I'm 
waiting for the glibc list to confirm whether my recent bug reports are valid.

The last patch in the series does a partial sync from glibc to gnulib, to 
consistently quote error messages to stderr, and make parameter ordering 
consistent.  It does not use the same quoting conventions as other messages in 
gnulib ('a' vs. `a'), but by being identical to glibc, hopefully translaters 
will still make it look nice in non-C locales.  Note that it breaks 
compatibility with the ancient POSIX 1003.2 standard (1992), in that 
POSIXLY_CORRECT no longer bothers to use a literal message string that included 
the word "illegal", since GNU folks objected to it and the requirement for a 
specific wording was removed from POSIX 2001.

I see a potential further improvement: glibc now includes an interface version 
declaration (which I did not sync):

#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
# include <gnu-versions.h>
# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
#  define ELIDE_CODE
# endif

I know that gnulib's argp module expects to use the non-standard internal 
interface _getopt_long_r, and blindly replaces getopt even on glibc systems to 
guarantee that this interface will have an incompatible API change by newer 
glibc.  But now that glibc includes a versioning mechanism, perhaps we could 
improve getopt.m4 to not replace getopt on glibc systems that have 
_GNU_GETOPT_INTERFACE_VERSION==2, and thus reduce the size of applications 
using gnulib argp.


>From 875bba0c3e529c252ac3ecbea23c40d95048673b Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Tue, 1 Dec 2009 17:21:34 -0700
Subject: [PATCH 1/3] test-getopt: enhance test

Add coverage of optind==0 for getopt_long, since coreutils
depends on it.  Also test an optstring containing "W;", since
that tends to expose corner-case bugs (even in glibc, so the
test is weaker than it could be).

* m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Require that getopt_long
supports optind=0.
* tests/test-getopt.c (OPTIND_MIN): Move...
* tests/test-getopt.h (OPTIND_MIN): ...here.
* tests/test-getopt_long.h (test_getopt_long): Add more coverage.
Require that optind=0 works, since modern BSD supports it in
addition to optreset, and since coreutils expects it.
(test_getopt_long_only): New test.
* doc/glibc-functions/getopt_long.texi (getopt_long): Document
glibc shortcomings with 'W;', and enforcement of optind=0.
* doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
Likewise.
---
 ChangeLog                                 |   14 +
 doc/glibc-functions/getopt_long.texi      |   12 +-
 doc/glibc-functions/getopt_long_only.texi |   16 +-
 m4/getopt.m4                              |    9 +-
 tests/test-getopt.c                       |   13 +-
 tests/test-getopt.h                       |   11 +
 tests/test-getopt_long.h                  | 1119 ++++++++++++++++++++++++++++-
 7 files changed, 1142 insertions(+), 52 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5dd41d1..d7fe08c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2009-12-21  Eric Blake  <address@hidden>

+       test-getopt: enhance test
+       * m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Require that getopt_long
+       supports optind=0.
+       * tests/test-getopt.c (OPTIND_MIN): Move...
+       * tests/test-getopt.h (OPTIND_MIN): ...here.
+       * tests/test-getopt_long.h (test_getopt_long): Add more coverage.
+       Require that optind=0 works, since modern BSD supports it in
+       addition to optreset, and since coreutils expects it.
+       (test_getopt_long_only): New test.
+       * doc/glibc-functions/getopt_long.texi (getopt_long): Document
+       glibc shortcomings with 'W;', and enforcement of optind=0.
+       * doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
+       Likewise.
+
        test-utimens: avoid spurious failure
        * tests/test-chown.h (nap): Factor...
        * tests/nap.h: ...into new file.
diff --git a/doc/glibc-functions/getopt_long.texi b/doc/glibc-
functions/getopt_long.texi
index c9843ea..ca0fd34 100644
--- a/doc/glibc-functions/getopt_long.texi
+++ b/doc/glibc-functions/getopt_long.texi
@@ -15,6 +15,10 @@ getopt_long
 options string when @env{POSIXLY_CORRECT} is set on some platforms:
 Cygwin 1.7.0.
 @item
+Some implementations fail to reset state, including re-checking
address@hidden, when @code{optind} is set to @samp{0}:
+NetBSD, Cygwin 1.7.0.
address@hidden
 The function @code{getopt_long} does not support options with optional
 arguments on some platforms:
 MacOS X 10.5, OpenBSD 4.0, AIX 5.2, IRIX 6.5, Solaris 10, Cygwin 1.5.x.
@@ -26,9 +30,7 @@ getopt_long
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-The glibc implementation allows a complete reset of the environment,
-including re-checking for @env{POSIXLY_CORRECT}, by setting
address@hidden to 0.  Other implementations provide @code{optreset},
-causing a reset by setting it non-zero, although it does not
-necessarily re-read @env{POSIXLY_CORRECT}.
+The glibc extension of using an option string containing @samp{W;} to
+allow @code{-W foo} to behave synonymously with @code{--foo} is not
+very reliable, even in glibc.
 @end itemize
diff --git a/doc/glibc-functions/getopt_long_only.texi b/doc/glibc-
functions/getopt_long_only.texi
index ed6cd7b..7a11874 100644
--- a/doc/glibc-functions/getopt_long_only.texi
+++ b/doc/glibc-functions/getopt_long_only.texi
@@ -15,6 +15,10 @@ getopt_long_only
 in the options string when @env{POSIXLY_CORRECT} is set on some platforms:
 Cygwin 1.7.0.
 @item
+Some implementations fail to reset state, including re-checking
address@hidden, when @code{optind} is set to @samp{0}:
+NetBSD, Cygwin 1.7.0.
address@hidden
 The function @code{getopt_long_only} does not support options with
 optional arguments on some platforms:
 MacOS X 10.5, OpenBSD 4.0, AIX 5.2, Solaris 10, Cygwin 1.5.x.
@@ -27,9 +31,11 @@ getopt_long_only
 Portability problems not fixed by Gnulib:
 @itemize
 @item
-The glibc implementation allows a complete reset of the environment,
-including re-checking for @env{POSIXLY_CORRECT}, by setting
address@hidden to 0.  Other implementations provide @code{optreset},
-causing a reset by setting it non-zero, although it does not
-necessarily re-read @env{POSIXLY_CORRECT}.
+The glibc extension of using an option string containing @samp{W;} to
+allow @code{-W foo} to behave synonymously with @code{--foo} is not
+very reliable.
address@hidden
+Some implementations return success instead of reporting an ambiguity
+if user's option is a prefix of two long options with the same flag:
+FreeBSD.
 @end itemize
diff --git a/m4/getopt.m4 b/m4/getopt.m4
index 5809816..8b1bb8f 100644
--- a/m4/getopt.m4
+++ b/m4/getopt.m4
@@ -88,6 +88,7 @@ AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
   dnl strings starts with '+' and it's not the first call.  Some internal state
   dnl is left over from earlier calls, and neither setting optind = 0 nor
   dnl setting optreset = 1 get rid of this internal state.
+  dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
   if test -z "$gl_replace_getopt"; then
     AC_CACHE_CHECK([whether getopt is POSIX compatible],
       [gl_cv_func_getopt_posix],
@@ -187,6 +188,7 @@ main ()
       [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
        # optstring is necessary for programs like m4 that have POSIX-mandated
        # semantics for supporting options interspersed with files.
+       # Also, since getopt_long is a GNU extension, we require optind=0.
        gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes}
        POSIXLY_CORRECT=1
        export POSIXLY_CORRECT
@@ -194,11 +196,6 @@ main ()
         [AC_LANG_PROGRAM([[#include <getopt.h>
                            #include <stddef.h>
                            #include <string.h>
-#if !HAVE_DECL_OPTRESET
-# define OPTIND_MIN 0
-#else
-# define OPTIND_MIN (optreset = 1)
-#endif
            ]], [[
              /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
                 and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
@@ -231,7 +228,7 @@ main ()
              /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0.  */
              {
                char *argv[] = { "program", "foo", "-p", NULL };
-               optind = OPTIND_MIN;
+               optind = 0;
                if (getopt (3, argv, "-p") != 1)
                  return 6;
                if (getopt (3, argv, "-p") != 'p')
diff --git a/tests/test-getopt.c b/tests/test-getopt.c
index bf85a0d..591cf0b 100644
--- a/tests/test-getopt.c
+++ b/tests/test-getopt.c
@@ -39,14 +39,6 @@
     }                                                                        \
   while (0)

-/* The glibc/gnulib implementation of getopt supports setting optind = 0,
-   but other implementations don't.  */
-#if defined __GETOPT_PREFIX || (__GLIBC__ >= 2)
-# define OPTIND_MIN 0
-#else
-# define OPTIND_MIN 1
-#endif
-
 #include "test-getopt.h"
 #if GNULIB_GETOPT_GNU
 # include "test-getopt_long.h"
@@ -55,6 +47,10 @@
 int
 main (void)
 {
+  /* These default values are required by POSIX.  */
+  ASSERT (optind == 1);
+  ASSERT (opterr != 0);
+
   setenv ("POSIXLY_CORRECT", "1", 1);
   test_getopt ();

@@ -67,6 +63,7 @@ main (void)

 #if GNULIB_GETOPT_GNU
   test_getopt_long ();
+  test_getopt_long_only ();
 #endif

   return 0;
diff --git a/tests/test-getopt.h b/tests/test-getopt.h
index bcc7fe6..400bc27 100644
--- a/tests/test-getopt.h
+++ b/tests/test-getopt.h
@@ -18,6 +18,17 @@

 #include <stdbool.h>

+/* The glibc/gnulib implementation of getopt supports setting optind =
+   0, but not all other implementations do.  This matters for getopt.
+   But for getopt_long, we require GNU compatibility.  */
+#if defined __GETOPT_PREFIX || (__GLIBC__ >= 2)
+# define OPTIND_MIN 0
+#elif HAVE_DECL_OPTRESET
+# define OPTIND_MIN (optreset = 1)
+#else
+# define OPTIND_MIN 1
+#endif
+
 static void
 getopt_loop (int argc, const char **argv,
              const char *options,
diff --git a/tests/test-getopt_long.h b/tests/test-getopt_long.h
index 5b6acc6..097f9cf 100644
--- a/tests/test-getopt_long.h
+++ b/tests/test-getopt_long.h
@@ -18,13 +18,15 @@

 static int a_seen;
 static int b_seen;
+static int q_seen;

 static const struct option long_options_required[] =
   {
     { "alpha",    no_argument,       NULL, 'a' },
     { "beta",     no_argument,       &b_seen, 1 },
     { "prune",    required_argument, NULL, 'p' },
-    { "quetsche", required_argument, NULL, 'q' },
+    { "quetsche", required_argument, &q_seen, 1 },
+    { "xtremely-",no_argument,       NULL, 1003 },
     { "xtra",     no_argument,       NULL, 1001 },
     { "xtreme",   no_argument,       NULL, 1002 },
     { "xtremely", no_argument,       NULL, 1003 },
@@ -36,7 +38,7 @@ static const struct option long_options_optional[] =
     { "alpha",    no_argument,       NULL, 'a' },
     { "beta",     no_argument,       &b_seen, 1 },
     { "prune",    optional_argument, NULL, 'p' },
-    { "quetsche", optional_argument, NULL, 'q' },
+    { "quetsche", optional_argument, &q_seen, 1 },
     { NULL,       0,                 NULL, 0 }
   };

@@ -47,10 +49,11 @@ getopt_long_loop (int argc, const char **argv,
                   int *non_options_count, const char **non_options,
                   int *unrecognized)
 {
-  int option_index;
+  int option_index = -1;
   int c;

   opterr = 0;
+  q_seen = 0;
   while ((c = getopt_long (argc, (char **) argv, options, long_options,
                            &option_index))
          != -1)
@@ -59,6 +62,8 @@ getopt_long_loop (int argc, const char **argv,
         {
         case 0:
           /* An option with a non-NULL flag pointer was processed.  */
+          if (q_seen)
+            *q_value = optarg;
           break;
         case 'a':
           a_seen++;
@@ -77,6 +82,12 @@ getopt_long_loop (int argc, const char **argv,
           ASSERT (options[0] == '-');
           non_options[(*non_options_count)++] = optarg;
           break;
+        case ':':
+          /* Must only happen with option ':' at the beginning.  */
+          ASSERT (options[0] == ':'
+                  || ((options[0] == '-' || options[0] == '+')
+                      && options[1] == ':'));
+          /* fall through */
         case '?':
           *unrecognized = optopt;
           break;
@@ -235,8 +246,76 @@ test_getopt_long (void)
     ASSERT (c == 1003);
   }

-  /* Test processing of boolean options.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  /* Check that -W handles unknown options.  */
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-W";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long (argc, argv, "W;", long_options_required, 
&option_index);
+    ASSERT (c == '?');
+    ASSERT (optopt == 'W');
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-Wunknown";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long (argc, argv, "W;", long_options_required, 
&option_index);
+    /* glibc and BSD behave differently here, but for now, we allow
+       both behaviors since W support is not frequently used.  */
+    if (c == '?')
+      {
+        ASSERT (optopt == 0);
+        ASSERT (optarg == NULL);
+      }
+    else
+      {
+        ASSERT (c == 'W');
+        ASSERT (strcmp (optarg, "unknown") == 0);
+      }
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-W";
+    argv[argc++] = "unknown";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long (argc, argv, "W;", long_options_required, 
&option_index);
+    /* glibc and BSD behave differently here, but for now, we allow
+       both behaviors since W support is not frequently used.  */
+    if (c == '?')
+      {
+        ASSERT (optopt == 0);
+        ASSERT (optarg == NULL);
+      }
+    else
+      {
+        ASSERT (c == 'W');
+        ASSERT (strcmp (optarg, "unknown") == 0);
+      }
+  }
+
+  /* Test processing of boolean short options.  */
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -265,7 +344,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -295,7 +374,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 3);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -324,7 +403,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -355,8 +434,196 @@ test_getopt_long (void)
       ASSERT (optind == 3);
     }

-  /* Test processing of options with arguments.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  /* Test processing of boolean long options.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--alpha";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "ab", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--beta";
+      argv[argc++] = "--alpha";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "ab", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 1);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 3);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--alpha";
+      argv[argc++] = "--beta";
+      argv[argc++] = "--alpha";
+      argv[argc++] = "--beta";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "ab", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 2);
+      ASSERT (b_seen == 1);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 5);
+    }
+
+  /* Test processing of boolean long options via -W.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-Walpha";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "abW;", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-W";
+      argv[argc++] = "beta";
+      argv[argc++] = "-W";
+      argv[argc++] = "alpha";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "aW;b", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 1);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 5);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-Walpha";
+      argv[argc++] = "-Wbeta";
+      argv[argc++] = "-Walpha";
+      argv[argc++] = "-Wbeta";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "W;ab", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 2);
+      ASSERT (b_seen == 1);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 5);
+    }
+
+  /* Test processing of short options with arguments.  */
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -384,7 +651,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -413,7 +680,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 3);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -445,8 +712,190 @@ test_getopt_long (void)
       ASSERT (optind == 5);
     }

-  /* Test processing of options with optional arguments.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  /* Test processing of long options with arguments.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--p=foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--p";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 3);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-ab";
+      argv[argc++] = "--q";
+      argv[argc++] = "baz";
+      argv[argc++] = "--p=foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "abp:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 1);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 5);
+    }
+
+  /* Test processing of long options with arguments via -W.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-Wp=foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p:q:W;", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-W";
+      argv[argc++] = "p";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p:W;q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 4);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-ab";
+      argv[argc++] = "-Wq";
+      argv[argc++] = "baz";
+      argv[argc++] = "-W";
+      argv[argc++] = "p=foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "W;abp:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 1);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 6);
+    }
+
+  /* Test processing of short options with optional arguments.  */
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -474,7 +923,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -503,7 +952,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -533,8 +982,274 @@ test_getopt_long (void)
       ASSERT (optind == 3);
     }

+  /* Test processing of long options with optional arguments.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--p=foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p::q::", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--p";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p::q::", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--p=";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p::q::", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && *p_value == '\0');
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "--p";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "abp::q::", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 3);
+    }
+
+  /* Test processing of long options with optional arguments via -W.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-Wp=foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p::q::W;", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-Wp";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "p::q::W;", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      /* glibc bug http://sources.redhat.com/bugzilla/show_bug.cgi?id=11041 */
+      /* ASSERT (p_value == NULL); */
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-Wp=";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "W;p::q::", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && *p_value == '\0');
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 2);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-W";
+      argv[argc++] = "p=";
+      argv[argc++] = "foo";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "W;p::q::", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && *p_value == '\0');
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 3);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-W";
+      argv[argc++] = "p";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "W;abp::q::", long_options_optional,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      /* ASSERT (p_value == NULL); */
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 4);
+    }
+
   /* Check that invalid options are recognized.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -565,9 +1280,104 @@ test_getopt_long (void)
       ASSERT (unrecognized == 'x');
       ASSERT (optind == 5);
     }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "-:";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "abp:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == ':');
+      ASSERT (optind == 5);
+    }
+
+  /* Check that unexpected arguments are recognized.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "--a=";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "abp:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 'a');
+      ASSERT (optind == 4);
+    }
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "--b=";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "abp:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      /* When flag is non-zero, glibc sets optopt anyway, but BSD
+         leaves optopt unchanged.  */
+      ASSERT (unrecognized == 1 || unrecognized == 0);
+      ASSERT (optind == 4);
+    }

   /* Check that by default, non-options arguments are moved to the end.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -609,7 +1419,7 @@ test_getopt_long (void)
     }

   /* Check that '--' ends the argument processing.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -661,7 +1471,7 @@ test_getopt_long (void)
     }

   /* Check that the '-' flag causes non-options to be returned in order.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -706,7 +1516,7 @@ test_getopt_long (void)
     }

   /* Check that '--' ends the argument processing.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -778,7 +1588,7 @@ test_getopt_long (void)
     }

   /* Check that the '-' flag has to come first.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -821,7 +1631,7 @@ test_getopt_long (void)

   /* Check that the '+' flag causes the first non-option to terminate the
      loop.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -861,7 +1671,7 @@ test_getopt_long (void)
       ASSERT (unrecognized == 0);
       ASSERT (optind == 1);
     }
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -890,7 +1700,7 @@ test_getopt_long (void)
     }

   /* Check that '--' ends the argument processing.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -942,7 +1752,7 @@ test_getopt_long (void)
     }

   /* Check that the '+' flag has to come first.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -993,8 +1803,50 @@ test_getopt_long_posix (void)
 {
   int start;

+  /* Check that POSIXLY_CORRECT stops parsing the same as leading '+'.  */
+  for (start = 0; start <= 1; start++)
+    {
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      int argc = 0;
+      const char *argv[10];
+      a_seen = 0;
+      b_seen = 0;
+
+      argv[argc++] = "program";
+      argv[argc++] = "donald";
+      argv[argc++] = "-p";
+      argv[argc++] = "billy";
+      argv[argc++] = "duck";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      getopt_long_loop (argc, argv, "abp:q:", long_options_required,
+                        &p_value, &q_value,
+                        &non_options_count, non_options, &unrecognized);
+      ASSERT (strcmp (argv[0], "program") == 0);
+      ASSERT (strcmp (argv[1], "donald") == 0);
+      ASSERT (strcmp (argv[2], "-p") == 0);
+      ASSERT (strcmp (argv[3], "billy") == 0);
+      ASSERT (strcmp (argv[4], "duck") == 0);
+      ASSERT (strcmp (argv[5], "-a") == 0);
+      ASSERT (strcmp (argv[6], "bar") == 0);
+      ASSERT (argv[7] == NULL);
+      ASSERT (a_seen == 0);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 0);
+      ASSERT (optind == 1);
+    }
+
   /* Check that POSIXLY_CORRECT doesn't change optional arguments.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -1024,7 +1876,7 @@ test_getopt_long_posix (void)
     }

   /* Check that leading - still sees options after non-options.  */
-  for (start = OPTIND_MIN; start <= 1; start++)
+  for (start = 0; start <= 1; start++)
     {
       const char *p_value = NULL;
       const char *q_value = NULL;
@@ -1055,3 +1907,214 @@ test_getopt_long_posix (void)
       ASSERT (optind == 4);
     }
 }
+
+/* Reduce casting, so we can use string literals elsewhere.
+   getopt_long_only takes an array of char*, but luckily does not
+   modify those elements, so we can pass const char*.  */
+static int
+do_getopt_long_only (int argc, const char **argv, const char *shortopts,
+                     const struct option *longopts, int *longind)
+{
+  return getopt_long_only (argc, (char **) argv, shortopts, longopts, longind);
+}
+
+static void
+test_getopt_long_only (void)
+{
+  /* Test disambiguation of options.  */
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-x";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "ab", long_options_required,
+                             &option_index);
+    ASSERT (c == '?');
+    ASSERT (optopt == 0);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-x";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "abx", long_options_required,
+                             &option_index);
+    ASSERT (c == 'x');
+    ASSERT (optopt == 0);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "--x";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "abx", long_options_required,
+                             &option_index);
+    ASSERT (c == '?');
+    ASSERT (optopt == 0);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-b";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    b_seen = 0;
+    c = do_getopt_long_only (argc, argv, "abx", long_options_required,
+                             &option_index);
+    ASSERT (c == 'b');
+    ASSERT (b_seen == 0);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "--b";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    b_seen = 0;
+    c = do_getopt_long_only (argc, argv, "abx", long_options_required,
+                             &option_index);
+    ASSERT (c == 0);
+    ASSERT (b_seen == 1);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-xt";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "ab", long_options_required,
+                             &option_index);
+    ASSERT (c == '?');
+    ASSERT (optopt == 0);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-xt";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "abx", long_options_required,
+                             &option_index);
+    ASSERT (c == '?');
+    ASSERT (optopt == 0);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-xtra";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "ab", long_options_required,
+                             &option_index);
+    ASSERT (c == 1001);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-xtreme";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "abx:", long_options_required,
+                             &option_index);
+    ASSERT (c == 1002);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-xtremel";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "ab", long_options_required,
+                             &option_index);
+    /* glibc bug http://sources.redhat.com/bugzilla/show_bug.cgi?id=11041 */
+    /* ASSERT (c == 1003); */
+    ASSERT (optind == 2);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-xtremel";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "abx::", long_options_required,
+                             &option_index);
+    /* glibc bug http://sources.redhat.com/bugzilla/show_bug.cgi?id=11041 */
+    /* ASSERT (c == 1003); */
+    ASSERT (optind == 2);
+    ASSERT (optarg == NULL);
+  }
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-xtras";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long_only (argc, argv, "abx::", long_options_required,
+                             &option_index);
+    ASSERT (c == 'x');
+    ASSERT (strcmp (optarg, "tras") == 0);
+  }
+}
-- 
1.6.4.2


>From 5ce8c348609272596949a53f35a2c6361ecf0a21 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Wed, 2 Dec 2009 15:48:08 -0700
Subject: [PATCH 2/3] test-getopt: test stderr behavior

Portions of this commit are commented out because they tickle
glibc bugs.  For a real-life example of the bug:

$ env -ua -:
env: invalid option -- :
Try `env --help' for more information.
$ env -:
env: invalid option -- :
Try `env --help' for more information.
$ env -+
env: invalid option -- +
Try `env --help' for more information.
$ env -ua -+
Try `env --help' for more information.

Notice that when -+ is not given as the first argument, the
error message is mistakenly suppressed.

* modules/getopt-posix-tests (Depends-on): Add dup2.
* tests/test-getopt.c (ASSERT): Avoid stderr.
(main): Move stderr to a temporary file.
* tests/test-getopt.h (getopt_loop): No longer manipulate opterr.
Instead, add parameter to inform caller if output occurred.
(test_getopt): Adjust all tests to expect silence, and add new
tests of leading ":".
* doc/glibc-functions/getopt_long.texi (getopt_long): Document
glibc shortcomings leading "-:" or "+:".
* doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
Likewise.
* doc/posix-functions/getopt.texi (getopt): Likewise.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog                                 |   14 +
 doc/glibc-functions/getopt_long.texi      |    3 +
 doc/glibc-functions/getopt_long_only.texi |    3 +
 doc/posix-functions/getopt.texi           |    3 +
 modules/getopt-posix-tests                |    1 +
 tests/test-getopt.c                       |   29 ++-
 tests/test-getopt.h                       |  387 +++++++++++++++++++++++++++--
 7 files changed, 415 insertions(+), 25 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d7fe08c..003615f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2009-12-21  Eric Blake  <address@hidden>

+       test-getopt: test stderr behavior
+       * modules/getopt-posix-tests (Depends-on): Add dup2.
+       * tests/test-getopt.c (ASSERT): Avoid stderr.
+       (main): Move stderr to a temporary file.
+       * tests/test-getopt.h (getopt_loop): No longer manipulate opterr.
+       Instead, add parameter to inform caller if output occurred.
+       (test_getopt): Adjust all existing tests to expect silence, and
+       add new tests of leading ":".
+       * doc/glibc-functions/getopt_long.texi (getopt_long): Document
+       glibc shortcomings leading "-:" or "+:".
+       * doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
+       Likewise.
+       * doc/posix-functions/getopt.texi (getopt): Likewise.
+
        test-getopt: enhance test
        * m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Require that getopt_long
        supports optind=0.
diff --git a/doc/glibc-functions/getopt_long.texi b/doc/glibc-
functions/getopt_long.texi
index ca0fd34..28118aa 100644
--- a/doc/glibc-functions/getopt_long.texi
+++ b/doc/glibc-functions/getopt_long.texi
@@ -33,4 +33,7 @@ getopt_long
 The glibc extension of using an option string containing @samp{W;} to
 allow @code{-W foo} to behave synonymously with @code{--foo} is not
 very reliable, even in glibc.
address@hidden
+Mixing a leading @samp{-} or @samp{+} with a leading @samp{:} in the
+optstring argument has inconsistent effects across platforms.
 @end itemize
diff --git a/doc/glibc-functions/getopt_long_only.texi b/doc/glibc-
functions/getopt_long_only.texi
index 7a11874..0255359 100644
--- a/doc/glibc-functions/getopt_long_only.texi
+++ b/doc/glibc-functions/getopt_long_only.texi
@@ -38,4 +38,7 @@ getopt_long_only
 Some implementations return success instead of reporting an ambiguity
 if user's option is a prefix of two long options with the same flag:
 FreeBSD.
address@hidden
+Mixing a leading @samp{-} or @samp{+} with a leading @samp{:} in the
+optstring argument has inconsistent effects across platforms.
 @end itemize
diff --git a/doc/posix-functions/getopt.texi b/doc/posix-functions/getopt.texi
index 85f5289..822dbb3 100644
--- a/doc/posix-functions/getopt.texi
+++ b/doc/posix-functions/getopt.texi
@@ -57,4 +57,7 @@ getopt
 @code{optind} to 0.  Other implementations provide @code{optreset},
 causing a reset by setting it non-zero, although it does not
 necessarily re-read @env{POSIXLY_CORRECT}.
address@hidden
+Mixing a leading @samp{-} or @samp{+} with a leading @samp{:} in the
+optstring argument has inconsistent effects across platforms.
 @end itemize
diff --git a/modules/getopt-posix-tests b/modules/getopt-posix-tests
index be828cf..b2e3727 100644
--- a/modules/getopt-posix-tests
+++ b/modules/getopt-posix-tests
@@ -4,6 +4,7 @@ tests/test-getopt.h
 tests/test-getopt_long.h

 Depends-on:
+dup2
 setenv
 stdbool
 unistd
diff --git a/tests/test-getopt.c b/tests/test-getopt.c
index 591cf0b..6811a02 100644
--- a/tests/test-getopt.c
+++ b/tests/test-getopt.c
@@ -18,6 +18,13 @@

 #include <config.h>

+/* None of the files accessed by this test are large, so disable the
+   ftell link warning if we are not using the gnulib ftell module.  */
+#if !GNULIB_FTELL
+# undef GL_LINK_WARNING
+# define GL_LINK_WARNING(ignored) ((void) 0)
+#endif
+
 #if GNULIB_GETOPT_GNU
 # include <getopt.h>
 #endif
@@ -27,13 +34,20 @@
 #include <stdlib.h>
 #include <string.h>

+/* This test intentionally remaps stderr.  So, we arrange to have fd 10
+   (outside the range of interesting fd's during the test) set up to
+   duplicate the original stderr.  */
+
+#define BACKUP_STDERR_FILENO 10
+static FILE *myerr;
+
 #define ASSERT(expr) \
   do                                                                         \
     {                                                                        \
       if (!(expr))                                                           \
         {                                                                    \
-          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
-          fflush (stderr);                                                   \
+          fprintf (myerr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
+          fflush (myerr);                                                    \
           abort ();                                                          \
         }                                                                    \
     }                                                                        \
@@ -47,6 +61,14 @@
 int
 main (void)
 {
+   /* This test validates that stderr is used correctly, so move the
+      original into fd 10.  */
+  if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
+      || (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
+    return 2;
+
+  ASSERT (freopen ("test-getopt.tmp", "w", stderr) == stderr);
+
   /* These default values are required by POSIX.  */
   ASSERT (optind == 1);
   ASSERT (opterr != 0);
@@ -66,5 +88,8 @@ main (void)
   test_getopt_long_only ();
 #endif

+  ASSERT (fclose (stderr) == 0);
+  ASSERT (remove ("test-getopt.tmp") == 0);
+
   return 0;
 }
diff --git a/tests/test-getopt.h b/tests/test-getopt.h
index 400bc27..ca8872f 100644
--- a/tests/test-getopt.h
+++ b/tests/test-getopt.h
@@ -35,11 +35,11 @@ getopt_loop (int argc, const char **argv,
              int *a_seen, int *b_seen,
              const char **p_value, const char **q_value,
              int *non_options_count, const char **non_options,
-             int *unrecognized)
+             int *unrecognized, bool *message_issued)
 {
   int c;
+  int pos = ftell (stderr);

-  opterr = 0;
   while ((c = getopt (argc, (char **) argv, options)) != -1)
     {
       switch (c)
@@ -61,6 +61,12 @@ getopt_loop (int argc, const char **argv,
           ASSERT (options[0] == '-');
           non_options[(*non_options_count)++] = optarg;
           break;
+        case ':':
+          /* Must only happen with option ':' at the beginning.  */
+          ASSERT (options[0] == ':'
+                  || ((options[0] == '-' || options[0] == '+')
+                      && options[1] == ':'));
+          /* fall through */
         case '?':
           *unrecognized = optopt;
           break;
@@ -69,6 +75,8 @@ getopt_loop (int argc, const char **argv,
           break;
         }
     }
+
+  *message_issued = pos < ftell (stderr);
 }

 static void
@@ -94,6 +102,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -103,9 +112,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "ab",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 1);
       ASSERT (b_seen == 0);
       ASSERT (p_value == NULL);
@@ -113,6 +123,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -123,6 +134,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -133,9 +145,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "ab",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 1);
       ASSERT (b_seen == 1);
       ASSERT (p_value == NULL);
@@ -143,6 +156,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 3);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -153,6 +167,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -162,9 +177,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "ab",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 1);
       ASSERT (b_seen == 1);
       ASSERT (p_value == NULL);
@@ -172,6 +188,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -182,6 +199,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -192,9 +210,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "ab",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 2);
       ASSERT (b_seen == 1);
       ASSERT (p_value == NULL);
@@ -202,6 +221,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 3);
+      ASSERT (!output);
     }

   /* Test processing of options with arguments.  */
@@ -214,6 +234,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -222,9 +243,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "p:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 0);
       ASSERT (b_seen == 0);
       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
@@ -232,6 +254,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -242,6 +265,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -251,9 +275,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "p:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 0);
       ASSERT (b_seen == 0);
       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
@@ -261,6 +286,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 3);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -271,6 +297,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -282,9 +309,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 1);
       ASSERT (b_seen == 1);
       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
@@ -292,6 +320,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 5);
+      ASSERT (!output);
     }

 #if GNULIB_GETOPT_GNU
@@ -305,6 +334,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -313,9 +343,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "p::q::",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 0);
       ASSERT (b_seen == 0);
       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
@@ -323,6 +354,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -333,6 +365,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -342,9 +375,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "p::q::",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 0);
       ASSERT (b_seen == 0);
       ASSERT (p_value == NULL);
@@ -352,6 +386,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 2);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -362,6 +397,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -371,9 +407,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "abp::q::",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 1);
       ASSERT (b_seen == 0);
       ASSERT (p_value == NULL);
@@ -381,10 +418,46 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 3);
+      ASSERT (!output);
     }
 #endif

-  /* Check that invalid options are recognized.  */
+  /* Check that invalid options are recognized; and that both opterr
+     and leading ':' can silence output.  */
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "-x";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 42;
+      getopt_loop (argc, argv, "abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 'x');
+      ASSERT (optind == 5);
+      ASSERT (output);
+    }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
       int a_seen = 0;
@@ -394,6 +467,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -405,9 +479,44 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 0;
       getopt_loop (argc, argv, "abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 'x');
+      ASSERT (optind == 5);
+      ASSERT (!output);
+    }
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "-x";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 1;
+      getopt_loop (argc, argv, ":abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 1);
       ASSERT (b_seen == 0);
       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
@@ -415,6 +524,201 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 'x');
       ASSERT (optind == 5);
+      ASSERT (!output);
+    }
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "-:";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 42;
+      getopt_loop (argc, argv, "abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == ':');
+      ASSERT (optind == 5);
+      ASSERT (output);
+    }
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "-:";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 0;
+      getopt_loop (argc, argv, "abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == ':');
+      ASSERT (optind == 5);
+      ASSERT (!output);
+    }
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-p";
+      argv[argc++] = "foo";
+      argv[argc++] = "-:";
+      argv[argc++] = "-a";
+      argv[argc++] = "bar";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 1;
+      getopt_loop (argc, argv, ":abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == ':');
+      ASSERT (optind == 5);
+      ASSERT (!output);
+    }
+
+  /* Check for missing argument behavior.  */
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-ap";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 1;
+      getopt_loop (argc, argv, "abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 'p');
+      ASSERT (optind == 2);
+      ASSERT (output);
+    }
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-ap";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 0;
+      getopt_loop (argc, argv, "abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 'p');
+      ASSERT (optind == 2);
+      ASSERT (!output);
+    }
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int a_seen = 0;
+      int b_seen = 0;
+      const char *p_value = NULL;
+      const char *q_value = NULL;
+      int non_options_count = 0;
+      const char *non_options[10];
+      int unrecognized = 0;
+      bool output;
+      int argc = 0;
+      const char *argv[10];
+
+      argv[argc++] = "program";
+      argv[argc++] = "-ap";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 1;
+      getopt_loop (argc, argv, ":abp:q:",
+                   &a_seen, &b_seen, &p_value, &q_value,
+                   &non_options_count, non_options, &unrecognized, &output);
+      ASSERT (a_seen == 1);
+      ASSERT (b_seen == 0);
+      ASSERT (p_value == NULL);
+      ASSERT (q_value == NULL);
+      ASSERT (non_options_count == 0);
+      ASSERT (unrecognized == 'p');
+      ASSERT (optind == 2);
+      ASSERT (!output);
     }

   /* Check that by default, non-options arguments are moved to the end.  */
@@ -427,6 +731,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -439,9 +744,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       if (posixly)
         {
           ASSERT (strcmp (argv[0], "program") == 0);
@@ -459,6 +765,7 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 1);
+          ASSERT (!output);
         }
       else
         {
@@ -477,6 +784,7 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 4);
+          ASSERT (!output);
         }
     }

@@ -490,6 +798,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[20];

@@ -507,9 +816,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       if (posixly)
         {
           ASSERT (strcmp (argv[0], "program") == 0);
@@ -532,6 +842,7 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 1);
+          ASSERT (!output);
         }
       else
         {
@@ -555,6 +866,7 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 5);
+          ASSERT (!output);
         }
     }

@@ -569,6 +881,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -581,9 +894,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "-abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (strcmp (argv[0], "program") == 0);
       ASSERT (strcmp (argv[1], "donald") == 0);
       ASSERT (strcmp (argv[2], "-p") == 0);
@@ -602,6 +916,7 @@ test_getopt (void)
       ASSERT (strcmp (non_options[2], "bar") == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 7);
+      ASSERT (!output);
     }

   /* Check that '--' ends the argument processing.  */
@@ -614,6 +929,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[20];

@@ -631,9 +947,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "-abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (strcmp (argv[0], "program") == 0);
       ASSERT (strcmp (argv[1], "donald") == 0);
       ASSERT (strcmp (argv[2], "-p") == 0);
@@ -651,6 +968,7 @@ test_getopt (void)
       ASSERT (b_seen == 0);
       ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
       ASSERT (q_value == NULL);
+      ASSERT (!output);
       if (non_options_count == 2)
         {
           /* glibc behaviour.  */
@@ -687,6 +1005,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -699,9 +1018,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "abp:q:-",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       if (posixly)
         {
           ASSERT (strcmp (argv[0], "program") == 0);
@@ -719,6 +1039,7 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 1);
+          ASSERT (!output);
         }
       else
         {
@@ -737,6 +1058,7 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 4);
+          ASSERT (!output);
         }
     }

@@ -751,6 +1073,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -763,9 +1086,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "+abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (strcmp (argv[0], "program") == 0);
       ASSERT (strcmp (argv[1], "donald") == 0);
       ASSERT (strcmp (argv[2], "-p") == 0);
@@ -781,6 +1105,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind == 1);
+      ASSERT (!output);
     }
   for (start = OPTIND_MIN; start <= 1; start++)
     {
@@ -791,6 +1116,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -798,9 +1124,13 @@ test_getopt (void)
       argv[argc++] = "-+";
       argv[argc] = NULL;
       optind = start;
+      /* Suppress output, since glibc is inconsistent on whether this
+         prints a message:
+         http://sources.redhat.com/bugzilla/show_bug.cgi?id=11039 */
+      opterr = 0;
       getopt_loop (argc, argv, "+abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (a_seen == 0);
       ASSERT (b_seen == 0);
       ASSERT (p_value == NULL);
@@ -808,6 +1138,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == '+');
       ASSERT (optind == 2);
+      ASSERT (!output);
     }

   /* Check that '--' ends the argument processing.  */
@@ -820,6 +1151,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[20];

@@ -837,9 +1169,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "+abp:q:",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       ASSERT (strcmp (argv[0], "program") == 0);
       ASSERT (strcmp (argv[1], "donald") == 0);
       ASSERT (strcmp (argv[2], "-p") == 0);
@@ -860,6 +1193,7 @@ test_getopt (void)
       ASSERT (non_options_count == 0);
       ASSERT (unrecognized == 0);
       ASSERT (optind = 1);
+      ASSERT (!output);
     }

   /* Check that the '+' flag has to come first.  */
@@ -872,6 +1206,7 @@ test_getopt (void)
       int non_options_count = 0;
       const char *non_options[10];
       int unrecognized = 0;
+      bool output;
       int argc = 0;
       const char *argv[10];

@@ -884,9 +1219,10 @@ test_getopt (void)
       argv[argc++] = "bar";
       argv[argc] = NULL;
       optind = start;
+      opterr = 1;
       getopt_loop (argc, argv, "abp:q:+",
                    &a_seen, &b_seen, &p_value, &q_value,
-                   &non_options_count, non_options, &unrecognized);
+                   &non_options_count, non_options, &unrecognized, &output);
       if (posixly)
         {
           ASSERT (strcmp (argv[0], "program") == 0);
@@ -904,6 +1240,7 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 1);
+          ASSERT (!output);
         }
       else
         {
@@ -922,6 +1259,10 @@ test_getopt (void)
           ASSERT (non_options_count == 0);
           ASSERT (unrecognized == 0);
           ASSERT (optind == 4);
+          ASSERT (!output);
         }
     }
+
+  /* No tests of "-:..." or "+:...", due to glibc bug:
+     http://sources.redhat.com/bugzilla/show_bug.cgi?id=11039 */
 }
-- 
1.6.4.2


>From 0dcaf7ef6f3e8246691170f958fc5a087b1bb288 Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Wed, 16 Dec 2009 12:03:14 -0700
Subject: [PATCH 3/3] getopt: synchronize from glibc

Make gnulib closer to glibc, to make tracking it easier to track
extent of pending patches against glibc.

* lib/getopt.c (_getopt_initialize, _getopt_internal_r): Swap
parameter order.  Adjust all callers.
(_getopt_internal_r, main): Adjust quoting in error messages.
Drop considerations for outdated POSIX 1003.2 error message.
* lib/getopt1.c (_getopt_long_r, _getopt_long_only_r): Adjust
callers.
* lib/getopt_int.h (_getopt_internal_r): Adjust prototype.

Signed-off-by: Eric Blake <address@hidden>
---
 ChangeLog        |    9 ++++
 lib/getopt.c     |  124 +++++++++++++++++++++++++++--------------------------
 lib/getopt1.c    |    4 +-
 lib/getopt_int.h |    4 +-
 4 files changed, 76 insertions(+), 65 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 003615f..f6a1a7a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2009-12-21  Eric Blake  <address@hidden>

+       getopt: synchronize from glibc
+       * lib/getopt.c (_getopt_initialize, _getopt_internal_r): Swap
+       parameter order.  Adjust all callers.
+       (_getopt_internal_r, main): Adjust quoting in error messages.
+       Drop considerations for outdated POSIX 1003.2 error message.
+       * lib/getopt1.c (_getopt_long_r, _getopt_long_only_r): Adjust
+       callers.
+       * lib/getopt_int.h (_getopt_internal_r): Adjust prototype.
+
        test-getopt: test stderr behavior
        * modules/getopt-posix-tests (Depends-on): Add dup2.
        * tests/test-getopt.c (ASSERT): Avoid stderr.
diff --git a/lib/getopt.c b/lib/getopt.c
index fbe7b5b..5e9a4a5 100644
--- a/lib/getopt.c
+++ b/lib/getopt.c
@@ -1,9 +1,9 @@
 /* Getopt for GNU.
-   NOTE: getopt is now part of the C library, so if you don't know what
+   NOTE: getopt is part of the C library, so if you don't know what
    "Keep this file name-space clean" means, talk to address@hidden
    before changing it!
-   Copyright (C) 
1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004,2006,2008,2009
-        Free Software Foundation, Inc.
+   Copyright (C) 1987-1996,1998-2004,2006,2008-2009 Free Software
+   Foundation, Inc.
    This file is part of the GNU C Library.

    This program is free software: you can redistribute it and/or modify
@@ -45,8 +45,9 @@
 # define attribute_hidden
 #endif

-/* Unlike standard Unix `getopt', functions like `getopt_long'
-   let the user intersperse the options with the other arguments.
+/* This version of `getopt' appears to the caller like standard Unix `getopt'
+   but it behaves differently for the user, since it allows the user
+   to intersperse the options with the other arguments.

    As `getopt_long' works, it permutes the elements of ARGV so that,
    when it is done, all the options precede everything else.  Thus
@@ -54,7 +55,7 @@

    Using `getopt' or setting the environment variable POSIXLY_CORRECT
    disables permutation.
-   Then the application's behavior is completely standard.
+   Then the behavior is completely standard.

    GNU application programs can use a third alternative mode in which
    they can distinguish the relative order of options and other arguments.  */
@@ -227,7 +228,7 @@ exchange (char **argv, struct _getopt_data *d)
 static const char *
 _getopt_initialize (int argc _UNUSED_PARAMETER_,
                     char **argv _UNUSED_PARAMETER_, const char *optstring,
-                    int posixly_correct, struct _getopt_data *d)
+                    struct _getopt_data *d, int posixly_correct)
 {
   /* Start processing options with ARGV-element 1 (since ARGV-element 0
      is the program name); the sequence of previously skipped
@@ -331,6 +332,10 @@ _getopt_initialize (int argc _UNUSED_PARAMETER_,
    `flag' field is nonzero, the value of the option's `val' field
    if the `flag' field is zero.

+   The elements of ARGV aren't really const, because we permute them.
+   But we pretend they're const in the prototype to be compatible
+   with other systems.
+
    LONGOPTS is a vector of `struct option' terminated by an
    element containing a name which is zero.

@@ -339,15 +344,12 @@ _getopt_initialize (int argc _UNUSED_PARAMETER_,
    recent call.

    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
-   long-named options.
-
-   If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
-   environment variable were set.  */
+   long-named options.  */

 int
 _getopt_internal_r (int argc, char **argv, const char *optstring,
                     const struct option *longopts, int *longind,
-                    int long_only, int posixly_correct, struct _getopt_data *d)
+                    int long_only, struct _getopt_data *d, int posixly_correct)
 {
   int print_errors = d->opterr;
   if (optstring[0] == ':')
@@ -362,8 +364,8 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
     {
       if (d->optind == 0)
         d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
-      optstring = _getopt_initialize (argc, argv, optstring,
-                                      posixly_correct, d);
+      optstring = _getopt_initialize (argc, argv, optstring, d,
+                                      posixly_correct);
       d->__initialized = 1;
     }

@@ -524,7 +526,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
 #if defined _LIBC && defined USE_IN_LIBIO
               char *buf;

-              if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
+              if (__asprintf (&buf, _("%s: option '%s' is ambiguous\n"),
                               argv[0], argv[d->optind]) >= 0)
                 {
                   _IO_flockfile (stderr);
@@ -540,7 +542,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                   free (buf);
                 }
 #else
-              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
+              fprintf (stderr, _("%s: option '%s' is ambiguous\n"),
                        argv[0], argv[d->optind]);
 #endif
             }
@@ -574,11 +576,11 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                           /* --option */
 #if defined _LIBC && defined USE_IN_LIBIO
                           n = __asprintf (&buf, _("\
-%s: option `--%s' doesn't allow an argument\n"),
+%s: option '--%s' doesn't allow an argument\n"),
                                           argv[0], pfound->name);
 #else
                           fprintf (stderr, _("\
-%s: option `--%s' doesn't allow an argument\n"),
+%s: option '--%s' doesn't allow an argument\n"),
                                    argv[0], pfound->name);
 #endif
                         }
@@ -587,12 +589,12 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                           /* +option or -option */
 #if defined _LIBC && defined USE_IN_LIBIO
                           n = __asprintf (&buf, _("\
-%s: option `%c%s' doesn't allow an argument\n"),
+%s: option '%c%s' doesn't allow an argument\n"),
                                           argv[0], argv[d->optind - 1][0],
                                           pfound->name);
 #else
                           fprintf (stderr, _("\
-%s: option `%c%s' doesn't allow an argument\n"),
+%s: option '%c%s' doesn't allow an argument\n"),
                                    argv[0], argv[d->optind - 1][0],
                                    pfound->name);
 #endif
@@ -635,7 +637,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                       char *buf;

                       if (__asprintf (&buf, _("\
-%s: option `%s' requires an argument\n"),
+%s: option '%s' requires an argument\n"),
                                       argv[0], argv[d->optind - 1]) >= 0)
                         {
                           _IO_flockfile (stderr);
@@ -653,7 +655,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                         }
 #else
                       fprintf (stderr,
-                               _("%s: option `%s' requires an argument\n"),
+                               _("%s: option '%s' requires an argument\n"),
                                argv[0], argv[d->optind - 1]);
 #endif
                     }
@@ -691,10 +693,10 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                 {
                   /* --option */
 #if defined _LIBC && defined USE_IN_LIBIO
-                  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
+                  n = __asprintf (&buf, _("%s: unrecognized option '--%s'\n"),
                                   argv[0], d->__nextchar);
 #else
-                  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
+                  fprintf (stderr, _("%s: unrecognized option '--%s'\n"),
                            argv[0], d->__nextchar);
 #endif
                 }
@@ -702,10 +704,10 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                 {
                   /* +option or -option */
 #if defined _LIBC && defined USE_IN_LIBIO
-                  n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
+                  n = __asprintf (&buf, _("%s: unrecognized option '%c%s'\n"),
                                   argv[0], argv[d->optind][0], d->__nextchar);
 #else
-                  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
+                  fprintf (stderr, _("%s: unrecognized option '%c%s'\n"),
                            argv[0], argv[d->optind][0], d->__nextchar);
 #endif
                 }
@@ -753,25 +755,12 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
               int n;
 #endif

-            if (d->__posixly_correct)
-              {
-                /* 1003.2 specifies the format of this message.  */
-#if defined _LIBC && defined USE_IN_LIBIO
-                n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
-                                argv[0], c);
-#else
-                fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
-#endif
-              }
-            else
-              {
 #if defined _LIBC && defined USE_IN_LIBIO
-                n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
-                                argv[0], c);
+              n = __asprintf (&buf, _("%s: invalid option -- '%c'\n"),
+                              argv[0], c);
 #else
-                fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
+              fprintf (stderr, _("%s: invalid option -- '%c'\n"), argv[0], c);
 #endif
-              }

 #if defined _LIBC && defined USE_IN_LIBIO
             if (n >= 0)
@@ -816,12 +805,11 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
           {
             if (print_errors)
               {
-                /* 1003.2 specifies the format of this message.  */
 #if defined _LIBC && defined USE_IN_LIBIO
                 char *buf;

                 if (__asprintf (&buf,
-                                _("%s: option requires an argument -- %c\n"),
+                                _("%s: option requires an argument -- '%c'\n"),
                                 argv[0], c) >= 0)
                   {
                     _IO_flockfile (stderr);
@@ -837,7 +825,8 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                     free (buf);
                   }
 #else
-                fprintf (stderr, _("%s: option requires an argument -- %c\n"),
+                fprintf (stderr,
+                         _("%s: option requires an argument -- '%c'\n"),
                          argv[0], c);
 #endif
               }
@@ -890,7 +879,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
 #if defined _LIBC && defined USE_IN_LIBIO
                 char *buf;

-                if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
+                if (__asprintf (&buf, _("%s: option '-W %s' is ambiguous\n"),
                                 argv[0], argv[d->optind]) >= 0)
                   {
                     _IO_flockfile (stderr);
@@ -906,7 +895,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                     free (buf);
                   }
 #else
-                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
+                fprintf (stderr, _("%s: option '-W %s' is ambiguous\n"),
                          argv[0], argv[d->optind]);
 #endif
               }
@@ -931,7 +920,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                         char *buf;

                         if (__asprintf (&buf, _("\
-%s: option `-W %s' doesn't allow an argument\n"),
+%s: option '-W %s' doesn't allow an argument\n"),
                                         argv[0], pfound->name) >= 0)
                           {
                             _IO_flockfile (stderr);
@@ -949,7 +938,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                           }
 #else
                         fprintf (stderr, _("\
-%s: option `-W %s' doesn't allow an argument\n"),
+%s: option '-W %s' doesn't allow an argument\n"),
                                  argv[0], pfound->name);
 #endif
                       }
@@ -970,7 +959,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                         char *buf;

                         if (__asprintf (&buf, _("\
-%s: option `%s' requires an argument\n"),
+%s: option '%s' requires an argument\n"),
                                         argv[0], argv[d->optind - 1]) >= 0)
                           {
                             _IO_flockfile (stderr);
@@ -988,7 +977,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                           }
 #else
                         fprintf (stderr,
-                                 _("%s: option `%s' requires an argument\n"),
+                                 _("%s: option '%s' requires an argument\n"),
                                  argv[0], argv[d->optind - 1]);
 #endif
                       }
@@ -1037,12 +1026,11 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
               {
                 if (print_errors)
                   {
-                    /* 1003.2 specifies the format of this message.  */
 #if defined _LIBC && defined USE_IN_LIBIO
                     char *buf;

                     if (__asprintf (&buf, _("\
-%s: option requires an argument -- %c\n"),
+%s: option requires an argument -- '%c'\n"),
                                     argv[0], c) >= 0)
                       {
                         _IO_flockfile (stderr);
@@ -1059,7 +1047,7 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,
                       }
 #else
                     fprintf (stderr,
-                             _("%s: option requires an argument -- %c\n"),
+                             _("%s: option requires an argument -- '%c'\n"),
                              argv[0], c);
 #endif
                   }
@@ -1082,16 +1070,17 @@ _getopt_internal_r (int argc, char **argv, const char 
*optstring,

 int
 _getopt_internal (int argc, char **argv, const char *optstring,
-                  const struct option *longopts, int *longind,
-                  int long_only, int posixly_correct)
+                  const struct option *longopts, int *longind, int long_only,
+                  int posixly_correct)
 {
   int result;

   getopt_data.optind = optind;
   getopt_data.opterr = opterr;

-  result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
-                               long_only, posixly_correct, &getopt_data);
+  result = _getopt_internal_r (argc, argv, optstring, longopts,
+                               longind, long_only, &getopt_data,
+                               posixly_correct);

   optind = getopt_data.optind;
   optarg = getopt_data.optarg;
@@ -1111,9 +1100,22 @@ enum { POSIXLY_CORRECT = 1 };
 int
 getopt (int argc, char *const *argv, const char *optstring)
 {
-  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
-                           POSIXLY_CORRECT);
+  return _getopt_internal (argc, (char **) argv, optstring,
+                           (const struct option *) 0,
+                           (int *) 0,
+                           0, POSIXLY_CORRECT);
+}
+
+#ifdef _LIBC
+int
+__posix_getopt (int argc, char *const *argv, const char *optstring)
+{
+  return _getopt_internal (argc, argv, optstring,
+                           (const struct option *) 0,
+                           (int *) 0,
+                           0, 1);
 }
+#endif

 
 #ifdef TEST
@@ -1162,7 +1164,7 @@ main (int argc, char **argv)
           break;

         case 'c':
-          printf ("option c with value `%s'\n", optarg);
+          printf ("option c with value '%s'\n", optarg);
           break;

         case '?':
diff --git a/lib/getopt1.c b/lib/getopt1.c
index c3179c5..6ec0fc0 100644
--- a/lib/getopt1.c
+++ b/lib/getopt1.c
@@ -50,7 +50,7 @@ _getopt_long_r (int argc, char **argv, const char *options,
                 struct _getopt_data *d)
 {
   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
-                             0, 0, d);
+                             0, d, 0);
 }

 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
@@ -73,7 +73,7 @@ _getopt_long_only_r (int argc, char **argv, const char 
*options,
                      struct _getopt_data *d)
 {
   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
-                             1, 0, d);
+                             1, d, 0);
 }

 
diff --git a/lib/getopt_int.h b/lib/getopt_int.h
index 18465bc..c407898 100644
--- a/lib/getopt_int.h
+++ b/lib/getopt_int.h
@@ -115,8 +115,8 @@ struct _getopt_data
 extern int _getopt_internal_r (int ___argc, char **___argv,
                                const char *__shortopts,
                                const struct option *__longopts, int *__longind,
-                               int __long_only, int __posixly_correct,
-                               struct _getopt_data *__data);
+                               int __long_only, struct _getopt_data *__data,
+                               int __posixly_correct);

 extern int _getopt_long_r (int ___argc, char **___argv,
                            const char *__shortopts,
-- 
1.6.4.2







reply via email to

[Prev in Thread] Current Thread [Next in Thread]