LCOV - code coverage report
Current view: top level - gltests - test-getopt.h (source / functions) Hit Total Coverage
Test: GNU Libidn Lines: 987 999 98.8 %
Date: 2015-08-01 15:30:48 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Test of command line argument processing.
       2             :    Copyright (C) 2009-2015 Free Software Foundation, Inc.
       3             : 
       4             :    This program is free software: you can redistribute it and/or modify
       5             :    it under the terms of the GNU General Public License as published by
       6             :    the Free Software Foundation; either version 3 of the License, or
       7             :    (at your option) any later version.
       8             : 
       9             :    This program is distributed in the hope that it will be useful,
      10             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :    GNU General Public License for more details.
      13             : 
      14             :    You should have received a copy of the GNU General Public License
      15             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      16             : 
      17             : /* Written by Bruno Haible <bruno@clisp.org>, 2009.  */
      18             : 
      19             : #include <stdbool.h>
      20             : 
      21             : /* The glibc/gnulib implementation of getopt supports setting optind =
      22             :    0, but not all other implementations do.  This matters for getopt.
      23             :    But for getopt_long, we require GNU compatibility.  */
      24             : #if defined __GETOPT_PREFIX || (__GLIBC__ >= 2 && !defined __UCLIBC__)
      25             : # define OPTIND_MIN 0
      26             : #elif HAVE_DECL_OPTRESET
      27             : # define OPTIND_MIN (optreset = 1)
      28             : #else
      29             : # define OPTIND_MIN 1
      30             : #endif
      31             : 
      32             : static void
      33         124 : getopt_loop (int argc, const char **argv,
      34             :              const char *options,
      35             :              int *a_seen, int *b_seen,
      36             :              const char **p_value, const char **q_value,
      37             :              int *non_options_count, const char **non_options,
      38             :              int *unrecognized, bool *message_issued)
      39             : {
      40             :   int c;
      41         124 :   int pos = ftell (stderr);
      42             : 
      43         484 :   while ((c = getopt (argc, (char **) argv, options)) != -1)
      44             :     {
      45         236 :       switch (c)
      46             :         {
      47             :         case 'a':
      48          80 :           (*a_seen)++;
      49          80 :           break;
      50             :         case 'b':
      51          20 :           (*b_seen)++;
      52          20 :           break;
      53             :         case 'p':
      54          64 :           *p_value = optarg;
      55          64 :           break;
      56             :         case 'q':
      57           4 :           *q_value = optarg;
      58           4 :           break;
      59             :         case '\1':
      60             :           /* Must only happen with option '-' at the beginning.  */
      61          20 :           ASSERT (options[0] == '-');
      62          20 :           non_options[(*non_options_count)++] = optarg;
      63          20 :           break;
      64             :         case ':':
      65             :           /* Must only happen with option ':' at the beginning.  */
      66          12 :           ASSERT (options[0] == ':'
      67             :                   || ((options[0] == '-' || options[0] == '+')
      68             :                       && options[1] == ':'));
      69             :           /* fall through */
      70             :         case '?':
      71          48 :           *unrecognized = optopt;
      72          48 :           break;
      73             :         default:
      74           0 :           *unrecognized = c;
      75           0 :           break;
      76             :         }
      77             :     }
      78             : 
      79         124 :   *message_issued = pos < ftell (stderr);
      80         124 : }
      81             : 
      82             : static void
      83           2 : test_getopt (void)
      84             : {
      85             :   int start;
      86           2 :   bool posixly = !!getenv ("POSIXLY_CORRECT");
      87             :   /* See comment in getopt.c:
      88             :      glibc gets a LSB-compliant getopt.
      89             :      Standalone applications get a POSIX-compliant getopt.  */
      90             : #if defined __GETOPT_PREFIX || !(__GLIBC__ >= 2 || defined __MINGW32__)
      91             :   /* Using getopt from gnulib or from a non-glibc system.  */
      92             :   posixly = true;
      93             : #endif
      94             : 
      95             :   /* Test processing of boolean options.  */
      96           6 :   for (start = OPTIND_MIN; start <= 1; start++)
      97             :     {
      98           4 :       int a_seen = 0;
      99           4 :       int b_seen = 0;
     100           4 :       const char *p_value = NULL;
     101           4 :       const char *q_value = NULL;
     102           4 :       int non_options_count = 0;
     103             :       const char *non_options[10];
     104           4 :       int unrecognized = 0;
     105             :       bool output;
     106           4 :       int argc = 0;
     107             :       const char *argv[10];
     108             : 
     109           4 :       argv[argc++] = "program";
     110           4 :       argv[argc++] = "-a";
     111           4 :       argv[argc++] = "foo";
     112           4 :       argv[argc++] = "bar";
     113           4 :       argv[argc] = NULL;
     114           4 :       optind = start;
     115           4 :       opterr = 1;
     116           4 :       getopt_loop (argc, argv, "ab",
     117             :                    &a_seen, &b_seen, &p_value, &q_value,
     118             :                    &non_options_count, non_options, &unrecognized, &output);
     119           4 :       ASSERT (a_seen == 1);
     120           4 :       ASSERT (b_seen == 0);
     121           4 :       ASSERT (p_value == NULL);
     122           4 :       ASSERT (q_value == NULL);
     123           4 :       ASSERT (non_options_count == 0);
     124           4 :       ASSERT (unrecognized == 0);
     125           4 :       ASSERT (optind == 2);
     126           4 :       ASSERT (!output);
     127             :     }
     128           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     129             :     {
     130           4 :       int a_seen = 0;
     131           4 :       int b_seen = 0;
     132           4 :       const char *p_value = NULL;
     133           4 :       const char *q_value = NULL;
     134           4 :       int non_options_count = 0;
     135             :       const char *non_options[10];
     136           4 :       int unrecognized = 0;
     137             :       bool output;
     138           4 :       int argc = 0;
     139             :       const char *argv[10];
     140             : 
     141           4 :       argv[argc++] = "program";
     142           4 :       argv[argc++] = "-b";
     143           4 :       argv[argc++] = "-a";
     144           4 :       argv[argc++] = "foo";
     145           4 :       argv[argc++] = "bar";
     146           4 :       argv[argc] = NULL;
     147           4 :       optind = start;
     148           4 :       opterr = 1;
     149           4 :       getopt_loop (argc, argv, "ab",
     150             :                    &a_seen, &b_seen, &p_value, &q_value,
     151             :                    &non_options_count, non_options, &unrecognized, &output);
     152           4 :       ASSERT (a_seen == 1);
     153           4 :       ASSERT (b_seen == 1);
     154           4 :       ASSERT (p_value == NULL);
     155           4 :       ASSERT (q_value == NULL);
     156           4 :       ASSERT (non_options_count == 0);
     157           4 :       ASSERT (unrecognized == 0);
     158           4 :       ASSERT (optind == 3);
     159           4 :       ASSERT (!output);
     160             :     }
     161           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     162             :     {
     163           4 :       int a_seen = 0;
     164           4 :       int b_seen = 0;
     165           4 :       const char *p_value = NULL;
     166           4 :       const char *q_value = NULL;
     167           4 :       int non_options_count = 0;
     168             :       const char *non_options[10];
     169           4 :       int unrecognized = 0;
     170             :       bool output;
     171           4 :       int argc = 0;
     172             :       const char *argv[10];
     173             : 
     174           4 :       argv[argc++] = "program";
     175           4 :       argv[argc++] = "-ba";
     176           4 :       argv[argc++] = "foo";
     177           4 :       argv[argc++] = "bar";
     178           4 :       argv[argc] = NULL;
     179           4 :       optind = start;
     180           4 :       opterr = 1;
     181           4 :       getopt_loop (argc, argv, "ab",
     182             :                    &a_seen, &b_seen, &p_value, &q_value,
     183             :                    &non_options_count, non_options, &unrecognized, &output);
     184           4 :       ASSERT (a_seen == 1);
     185           4 :       ASSERT (b_seen == 1);
     186           4 :       ASSERT (p_value == NULL);
     187           4 :       ASSERT (q_value == NULL);
     188           4 :       ASSERT (non_options_count == 0);
     189           4 :       ASSERT (unrecognized == 0);
     190           4 :       ASSERT (optind == 2);
     191           4 :       ASSERT (!output);
     192             :     }
     193           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     194             :     {
     195           4 :       int a_seen = 0;
     196           4 :       int b_seen = 0;
     197           4 :       const char *p_value = NULL;
     198           4 :       const char *q_value = NULL;
     199           4 :       int non_options_count = 0;
     200             :       const char *non_options[10];
     201           4 :       int unrecognized = 0;
     202             :       bool output;
     203           4 :       int argc = 0;
     204             :       const char *argv[10];
     205             : 
     206           4 :       argv[argc++] = "program";
     207           4 :       argv[argc++] = "-ab";
     208           4 :       argv[argc++] = "-a";
     209           4 :       argv[argc++] = "foo";
     210           4 :       argv[argc++] = "bar";
     211           4 :       argv[argc] = NULL;
     212           4 :       optind = start;
     213           4 :       opterr = 1;
     214           4 :       getopt_loop (argc, argv, "ab",
     215             :                    &a_seen, &b_seen, &p_value, &q_value,
     216             :                    &non_options_count, non_options, &unrecognized, &output);
     217           4 :       ASSERT (a_seen == 2);
     218           4 :       ASSERT (b_seen == 1);
     219           4 :       ASSERT (p_value == NULL);
     220           4 :       ASSERT (q_value == NULL);
     221           4 :       ASSERT (non_options_count == 0);
     222           4 :       ASSERT (unrecognized == 0);
     223           4 :       ASSERT (optind == 3);
     224           4 :       ASSERT (!output);
     225             :     }
     226             : 
     227             :   /* Test processing of options with arguments.  */
     228           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     229             :     {
     230           4 :       int a_seen = 0;
     231           4 :       int b_seen = 0;
     232           4 :       const char *p_value = NULL;
     233           4 :       const char *q_value = NULL;
     234           4 :       int non_options_count = 0;
     235             :       const char *non_options[10];
     236           4 :       int unrecognized = 0;
     237             :       bool output;
     238           4 :       int argc = 0;
     239             :       const char *argv[10];
     240             : 
     241           4 :       argv[argc++] = "program";
     242           4 :       argv[argc++] = "-pfoo";
     243           4 :       argv[argc++] = "bar";
     244           4 :       argv[argc] = NULL;
     245           4 :       optind = start;
     246           4 :       opterr = 1;
     247           4 :       getopt_loop (argc, argv, "p:q:",
     248             :                    &a_seen, &b_seen, &p_value, &q_value,
     249             :                    &non_options_count, non_options, &unrecognized, &output);
     250           4 :       ASSERT (a_seen == 0);
     251           4 :       ASSERT (b_seen == 0);
     252           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     253           4 :       ASSERT (q_value == NULL);
     254           4 :       ASSERT (non_options_count == 0);
     255           4 :       ASSERT (unrecognized == 0);
     256           4 :       ASSERT (optind == 2);
     257           4 :       ASSERT (!output);
     258             :     }
     259           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     260             :     {
     261           4 :       int a_seen = 0;
     262           4 :       int b_seen = 0;
     263           4 :       const char *p_value = NULL;
     264           4 :       const char *q_value = NULL;
     265           4 :       int non_options_count = 0;
     266             :       const char *non_options[10];
     267           4 :       int unrecognized = 0;
     268             :       bool output;
     269           4 :       int argc = 0;
     270             :       const char *argv[10];
     271             : 
     272           4 :       argv[argc++] = "program";
     273           4 :       argv[argc++] = "-p";
     274           4 :       argv[argc++] = "foo";
     275           4 :       argv[argc++] = "bar";
     276           4 :       argv[argc] = NULL;
     277           4 :       optind = start;
     278           4 :       opterr = 1;
     279           4 :       getopt_loop (argc, argv, "p:q:",
     280             :                    &a_seen, &b_seen, &p_value, &q_value,
     281             :                    &non_options_count, non_options, &unrecognized, &output);
     282           4 :       ASSERT (a_seen == 0);
     283           4 :       ASSERT (b_seen == 0);
     284           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     285           4 :       ASSERT (q_value == NULL);
     286           4 :       ASSERT (non_options_count == 0);
     287           4 :       ASSERT (unrecognized == 0);
     288           4 :       ASSERT (optind == 3);
     289           4 :       ASSERT (!output);
     290             :     }
     291           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     292             :     {
     293           4 :       int a_seen = 0;
     294           4 :       int b_seen = 0;
     295           4 :       const char *p_value = NULL;
     296           4 :       const char *q_value = NULL;
     297           4 :       int non_options_count = 0;
     298             :       const char *non_options[10];
     299           4 :       int unrecognized = 0;
     300             :       bool output;
     301           4 :       int argc = 0;
     302             :       const char *argv[10];
     303             : 
     304           4 :       argv[argc++] = "program";
     305           4 :       argv[argc++] = "-ab";
     306           4 :       argv[argc++] = "-q";
     307           4 :       argv[argc++] = "baz";
     308           4 :       argv[argc++] = "-pfoo";
     309           4 :       argv[argc++] = "bar";
     310           4 :       argv[argc] = NULL;
     311           4 :       optind = start;
     312           4 :       opterr = 1;
     313           4 :       getopt_loop (argc, argv, "abp:q:",
     314             :                    &a_seen, &b_seen, &p_value, &q_value,
     315             :                    &non_options_count, non_options, &unrecognized, &output);
     316           4 :       ASSERT (a_seen == 1);
     317           4 :       ASSERT (b_seen == 1);
     318           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     319           4 :       ASSERT (q_value != NULL && strcmp (q_value, "baz") == 0);
     320           4 :       ASSERT (non_options_count == 0);
     321           4 :       ASSERT (unrecognized == 0);
     322           4 :       ASSERT (optind == 5);
     323           4 :       ASSERT (!output);
     324             :     }
     325             : 
     326             : #if GNULIB_TEST_GETOPT_GNU
     327             :   /* Test processing of options with optional arguments.  */
     328           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     329             :     {
     330           4 :       int a_seen = 0;
     331           4 :       int b_seen = 0;
     332           4 :       const char *p_value = NULL;
     333           4 :       const char *q_value = NULL;
     334           4 :       int non_options_count = 0;
     335             :       const char *non_options[10];
     336           4 :       int unrecognized = 0;
     337             :       bool output;
     338           4 :       int argc = 0;
     339             :       const char *argv[10];
     340             : 
     341           4 :       argv[argc++] = "program";
     342           4 :       argv[argc++] = "-pfoo";
     343           4 :       argv[argc++] = "bar";
     344           4 :       argv[argc] = NULL;
     345           4 :       optind = start;
     346           4 :       opterr = 1;
     347           4 :       getopt_loop (argc, argv, "p::q::",
     348             :                    &a_seen, &b_seen, &p_value, &q_value,
     349             :                    &non_options_count, non_options, &unrecognized, &output);
     350           4 :       ASSERT (a_seen == 0);
     351           4 :       ASSERT (b_seen == 0);
     352           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     353           4 :       ASSERT (q_value == NULL);
     354           4 :       ASSERT (non_options_count == 0);
     355           4 :       ASSERT (unrecognized == 0);
     356           4 :       ASSERT (optind == 2);
     357           4 :       ASSERT (!output);
     358             :     }
     359           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     360             :     {
     361           4 :       int a_seen = 0;
     362           4 :       int b_seen = 0;
     363           4 :       const char *p_value = NULL;
     364           4 :       const char *q_value = NULL;
     365           4 :       int non_options_count = 0;
     366             :       const char *non_options[10];
     367           4 :       int unrecognized = 0;
     368             :       bool output;
     369           4 :       int argc = 0;
     370             :       const char *argv[10];
     371             : 
     372           4 :       argv[argc++] = "program";
     373           4 :       argv[argc++] = "-p";
     374           4 :       argv[argc++] = "foo";
     375           4 :       argv[argc++] = "bar";
     376           4 :       argv[argc] = NULL;
     377           4 :       optind = start;
     378           4 :       opterr = 1;
     379           4 :       getopt_loop (argc, argv, "p::q::",
     380             :                    &a_seen, &b_seen, &p_value, &q_value,
     381             :                    &non_options_count, non_options, &unrecognized, &output);
     382           4 :       ASSERT (a_seen == 0);
     383           4 :       ASSERT (b_seen == 0);
     384           4 :       ASSERT (p_value == NULL);
     385           4 :       ASSERT (q_value == NULL);
     386           4 :       ASSERT (non_options_count == 0);
     387           4 :       ASSERT (unrecognized == 0);
     388           4 :       ASSERT (optind == 2);
     389           4 :       ASSERT (!output);
     390             :     }
     391           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     392             :     {
     393           4 :       int a_seen = 0;
     394           4 :       int b_seen = 0;
     395           4 :       const char *p_value = NULL;
     396           4 :       const char *q_value = NULL;
     397           4 :       int non_options_count = 0;
     398             :       const char *non_options[10];
     399           4 :       int unrecognized = 0;
     400             :       bool output;
     401           4 :       int argc = 0;
     402             :       const char *argv[10];
     403             : 
     404           4 :       argv[argc++] = "program";
     405           4 :       argv[argc++] = "-p";
     406           4 :       argv[argc++] = "-a";
     407           4 :       argv[argc++] = "bar";
     408           4 :       argv[argc] = NULL;
     409           4 :       optind = start;
     410           4 :       opterr = 1;
     411           4 :       getopt_loop (argc, argv, "abp::q::",
     412             :                    &a_seen, &b_seen, &p_value, &q_value,
     413             :                    &non_options_count, non_options, &unrecognized, &output);
     414           4 :       ASSERT (a_seen == 1);
     415           4 :       ASSERT (b_seen == 0);
     416           4 :       ASSERT (p_value == NULL);
     417           4 :       ASSERT (q_value == NULL);
     418           4 :       ASSERT (non_options_count == 0);
     419           4 :       ASSERT (unrecognized == 0);
     420           4 :       ASSERT (optind == 3);
     421           4 :       ASSERT (!output);
     422             :     }
     423             : #endif /* GNULIB_TEST_GETOPT_GNU */
     424             : 
     425             :   /* Check that invalid options are recognized; and that both opterr
     426             :      and leading ':' can silence output.  */
     427           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     428             :     {
     429           4 :       int a_seen = 0;
     430           4 :       int b_seen = 0;
     431           4 :       const char *p_value = NULL;
     432           4 :       const char *q_value = NULL;
     433           4 :       int non_options_count = 0;
     434             :       const char *non_options[10];
     435           4 :       int unrecognized = 0;
     436             :       bool output;
     437           4 :       int argc = 0;
     438             :       const char *argv[10];
     439             : 
     440           4 :       argv[argc++] = "program";
     441           4 :       argv[argc++] = "-p";
     442           4 :       argv[argc++] = "foo";
     443           4 :       argv[argc++] = "-x";
     444           4 :       argv[argc++] = "-a";
     445           4 :       argv[argc++] = "bar";
     446           4 :       argv[argc] = NULL;
     447           4 :       optind = start;
     448           4 :       opterr = 42;
     449           4 :       getopt_loop (argc, argv, "abp:q:",
     450             :                    &a_seen, &b_seen, &p_value, &q_value,
     451             :                    &non_options_count, non_options, &unrecognized, &output);
     452           4 :       ASSERT (a_seen == 1);
     453           4 :       ASSERT (b_seen == 0);
     454           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     455           4 :       ASSERT (q_value == NULL);
     456           4 :       ASSERT (non_options_count == 0);
     457           4 :       ASSERT (unrecognized == 'x');
     458           4 :       ASSERT (optind == 5);
     459           4 :       ASSERT (output);
     460             :     }
     461           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     462             :     {
     463           4 :       int a_seen = 0;
     464           4 :       int b_seen = 0;
     465           4 :       const char *p_value = NULL;
     466           4 :       const char *q_value = NULL;
     467           4 :       int non_options_count = 0;
     468             :       const char *non_options[10];
     469           4 :       int unrecognized = 0;
     470             :       bool output;
     471           4 :       int argc = 0;
     472             :       const char *argv[10];
     473             : 
     474           4 :       argv[argc++] = "program";
     475           4 :       argv[argc++] = "-p";
     476           4 :       argv[argc++] = "foo";
     477           4 :       argv[argc++] = "-x";
     478           4 :       argv[argc++] = "-a";
     479           4 :       argv[argc++] = "bar";
     480           4 :       argv[argc] = NULL;
     481           4 :       optind = start;
     482           4 :       opterr = 0;
     483           4 :       getopt_loop (argc, argv, "abp:q:",
     484             :                    &a_seen, &b_seen, &p_value, &q_value,
     485             :                    &non_options_count, non_options, &unrecognized, &output);
     486           4 :       ASSERT (a_seen == 1);
     487           4 :       ASSERT (b_seen == 0);
     488           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     489           4 :       ASSERT (q_value == NULL);
     490           4 :       ASSERT (non_options_count == 0);
     491           4 :       ASSERT (unrecognized == 'x');
     492           4 :       ASSERT (optind == 5);
     493           4 :       ASSERT (!output);
     494             :     }
     495           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     496             :     {
     497           4 :       int a_seen = 0;
     498           4 :       int b_seen = 0;
     499           4 :       const char *p_value = NULL;
     500           4 :       const char *q_value = NULL;
     501           4 :       int non_options_count = 0;
     502             :       const char *non_options[10];
     503           4 :       int unrecognized = 0;
     504             :       bool output;
     505           4 :       int argc = 0;
     506             :       const char *argv[10];
     507             : 
     508           4 :       argv[argc++] = "program";
     509           4 :       argv[argc++] = "-p";
     510           4 :       argv[argc++] = "foo";
     511           4 :       argv[argc++] = "-x";
     512           4 :       argv[argc++] = "-a";
     513           4 :       argv[argc++] = "bar";
     514           4 :       argv[argc] = NULL;
     515           4 :       optind = start;
     516           4 :       opterr = 1;
     517           4 :       getopt_loop (argc, argv, ":abp:q:",
     518             :                    &a_seen, &b_seen, &p_value, &q_value,
     519             :                    &non_options_count, non_options, &unrecognized, &output);
     520           4 :       ASSERT (a_seen == 1);
     521           4 :       ASSERT (b_seen == 0);
     522           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     523           4 :       ASSERT (q_value == NULL);
     524           4 :       ASSERT (non_options_count == 0);
     525           4 :       ASSERT (unrecognized == 'x');
     526           4 :       ASSERT (optind == 5);
     527           4 :       ASSERT (!output);
     528             :     }
     529           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     530             :     {
     531           4 :       int a_seen = 0;
     532           4 :       int b_seen = 0;
     533           4 :       const char *p_value = NULL;
     534           4 :       const char *q_value = NULL;
     535           4 :       int non_options_count = 0;
     536             :       const char *non_options[10];
     537           4 :       int unrecognized = 0;
     538             :       bool output;
     539           4 :       int argc = 0;
     540             :       const char *argv[10];
     541             : 
     542           4 :       argv[argc++] = "program";
     543           4 :       argv[argc++] = "-p";
     544           4 :       argv[argc++] = "foo";
     545           4 :       argv[argc++] = "-:";
     546           4 :       argv[argc++] = "-a";
     547           4 :       argv[argc++] = "bar";
     548           4 :       argv[argc] = NULL;
     549           4 :       optind = start;
     550           4 :       opterr = 42;
     551           4 :       getopt_loop (argc, argv, "abp:q:",
     552             :                    &a_seen, &b_seen, &p_value, &q_value,
     553             :                    &non_options_count, non_options, &unrecognized, &output);
     554           4 :       ASSERT (a_seen == 1);
     555           4 :       ASSERT (b_seen == 0);
     556           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     557           4 :       ASSERT (q_value == NULL);
     558           4 :       ASSERT (non_options_count == 0);
     559           4 :       ASSERT (unrecognized == ':');
     560           4 :       ASSERT (optind == 5);
     561           4 :       ASSERT (output);
     562             :     }
     563           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     564             :     {
     565           4 :       int a_seen = 0;
     566           4 :       int b_seen = 0;
     567           4 :       const char *p_value = NULL;
     568           4 :       const char *q_value = NULL;
     569           4 :       int non_options_count = 0;
     570             :       const char *non_options[10];
     571           4 :       int unrecognized = 0;
     572             :       bool output;
     573           4 :       int argc = 0;
     574             :       const char *argv[10];
     575             : 
     576           4 :       argv[argc++] = "program";
     577           4 :       argv[argc++] = "-p";
     578           4 :       argv[argc++] = "foo";
     579           4 :       argv[argc++] = "-:";
     580           4 :       argv[argc++] = "-a";
     581           4 :       argv[argc++] = "bar";
     582           4 :       argv[argc] = NULL;
     583           4 :       optind = start;
     584           4 :       opterr = 0;
     585           4 :       getopt_loop (argc, argv, "abp:q:",
     586             :                    &a_seen, &b_seen, &p_value, &q_value,
     587             :                    &non_options_count, non_options, &unrecognized, &output);
     588           4 :       ASSERT (a_seen == 1);
     589           4 :       ASSERT (b_seen == 0);
     590           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     591           4 :       ASSERT (q_value == NULL);
     592           4 :       ASSERT (non_options_count == 0);
     593           4 :       ASSERT (unrecognized == ':');
     594           4 :       ASSERT (optind == 5);
     595           4 :       ASSERT (!output);
     596             :     }
     597           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     598             :     {
     599           4 :       int a_seen = 0;
     600           4 :       int b_seen = 0;
     601           4 :       const char *p_value = NULL;
     602           4 :       const char *q_value = NULL;
     603           4 :       int non_options_count = 0;
     604             :       const char *non_options[10];
     605           4 :       int unrecognized = 0;
     606             :       bool output;
     607           4 :       int argc = 0;
     608             :       const char *argv[10];
     609             : 
     610           4 :       argv[argc++] = "program";
     611           4 :       argv[argc++] = "-p";
     612           4 :       argv[argc++] = "foo";
     613           4 :       argv[argc++] = "-:";
     614           4 :       argv[argc++] = "-a";
     615           4 :       argv[argc++] = "bar";
     616           4 :       argv[argc] = NULL;
     617           4 :       optind = start;
     618           4 :       opterr = 1;
     619           4 :       getopt_loop (argc, argv, ":abp:q:",
     620             :                    &a_seen, &b_seen, &p_value, &q_value,
     621             :                    &non_options_count, non_options, &unrecognized, &output);
     622           4 :       ASSERT (a_seen == 1);
     623           4 :       ASSERT (b_seen == 0);
     624           4 :       ASSERT (p_value != NULL && strcmp (p_value, "foo") == 0);
     625           4 :       ASSERT (q_value == NULL);
     626           4 :       ASSERT (non_options_count == 0);
     627           4 :       ASSERT (unrecognized == ':');
     628           4 :       ASSERT (optind == 5);
     629           4 :       ASSERT (!output);
     630             :     }
     631             : 
     632             :   /* Check for missing argument behavior.  */
     633           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     634             :     {
     635           4 :       int a_seen = 0;
     636           4 :       int b_seen = 0;
     637           4 :       const char *p_value = NULL;
     638           4 :       const char *q_value = NULL;
     639           4 :       int non_options_count = 0;
     640             :       const char *non_options[10];
     641           4 :       int unrecognized = 0;
     642             :       bool output;
     643           4 :       int argc = 0;
     644             :       const char *argv[10];
     645             : 
     646           4 :       argv[argc++] = "program";
     647           4 :       argv[argc++] = "-ap";
     648           4 :       argv[argc] = NULL;
     649           4 :       optind = start;
     650           4 :       opterr = 1;
     651           4 :       getopt_loop (argc, argv, "abp:q:",
     652             :                    &a_seen, &b_seen, &p_value, &q_value,
     653             :                    &non_options_count, non_options, &unrecognized, &output);
     654           4 :       ASSERT (a_seen == 1);
     655           4 :       ASSERT (b_seen == 0);
     656           4 :       ASSERT (p_value == NULL);
     657           4 :       ASSERT (q_value == NULL);
     658           4 :       ASSERT (non_options_count == 0);
     659           4 :       ASSERT (unrecognized == 'p');
     660           4 :       ASSERT (optind == 2);
     661           4 :       ASSERT (output);
     662             :     }
     663           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     664             :     {
     665           4 :       int a_seen = 0;
     666           4 :       int b_seen = 0;
     667           4 :       const char *p_value = NULL;
     668           4 :       const char *q_value = NULL;
     669           4 :       int non_options_count = 0;
     670             :       const char *non_options[10];
     671           4 :       int unrecognized = 0;
     672             :       bool output;
     673           4 :       int argc = 0;
     674             :       const char *argv[10];
     675             : 
     676           4 :       argv[argc++] = "program";
     677           4 :       argv[argc++] = "-ap";
     678           4 :       argv[argc] = NULL;
     679           4 :       optind = start;
     680           4 :       opterr = 0;
     681           4 :       getopt_loop (argc, argv, "abp:q:",
     682             :                    &a_seen, &b_seen, &p_value, &q_value,
     683             :                    &non_options_count, non_options, &unrecognized, &output);
     684           4 :       ASSERT (a_seen == 1);
     685           4 :       ASSERT (b_seen == 0);
     686           4 :       ASSERT (p_value == NULL);
     687           4 :       ASSERT (q_value == NULL);
     688           4 :       ASSERT (non_options_count == 0);
     689           4 :       ASSERT (unrecognized == 'p');
     690           4 :       ASSERT (optind == 2);
     691           4 :       ASSERT (!output);
     692             :     }
     693           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     694             :     {
     695           4 :       int a_seen = 0;
     696           4 :       int b_seen = 0;
     697           4 :       const char *p_value = NULL;
     698           4 :       const char *q_value = NULL;
     699           4 :       int non_options_count = 0;
     700             :       const char *non_options[10];
     701           4 :       int unrecognized = 0;
     702             :       bool output;
     703           4 :       int argc = 0;
     704             :       const char *argv[10];
     705             : 
     706           4 :       argv[argc++] = "program";
     707           4 :       argv[argc++] = "-ap";
     708           4 :       argv[argc] = NULL;
     709           4 :       optind = start;
     710           4 :       opterr = 1;
     711           4 :       getopt_loop (argc, argv, ":abp:q:",
     712             :                    &a_seen, &b_seen, &p_value, &q_value,
     713             :                    &non_options_count, non_options, &unrecognized, &output);
     714           4 :       ASSERT (a_seen == 1);
     715           4 :       ASSERT (b_seen == 0);
     716           4 :       ASSERT (p_value == NULL);
     717           4 :       ASSERT (q_value == NULL);
     718           4 :       ASSERT (non_options_count == 0);
     719           4 :       ASSERT (unrecognized == 'p');
     720           4 :       ASSERT (optind == 2);
     721           4 :       ASSERT (!output);
     722             :     }
     723             : 
     724             :   /* Check that by default, non-options arguments are moved to the end.  */
     725           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     726             :     {
     727           4 :       int a_seen = 0;
     728           4 :       int b_seen = 0;
     729           4 :       const char *p_value = NULL;
     730           4 :       const char *q_value = NULL;
     731           4 :       int non_options_count = 0;
     732             :       const char *non_options[10];
     733           4 :       int unrecognized = 0;
     734             :       bool output;
     735           4 :       int argc = 0;
     736             :       const char *argv[10];
     737             : 
     738           4 :       argv[argc++] = "program";
     739           4 :       argv[argc++] = "donald";
     740           4 :       argv[argc++] = "-p";
     741           4 :       argv[argc++] = "billy";
     742           4 :       argv[argc++] = "duck";
     743           4 :       argv[argc++] = "-a";
     744           4 :       argv[argc++] = "bar";
     745           4 :       argv[argc] = NULL;
     746           4 :       optind = start;
     747           4 :       opterr = 1;
     748           4 :       getopt_loop (argc, argv, "abp:q:",
     749             :                    &a_seen, &b_seen, &p_value, &q_value,
     750             :                    &non_options_count, non_options, &unrecognized, &output);
     751           4 :       if (posixly)
     752             :         {
     753           2 :           ASSERT (strcmp (argv[0], "program") == 0);
     754           2 :           ASSERT (strcmp (argv[1], "donald") == 0);
     755           2 :           ASSERT (strcmp (argv[2], "-p") == 0);
     756           2 :           ASSERT (strcmp (argv[3], "billy") == 0);
     757           2 :           ASSERT (strcmp (argv[4], "duck") == 0);
     758           2 :           ASSERT (strcmp (argv[5], "-a") == 0);
     759           2 :           ASSERT (strcmp (argv[6], "bar") == 0);
     760           2 :           ASSERT (argv[7] == NULL);
     761           2 :           ASSERT (a_seen == 0);
     762           2 :           ASSERT (b_seen == 0);
     763           2 :           ASSERT (p_value == NULL);
     764           2 :           ASSERT (q_value == NULL);
     765           2 :           ASSERT (non_options_count == 0);
     766           2 :           ASSERT (unrecognized == 0);
     767           2 :           ASSERT (optind == 1);
     768           2 :           ASSERT (!output);
     769             :         }
     770             :       else
     771             :         {
     772           2 :           ASSERT (strcmp (argv[0], "program") == 0);
     773           2 :           ASSERT (strcmp (argv[1], "-p") == 0);
     774           2 :           ASSERT (strcmp (argv[2], "billy") == 0);
     775           2 :           ASSERT (strcmp (argv[3], "-a") == 0);
     776           2 :           ASSERT (strcmp (argv[4], "donald") == 0);
     777           2 :           ASSERT (strcmp (argv[5], "duck") == 0);
     778           2 :           ASSERT (strcmp (argv[6], "bar") == 0);
     779           2 :           ASSERT (argv[7] == NULL);
     780           2 :           ASSERT (a_seen == 1);
     781           2 :           ASSERT (b_seen == 0);
     782           2 :           ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
     783           2 :           ASSERT (q_value == NULL);
     784           2 :           ASSERT (non_options_count == 0);
     785           2 :           ASSERT (unrecognized == 0);
     786           2 :           ASSERT (optind == 4);
     787           2 :           ASSERT (!output);
     788             :         }
     789             :     }
     790             : 
     791             :   /* Check that '--' ends the argument processing.  */
     792           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     793             :     {
     794           4 :       int a_seen = 0;
     795           4 :       int b_seen = 0;
     796           4 :       const char *p_value = NULL;
     797           4 :       const char *q_value = NULL;
     798           4 :       int non_options_count = 0;
     799             :       const char *non_options[10];
     800           4 :       int unrecognized = 0;
     801             :       bool output;
     802           4 :       int argc = 0;
     803             :       const char *argv[20];
     804             : 
     805           4 :       argv[argc++] = "program";
     806           4 :       argv[argc++] = "donald";
     807           4 :       argv[argc++] = "-p";
     808           4 :       argv[argc++] = "billy";
     809           4 :       argv[argc++] = "duck";
     810           4 :       argv[argc++] = "-a";
     811           4 :       argv[argc++] = "--";
     812           4 :       argv[argc++] = "-b";
     813           4 :       argv[argc++] = "foo";
     814           4 :       argv[argc++] = "-q";
     815           4 :       argv[argc++] = "johnny";
     816           4 :       argv[argc++] = "bar";
     817           4 :       argv[argc] = NULL;
     818           4 :       optind = start;
     819           4 :       opterr = 1;
     820           4 :       getopt_loop (argc, argv, "abp:q:",
     821             :                    &a_seen, &b_seen, &p_value, &q_value,
     822             :                    &non_options_count, non_options, &unrecognized, &output);
     823           4 :       if (posixly)
     824             :         {
     825           2 :           ASSERT (strcmp (argv[0], "program") == 0);
     826           2 :           ASSERT (strcmp (argv[1], "donald") == 0);
     827           2 :           ASSERT (strcmp (argv[2], "-p") == 0);
     828           2 :           ASSERT (strcmp (argv[3], "billy") == 0);
     829           2 :           ASSERT (strcmp (argv[4], "duck") == 0);
     830           2 :           ASSERT (strcmp (argv[5], "-a") == 0);
     831           2 :           ASSERT (strcmp (argv[6], "--") == 0);
     832           2 :           ASSERT (strcmp (argv[7], "-b") == 0);
     833           2 :           ASSERT (strcmp (argv[8], "foo") == 0);
     834           2 :           ASSERT (strcmp (argv[9], "-q") == 0);
     835           2 :           ASSERT (strcmp (argv[10], "johnny") == 0);
     836           2 :           ASSERT (strcmp (argv[11], "bar") == 0);
     837           2 :           ASSERT (argv[12] == NULL);
     838           2 :           ASSERT (a_seen == 0);
     839           2 :           ASSERT (b_seen == 0);
     840           2 :           ASSERT (p_value == NULL);
     841           2 :           ASSERT (q_value == NULL);
     842           2 :           ASSERT (non_options_count == 0);
     843           2 :           ASSERT (unrecognized == 0);
     844           2 :           ASSERT (optind == 1);
     845           2 :           ASSERT (!output);
     846             :         }
     847             :       else
     848             :         {
     849           2 :           ASSERT (strcmp (argv[0], "program") == 0);
     850           2 :           ASSERT (strcmp (argv[1], "-p") == 0);
     851           2 :           ASSERT (strcmp (argv[2], "billy") == 0);
     852           2 :           ASSERT (strcmp (argv[3], "-a") == 0);
     853           2 :           ASSERT (strcmp (argv[4], "--") == 0);
     854           2 :           ASSERT (strcmp (argv[5], "donald") == 0);
     855           2 :           ASSERT (strcmp (argv[6], "duck") == 0);
     856           2 :           ASSERT (strcmp (argv[7], "-b") == 0);
     857           2 :           ASSERT (strcmp (argv[8], "foo") == 0);
     858           2 :           ASSERT (strcmp (argv[9], "-q") == 0);
     859           2 :           ASSERT (strcmp (argv[10], "johnny") == 0);
     860           2 :           ASSERT (strcmp (argv[11], "bar") == 0);
     861           2 :           ASSERT (argv[12] == NULL);
     862           2 :           ASSERT (a_seen == 1);
     863           2 :           ASSERT (b_seen == 0);
     864           2 :           ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
     865           2 :           ASSERT (q_value == NULL);
     866           2 :           ASSERT (non_options_count == 0);
     867           2 :           ASSERT (unrecognized == 0);
     868           2 :           ASSERT (optind == 5);
     869           2 :           ASSERT (!output);
     870             :         }
     871             :     }
     872             : 
     873             : #if GNULIB_TEST_GETOPT_GNU
     874             :   /* Check that the '-' flag causes non-options to be returned in order.  */
     875           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     876             :     {
     877           4 :       int a_seen = 0;
     878           4 :       int b_seen = 0;
     879           4 :       const char *p_value = NULL;
     880           4 :       const char *q_value = NULL;
     881           4 :       int non_options_count = 0;
     882             :       const char *non_options[10];
     883           4 :       int unrecognized = 0;
     884             :       bool output;
     885           4 :       int argc = 0;
     886             :       const char *argv[10];
     887             : 
     888           4 :       argv[argc++] = "program";
     889           4 :       argv[argc++] = "donald";
     890           4 :       argv[argc++] = "-p";
     891           4 :       argv[argc++] = "billy";
     892           4 :       argv[argc++] = "duck";
     893           4 :       argv[argc++] = "-a";
     894           4 :       argv[argc++] = "bar";
     895           4 :       argv[argc] = NULL;
     896           4 :       optind = start;
     897           4 :       opterr = 1;
     898           4 :       getopt_loop (argc, argv, "-abp:q:",
     899             :                    &a_seen, &b_seen, &p_value, &q_value,
     900             :                    &non_options_count, non_options, &unrecognized, &output);
     901           4 :       ASSERT (strcmp (argv[0], "program") == 0);
     902           4 :       ASSERT (strcmp (argv[1], "donald") == 0);
     903           4 :       ASSERT (strcmp (argv[2], "-p") == 0);
     904           4 :       ASSERT (strcmp (argv[3], "billy") == 0);
     905           4 :       ASSERT (strcmp (argv[4], "duck") == 0);
     906           4 :       ASSERT (strcmp (argv[5], "-a") == 0);
     907           4 :       ASSERT (strcmp (argv[6], "bar") == 0);
     908           4 :       ASSERT (argv[7] == NULL);
     909           4 :       ASSERT (a_seen == 1);
     910           4 :       ASSERT (b_seen == 0);
     911           4 :       ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
     912           4 :       ASSERT (q_value == NULL);
     913           4 :       ASSERT (non_options_count == 3);
     914           4 :       ASSERT (strcmp (non_options[0], "donald") == 0);
     915           4 :       ASSERT (strcmp (non_options[1], "duck") == 0);
     916           4 :       ASSERT (strcmp (non_options[2], "bar") == 0);
     917           4 :       ASSERT (unrecognized == 0);
     918           4 :       ASSERT (optind == 7);
     919           4 :       ASSERT (!output);
     920             :     }
     921             : 
     922             :   /* Check that '--' ends the argument processing.  */
     923           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     924             :     {
     925           4 :       int a_seen = 0;
     926           4 :       int b_seen = 0;
     927           4 :       const char *p_value = NULL;
     928           4 :       const char *q_value = NULL;
     929           4 :       int non_options_count = 0;
     930             :       const char *non_options[10];
     931           4 :       int unrecognized = 0;
     932             :       bool output;
     933           4 :       int argc = 0;
     934             :       const char *argv[20];
     935             : 
     936           4 :       argv[argc++] = "program";
     937           4 :       argv[argc++] = "donald";
     938           4 :       argv[argc++] = "-p";
     939           4 :       argv[argc++] = "billy";
     940           4 :       argv[argc++] = "duck";
     941           4 :       argv[argc++] = "-a";
     942           4 :       argv[argc++] = "--";
     943           4 :       argv[argc++] = "-b";
     944           4 :       argv[argc++] = "foo";
     945           4 :       argv[argc++] = "-q";
     946           4 :       argv[argc++] = "johnny";
     947           4 :       argv[argc++] = "bar";
     948           4 :       argv[argc] = NULL;
     949           4 :       optind = start;
     950           4 :       opterr = 1;
     951           4 :       getopt_loop (argc, argv, "-abp:q:",
     952             :                    &a_seen, &b_seen, &p_value, &q_value,
     953             :                    &non_options_count, non_options, &unrecognized, &output);
     954           4 :       ASSERT (strcmp (argv[0], "program") == 0);
     955           4 :       ASSERT (strcmp (argv[1], "donald") == 0);
     956           4 :       ASSERT (strcmp (argv[2], "-p") == 0);
     957           4 :       ASSERT (strcmp (argv[3], "billy") == 0);
     958           4 :       ASSERT (strcmp (argv[4], "duck") == 0);
     959           4 :       ASSERT (strcmp (argv[5], "-a") == 0);
     960           4 :       ASSERT (strcmp (argv[6], "--") == 0);
     961           4 :       ASSERT (strcmp (argv[7], "-b") == 0);
     962           4 :       ASSERT (strcmp (argv[8], "foo") == 0);
     963           4 :       ASSERT (strcmp (argv[9], "-q") == 0);
     964           4 :       ASSERT (strcmp (argv[10], "johnny") == 0);
     965           4 :       ASSERT (strcmp (argv[11], "bar") == 0);
     966           4 :       ASSERT (argv[12] == NULL);
     967           4 :       ASSERT (a_seen == 1);
     968           4 :       ASSERT (b_seen == 0);
     969           4 :       ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
     970           4 :       ASSERT (q_value == NULL);
     971           4 :       ASSERT (!output);
     972           4 :       if (non_options_count == 2)
     973             :         {
     974             :           /* glibc behaviour.  */
     975           4 :           ASSERT (non_options_count == 2);
     976           4 :           ASSERT (strcmp (non_options[0], "donald") == 0);
     977           4 :           ASSERT (strcmp (non_options[1], "duck") == 0);
     978           4 :           ASSERT (unrecognized == 0);
     979           4 :           ASSERT (optind == 7);
     980             :         }
     981             :       else
     982             :         {
     983             :           /* Another valid behaviour.  */
     984           0 :           ASSERT (non_options_count == 7);
     985           0 :           ASSERT (strcmp (non_options[0], "donald") == 0);
     986           0 :           ASSERT (strcmp (non_options[1], "duck") == 0);
     987           0 :           ASSERT (strcmp (non_options[2], "-b") == 0);
     988           0 :           ASSERT (strcmp (non_options[3], "foo") == 0);
     989           0 :           ASSERT (strcmp (non_options[4], "-q") == 0);
     990           0 :           ASSERT (strcmp (non_options[5], "johnny") == 0);
     991           0 :           ASSERT (strcmp (non_options[6], "bar") == 0);
     992           0 :           ASSERT (unrecognized == 0);
     993           0 :           ASSERT (optind == 12);
     994             :         }
     995             :     }
     996             : 
     997             :   /* Check that the '-' flag has to come first.  */
     998           6 :   for (start = OPTIND_MIN; start <= 1; start++)
     999             :     {
    1000           4 :       int a_seen = 0;
    1001           4 :       int b_seen = 0;
    1002           4 :       const char *p_value = NULL;
    1003           4 :       const char *q_value = NULL;
    1004           4 :       int non_options_count = 0;
    1005             :       const char *non_options[10];
    1006           4 :       int unrecognized = 0;
    1007             :       bool output;
    1008           4 :       int argc = 0;
    1009             :       const char *argv[10];
    1010             : 
    1011           4 :       argv[argc++] = "program";
    1012           4 :       argv[argc++] = "donald";
    1013           4 :       argv[argc++] = "-p";
    1014           4 :       argv[argc++] = "billy";
    1015           4 :       argv[argc++] = "duck";
    1016           4 :       argv[argc++] = "-a";
    1017           4 :       argv[argc++] = "bar";
    1018           4 :       argv[argc] = NULL;
    1019           4 :       optind = start;
    1020           4 :       opterr = 1;
    1021           4 :       getopt_loop (argc, argv, "abp:q:-",
    1022             :                    &a_seen, &b_seen, &p_value, &q_value,
    1023             :                    &non_options_count, non_options, &unrecognized, &output);
    1024           4 :       if (posixly)
    1025             :         {
    1026           2 :           ASSERT (strcmp (argv[0], "program") == 0);
    1027           2 :           ASSERT (strcmp (argv[1], "donald") == 0);
    1028           2 :           ASSERT (strcmp (argv[2], "-p") == 0);
    1029           2 :           ASSERT (strcmp (argv[3], "billy") == 0);
    1030           2 :           ASSERT (strcmp (argv[4], "duck") == 0);
    1031           2 :           ASSERT (strcmp (argv[5], "-a") == 0);
    1032           2 :           ASSERT (strcmp (argv[6], "bar") == 0);
    1033           2 :           ASSERT (argv[7] == NULL);
    1034           2 :           ASSERT (a_seen == 0);
    1035           2 :           ASSERT (b_seen == 0);
    1036           2 :           ASSERT (p_value == NULL);
    1037           2 :           ASSERT (q_value == NULL);
    1038           2 :           ASSERT (non_options_count == 0);
    1039           2 :           ASSERT (unrecognized == 0);
    1040           2 :           ASSERT (optind == 1);
    1041           2 :           ASSERT (!output);
    1042             :         }
    1043             :       else
    1044             :         {
    1045           2 :           ASSERT (strcmp (argv[0], "program") == 0);
    1046           2 :           ASSERT (strcmp (argv[1], "-p") == 0);
    1047           2 :           ASSERT (strcmp (argv[2], "billy") == 0);
    1048           2 :           ASSERT (strcmp (argv[3], "-a") == 0);
    1049           2 :           ASSERT (strcmp (argv[4], "donald") == 0);
    1050           2 :           ASSERT (strcmp (argv[5], "duck") == 0);
    1051           2 :           ASSERT (strcmp (argv[6], "bar") == 0);
    1052           2 :           ASSERT (argv[7] == NULL);
    1053           2 :           ASSERT (a_seen == 1);
    1054           2 :           ASSERT (b_seen == 0);
    1055           2 :           ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
    1056           2 :           ASSERT (q_value == NULL);
    1057           2 :           ASSERT (non_options_count == 0);
    1058           2 :           ASSERT (unrecognized == 0);
    1059           2 :           ASSERT (optind == 4);
    1060           2 :           ASSERT (!output);
    1061             :         }
    1062             :     }
    1063             : 
    1064             :   /* Check that the '+' flag causes the first non-option to terminate the
    1065             :      loop.  */
    1066           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1067             :     {
    1068           4 :       int a_seen = 0;
    1069           4 :       int b_seen = 0;
    1070           4 :       const char *p_value = NULL;
    1071           4 :       const char *q_value = NULL;
    1072           4 :       int non_options_count = 0;
    1073             :       const char *non_options[10];
    1074           4 :       int unrecognized = 0;
    1075             :       bool output;
    1076           4 :       int argc = 0;
    1077             :       const char *argv[10];
    1078             : 
    1079           4 :       argv[argc++] = "program";
    1080           4 :       argv[argc++] = "donald";
    1081           4 :       argv[argc++] = "-p";
    1082           4 :       argv[argc++] = "billy";
    1083           4 :       argv[argc++] = "duck";
    1084           4 :       argv[argc++] = "-a";
    1085           4 :       argv[argc++] = "bar";
    1086           4 :       argv[argc] = NULL;
    1087           4 :       optind = start;
    1088           4 :       opterr = 1;
    1089           4 :       getopt_loop (argc, argv, "+abp:q:",
    1090             :                    &a_seen, &b_seen, &p_value, &q_value,
    1091             :                    &non_options_count, non_options, &unrecognized, &output);
    1092           4 :       ASSERT (strcmp (argv[0], "program") == 0);
    1093           4 :       ASSERT (strcmp (argv[1], "donald") == 0);
    1094           4 :       ASSERT (strcmp (argv[2], "-p") == 0);
    1095           4 :       ASSERT (strcmp (argv[3], "billy") == 0);
    1096           4 :       ASSERT (strcmp (argv[4], "duck") == 0);
    1097           4 :       ASSERT (strcmp (argv[5], "-a") == 0);
    1098           4 :       ASSERT (strcmp (argv[6], "bar") == 0);
    1099           4 :       ASSERT (argv[7] == NULL);
    1100           4 :       ASSERT (a_seen == 0);
    1101           4 :       ASSERT (b_seen == 0);
    1102           4 :       ASSERT (p_value == NULL);
    1103           4 :       ASSERT (q_value == NULL);
    1104           4 :       ASSERT (non_options_count == 0);
    1105           4 :       ASSERT (unrecognized == 0);
    1106           4 :       ASSERT (optind == 1);
    1107           4 :       ASSERT (!output);
    1108             :     }
    1109           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1110             :     {
    1111           4 :       int a_seen = 0;
    1112           4 :       int b_seen = 0;
    1113           4 :       const char *p_value = NULL;
    1114           4 :       const char *q_value = NULL;
    1115           4 :       int non_options_count = 0;
    1116             :       const char *non_options[10];
    1117           4 :       int unrecognized = 0;
    1118             :       bool output;
    1119           4 :       int argc = 0;
    1120             :       const char *argv[10];
    1121             : 
    1122           4 :       argv[argc++] = "program";
    1123           4 :       argv[argc++] = "-+";
    1124           4 :       argv[argc] = NULL;
    1125           4 :       optind = start;
    1126           4 :       getopt_loop (argc, argv, "+abp:q:",
    1127             :                    &a_seen, &b_seen, &p_value, &q_value,
    1128             :                    &non_options_count, non_options, &unrecognized, &output);
    1129           4 :       ASSERT (a_seen == 0);
    1130           4 :       ASSERT (b_seen == 0);
    1131           4 :       ASSERT (p_value == NULL);
    1132           4 :       ASSERT (q_value == NULL);
    1133           4 :       ASSERT (non_options_count == 0);
    1134           4 :       ASSERT (unrecognized == '+');
    1135           4 :       ASSERT (optind == 2);
    1136           4 :       ASSERT (output);
    1137             :     }
    1138             : 
    1139             :   /* Check that '--' ends the argument processing.  */
    1140           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1141             :     {
    1142           4 :       int a_seen = 0;
    1143           4 :       int b_seen = 0;
    1144           4 :       const char *p_value = NULL;
    1145           4 :       const char *q_value = NULL;
    1146           4 :       int non_options_count = 0;
    1147             :       const char *non_options[10];
    1148           4 :       int unrecognized = 0;
    1149             :       bool output;
    1150           4 :       int argc = 0;
    1151             :       const char *argv[20];
    1152             : 
    1153           4 :       argv[argc++] = "program";
    1154           4 :       argv[argc++] = "donald";
    1155           4 :       argv[argc++] = "-p";
    1156           4 :       argv[argc++] = "billy";
    1157           4 :       argv[argc++] = "duck";
    1158           4 :       argv[argc++] = "-a";
    1159           4 :       argv[argc++] = "--";
    1160           4 :       argv[argc++] = "-b";
    1161           4 :       argv[argc++] = "foo";
    1162           4 :       argv[argc++] = "-q";
    1163           4 :       argv[argc++] = "johnny";
    1164           4 :       argv[argc++] = "bar";
    1165           4 :       argv[argc] = NULL;
    1166           4 :       optind = start;
    1167           4 :       opterr = 1;
    1168           4 :       getopt_loop (argc, argv, "+abp:q:",
    1169             :                    &a_seen, &b_seen, &p_value, &q_value,
    1170             :                    &non_options_count, non_options, &unrecognized, &output);
    1171           4 :       ASSERT (strcmp (argv[0], "program") == 0);
    1172           4 :       ASSERT (strcmp (argv[1], "donald") == 0);
    1173           4 :       ASSERT (strcmp (argv[2], "-p") == 0);
    1174           4 :       ASSERT (strcmp (argv[3], "billy") == 0);
    1175           4 :       ASSERT (strcmp (argv[4], "duck") == 0);
    1176           4 :       ASSERT (strcmp (argv[5], "-a") == 0);
    1177           4 :       ASSERT (strcmp (argv[6], "--") == 0);
    1178           4 :       ASSERT (strcmp (argv[7], "-b") == 0);
    1179           4 :       ASSERT (strcmp (argv[8], "foo") == 0);
    1180           4 :       ASSERT (strcmp (argv[9], "-q") == 0);
    1181           4 :       ASSERT (strcmp (argv[10], "johnny") == 0);
    1182           4 :       ASSERT (strcmp (argv[11], "bar") == 0);
    1183           4 :       ASSERT (argv[12] == NULL);
    1184           4 :       ASSERT (a_seen == 0);
    1185           4 :       ASSERT (b_seen == 0);
    1186           4 :       ASSERT (p_value == NULL);
    1187           4 :       ASSERT (q_value == NULL);
    1188           4 :       ASSERT (non_options_count == 0);
    1189           4 :       ASSERT (unrecognized == 0);
    1190           4 :       ASSERT (optind == 1);
    1191           4 :       ASSERT (!output);
    1192             :     }
    1193             : #endif /* GNULIB_TEST_GETOPT_GNU */
    1194             : 
    1195             :   /* Check that the '+' flag has to come first.  */
    1196           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1197             :     {
    1198           4 :       int a_seen = 0;
    1199           4 :       int b_seen = 0;
    1200           4 :       const char *p_value = NULL;
    1201           4 :       const char *q_value = NULL;
    1202           4 :       int non_options_count = 0;
    1203             :       const char *non_options[10];
    1204           4 :       int unrecognized = 0;
    1205             :       bool output;
    1206           4 :       int argc = 0;
    1207             :       const char *argv[10];
    1208             : 
    1209           4 :       argv[argc++] = "program";
    1210           4 :       argv[argc++] = "donald";
    1211           4 :       argv[argc++] = "-p";
    1212           4 :       argv[argc++] = "billy";
    1213           4 :       argv[argc++] = "duck";
    1214           4 :       argv[argc++] = "-a";
    1215           4 :       argv[argc++] = "bar";
    1216           4 :       argv[argc] = NULL;
    1217           4 :       optind = start;
    1218           4 :       opterr = 1;
    1219           4 :       getopt_loop (argc, argv, "abp:q:+",
    1220             :                    &a_seen, &b_seen, &p_value, &q_value,
    1221             :                    &non_options_count, non_options, &unrecognized, &output);
    1222           4 :       if (posixly)
    1223             :         {
    1224           2 :           ASSERT (strcmp (argv[0], "program") == 0);
    1225           2 :           ASSERT (strcmp (argv[1], "donald") == 0);
    1226           2 :           ASSERT (strcmp (argv[2], "-p") == 0);
    1227           2 :           ASSERT (strcmp (argv[3], "billy") == 0);
    1228           2 :           ASSERT (strcmp (argv[4], "duck") == 0);
    1229           2 :           ASSERT (strcmp (argv[5], "-a") == 0);
    1230           2 :           ASSERT (strcmp (argv[6], "bar") == 0);
    1231           2 :           ASSERT (argv[7] == NULL);
    1232           2 :           ASSERT (a_seen == 0);
    1233           2 :           ASSERT (b_seen == 0);
    1234           2 :           ASSERT (p_value == NULL);
    1235           2 :           ASSERT (q_value == NULL);
    1236           2 :           ASSERT (non_options_count == 0);
    1237           2 :           ASSERT (unrecognized == 0);
    1238           2 :           ASSERT (optind == 1);
    1239           2 :           ASSERT (!output);
    1240             :         }
    1241             :       else
    1242             :         {
    1243           2 :           ASSERT (strcmp (argv[0], "program") == 0);
    1244           2 :           ASSERT (strcmp (argv[1], "-p") == 0);
    1245           2 :           ASSERT (strcmp (argv[2], "billy") == 0);
    1246           2 :           ASSERT (strcmp (argv[3], "-a") == 0);
    1247           2 :           ASSERT (strcmp (argv[4], "donald") == 0);
    1248           2 :           ASSERT (strcmp (argv[5], "duck") == 0);
    1249           2 :           ASSERT (strcmp (argv[6], "bar") == 0);
    1250           2 :           ASSERT (argv[7] == NULL);
    1251           2 :           ASSERT (a_seen == 1);
    1252           2 :           ASSERT (b_seen == 0);
    1253           2 :           ASSERT (p_value != NULL && strcmp (p_value, "billy") == 0);
    1254           2 :           ASSERT (q_value == NULL);
    1255           2 :           ASSERT (non_options_count == 0);
    1256           2 :           ASSERT (unrecognized == 0);
    1257           2 :           ASSERT (optind == 4);
    1258           2 :           ASSERT (!output);
    1259             :         }
    1260             :     }
    1261             : 
    1262             : #if GNULIB_TEST_GETOPT_GNU
    1263             :   /* If GNU extensions are supported, require compliance with POSIX
    1264             :      interpretation on leading '+' behavior.
    1265             :      http://austingroupbugs.net/view.php?id=191  */
    1266           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1267             :     {
    1268           4 :       int a_seen = 0;
    1269           4 :       int b_seen = 0;
    1270           4 :       const char *p_value = NULL;
    1271           4 :       const char *q_value = NULL;
    1272           4 :       int non_options_count = 0;
    1273             :       const char *non_options[10];
    1274           4 :       int unrecognized = 0;
    1275             :       bool output;
    1276           4 :       int argc = 0;
    1277             :       const char *argv[10];
    1278             : 
    1279           4 :       argv[argc++] = "program";
    1280           4 :       argv[argc++] = "donald";
    1281           4 :       argv[argc++] = "-p";
    1282           4 :       argv[argc++] = "billy";
    1283           4 :       argv[argc++] = "duck";
    1284           4 :       argv[argc++] = "-a";
    1285           4 :       argv[argc++] = "bar";
    1286           4 :       argv[argc] = NULL;
    1287           4 :       optind = start;
    1288           4 :       opterr = 1;
    1289           4 :       getopt_loop (argc, argv, "+:abp:q:",
    1290             :                    &a_seen, &b_seen, &p_value, &q_value,
    1291             :                    &non_options_count, non_options, &unrecognized, &output);
    1292           4 :       ASSERT (strcmp (argv[0], "program") == 0);
    1293           4 :       ASSERT (strcmp (argv[1], "donald") == 0);
    1294           4 :       ASSERT (strcmp (argv[2], "-p") == 0);
    1295           4 :       ASSERT (strcmp (argv[3], "billy") == 0);
    1296           4 :       ASSERT (strcmp (argv[4], "duck") == 0);
    1297           4 :       ASSERT (strcmp (argv[5], "-a") == 0);
    1298           4 :       ASSERT (strcmp (argv[6], "bar") == 0);
    1299           4 :       ASSERT (argv[7] == NULL);
    1300           4 :       ASSERT (a_seen == 0);
    1301           4 :       ASSERT (b_seen == 0);
    1302           4 :       ASSERT (p_value == NULL);
    1303           4 :       ASSERT (q_value == NULL);
    1304           4 :       ASSERT (non_options_count == 0);
    1305           4 :       ASSERT (unrecognized == 0);
    1306           4 :       ASSERT (optind == 1);
    1307           4 :       ASSERT (!output);
    1308             :     }
    1309           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1310             :     {
    1311           4 :       int a_seen = 0;
    1312           4 :       int b_seen = 0;
    1313           4 :       const char *p_value = NULL;
    1314           4 :       const char *q_value = NULL;
    1315           4 :       int non_options_count = 0;
    1316             :       const char *non_options[10];
    1317           4 :       int unrecognized = 0;
    1318             :       bool output;
    1319           4 :       int argc = 0;
    1320             :       const char *argv[10];
    1321             : 
    1322           4 :       argv[argc++] = "program";
    1323           4 :       argv[argc++] = "-p";
    1324           4 :       argv[argc] = NULL;
    1325           4 :       optind = start;
    1326           4 :       getopt_loop (argc, argv, "+:abp:q:",
    1327             :                    &a_seen, &b_seen, &p_value, &q_value,
    1328             :                    &non_options_count, non_options, &unrecognized, &output);
    1329           4 :       ASSERT (a_seen == 0);
    1330           4 :       ASSERT (b_seen == 0);
    1331           4 :       ASSERT (p_value == NULL);
    1332           4 :       ASSERT (q_value == NULL);
    1333           4 :       ASSERT (non_options_count == 0);
    1334           4 :       ASSERT (unrecognized == 'p');
    1335           4 :       ASSERT (optind == 2);
    1336           4 :       ASSERT (!output);
    1337             :     }
    1338           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1339             :     {
    1340           4 :       int a_seen = 0;
    1341           4 :       int b_seen = 0;
    1342           4 :       const char *p_value = NULL;
    1343           4 :       const char *q_value = NULL;
    1344           4 :       int non_options_count = 0;
    1345             :       const char *non_options[10];
    1346           4 :       int unrecognized = 0;
    1347             :       bool output;
    1348           4 :       int argc = 0;
    1349             :       const char *argv[10];
    1350             : 
    1351           4 :       argv[argc++] = "program";
    1352           4 :       argv[argc++] = "-b";
    1353           4 :       argv[argc++] = "-p";
    1354           4 :       argv[argc] = NULL;
    1355           4 :       optind = start;
    1356           4 :       getopt_loop (argc, argv, "+:abp:q:",
    1357             :                    &a_seen, &b_seen, &p_value, &q_value,
    1358             :                    &non_options_count, non_options, &unrecognized, &output);
    1359           4 :       ASSERT (a_seen == 0);
    1360           4 :       ASSERT (b_seen == 1);
    1361           4 :       ASSERT (p_value == NULL);
    1362           4 :       ASSERT (q_value == NULL);
    1363           4 :       ASSERT (non_options_count == 0);
    1364           4 :       ASSERT (unrecognized == 'p');
    1365           4 :       ASSERT (optind == 3);
    1366           4 :       ASSERT (!output);
    1367             :     }
    1368             : 
    1369             :   /* Check that 'W' does not dump core:
    1370             :      http://sourceware.org/bugzilla/show_bug.cgi?id=12922
    1371             :      Technically, POSIX says the presence of ';' in the opt-string
    1372             :      gives unspecified behavior, so we only test this when GNU compliance
    1373             :      is desired.  */
    1374           6 :   for (start = OPTIND_MIN; start <= 1; start++)
    1375             :     {
    1376           4 :       int argc = 0;
    1377             :       const char *argv[10];
    1378           4 :       int pos = ftell (stderr);
    1379             : 
    1380           4 :       argv[argc++] = "program";
    1381           4 :       argv[argc++] = "-W";
    1382           4 :       argv[argc++] = "dummy";
    1383           4 :       argv[argc] = NULL;
    1384           4 :       optind = start;
    1385           4 :       opterr = 1;
    1386           4 :       ASSERT (getopt (argc, (char **) argv, "W;") == 'W');
    1387           4 :       ASSERT (ftell (stderr) == pos);
    1388           4 :       ASSERT (optind == 2);
    1389             :     }
    1390             : #endif /* GNULIB_TEST_GETOPT_GNU */
    1391           2 : }

Generated by: LCOV version 1.11