Updated from ../gpl2lgpl.sed /home/gd/gnu/lib/getopt.c
[platform/upstream/glibc.git] / posix / getopt.c
1 /* Getopt for GNU.
2    NOTE: getopt is now part of the C library, so if you don't know what
3    "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
4    before changing it!
5
6    Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95
7         Free Software Foundation, Inc.
8
9 This file is part of the GNU C Library.  Its master source is NOT part of
10 the C library, however.  The master source lives in /gd/gnu/lib.
11
12 The GNU C Library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Library General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 The GNU C Library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 Library General Public License for more details.
21
22 You should have received a copy of the GNU Library General Public
23 License along with the GNU C Library; see the file COPYING.LIB.  If
24 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
25 Cambridge, MA 02139, USA.  */
26 \f
27 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
28    Ditto for AIX 3.2 and <stdlib.h>.  */
29 #ifndef _NO_PROTO
30 #define _NO_PROTO
31 #endif
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #if !defined (__STDC__) || !__STDC__
38 /* This is a separate conditional since some stdc systems
39    reject `defined (const)'.  */
40 #ifndef const
41 #define const
42 #endif
43 #endif
44
45 #include <stdio.h>
46
47 /* Comment out all this code if we are using the GNU C Library, and are not
48    actually compiling the library itself.  This code is part of the GNU C
49    Library, but also included in many other GNU distributions.  Compiling
50    and linking in this code is a waste when using the GNU C library
51    (especially if it is a shared library).  Rather than having every GNU
52    program understand `configure --with-gnu-libc' and omit the object files,
53    it is simpler to just do this in the source for each such file.  */
54
55 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
56
57
58 /* This needs to come after some library #include
59    to get __GNU_LIBRARY__ defined.  */
60 #ifdef  __GNU_LIBRARY__
61 /* Don't include stdlib.h for non-GNU C libraries because some of them
62    contain conflicting prototypes for getopt.  */
63 #include <stdlib.h>
64 #endif  /* GNU C library.  */
65
66 #ifndef _
67 /* This is for other GNU distributions with internationalized messages.
68    When compiling libc, the _ macro is predefined.  */
69 #ifdef HAVE_LIBINTL_H
70 # include <libintl.h>
71 # define _(msgid)       gettext (msgid)
72 #else
73 # define _(msgid)       (msgid)
74 #endif
75 #endif
76
77 /* This version of `getopt' appears to the caller like standard Unix `getopt'
78    but it behaves differently for the user, since it allows the user
79    to intersperse the options with the other arguments.
80
81    As `getopt' works, it permutes the elements of ARGV so that,
82    when it is done, all the options precede everything else.  Thus
83    all application programs are extended to handle flexible argument order.
84
85    Setting the environment variable POSIXLY_CORRECT disables permutation.
86    Then the behavior is completely standard.
87
88    GNU application programs can use a third alternative mode in which
89    they can distinguish the relative order of options and other arguments.  */
90
91 #include "getopt.h"
92
93 /* For communication from `getopt' to the caller.
94    When `getopt' finds an option that takes an argument,
95    the argument value is returned here.
96    Also, when `ordering' is RETURN_IN_ORDER,
97    each non-option ARGV-element is returned here.  */
98
99 char *optarg = NULL;
100
101 /* Index in ARGV of the next element to be scanned.
102    This is used for communication to and from the caller
103    and for communication between successive calls to `getopt'.
104
105    On entry to `getopt', zero means this is the first call; initialize.
106
107    When `getopt' returns EOF, this is the index of the first of the
108    non-option elements that the caller should itself scan.
109
110    Otherwise, `optind' communicates from one call to the next
111    how much of ARGV has been scanned so far.  */
112
113 /* XXX 1003.2 says this must be 1 before any call.  */
114 int optind = 0;
115
116 /* The next char to be scanned in the option-element
117    in which the last option character we returned was found.
118    This allows us to pick up the scan where we left off.
119
120    If this is zero, or a null string, it means resume the scan
121    by advancing to the next ARGV-element.  */
122
123 static char *nextchar;
124
125 /* Callers store zero here to inhibit the error message
126    for unrecognized options.  */
127
128 int opterr = 1;
129
130 /* Set to an option character which was unrecognized.
131    This must be initialized on some systems to avoid linking in the
132    system's own getopt implementation.  */
133
134 int optopt = '?';
135
136 /* Describe how to deal with options that follow non-option ARGV-elements.
137
138    If the caller did not specify anything,
139    the default is REQUIRE_ORDER if the environment variable
140    POSIXLY_CORRECT is defined, PERMUTE otherwise.
141
142    REQUIRE_ORDER means don't recognize them as options;
143    stop option processing when the first non-option is seen.
144    This is what Unix does.
145    This mode of operation is selected by either setting the environment
146    variable POSIXLY_CORRECT, or using `+' as the first character
147    of the list of option characters.
148
149    PERMUTE is the default.  We permute the contents of ARGV as we scan,
150    so that eventually all the non-options are at the end.  This allows options
151    to be given in any order, even with programs that were not written to
152    expect this.
153
154    RETURN_IN_ORDER is an option available to programs that were written
155    to expect options and other ARGV-elements in any order and that care about
156    the ordering of the two.  We describe each non-option ARGV-element
157    as if it were the argument of an option with character code 1.
158    Using `-' as the first character of the list of option characters
159    selects this mode of operation.
160
161    The special argument `--' forces an end of option-scanning regardless
162    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
163    `--' can cause `getopt' to return EOF with `optind' != ARGC.  */
164
165 static enum
166 {
167   REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
168 } ordering;
169
170 /* Value of POSIXLY_CORRECT environment variable.  */
171 static char *posixly_correct;
172 \f
173 #ifdef  __GNU_LIBRARY__
174 /* We want to avoid inclusion of string.h with non-GNU libraries
175    because there are many ways it can cause trouble.
176    On some systems, it contains special magic macros that don't work
177    in GCC.  */
178 #include <string.h>
179 #define my_index        strchr
180 #else
181
182 /* Avoid depending on library functions or files
183    whose names are inconsistent.  */
184
185 char *getenv ();
186
187 static char *
188 my_index (str, chr)
189      const char *str;
190      int chr;
191 {
192   while (*str)
193     {
194       if (*str == chr)
195         return (char *) str;
196       str++;
197     }
198   return 0;
199 }
200
201 /* If using GCC, we can safely declare strlen this way.
202    If not using GCC, it is ok not to declare it.  */
203 #ifdef __GNUC__
204 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
205    That was relevant to code that was here before.  */
206 #if !defined (__STDC__) || !__STDC__
207 /* gcc with -traditional declares the built-in strlen to return int,
208    and has done so at least since version 2.4.5. -- rms.  */
209 extern int strlen (const char *);
210 #endif /* not __STDC__ */
211 #endif /* __GNUC__ */
212
213 #endif /* not __GNU_LIBRARY__ */
214 \f
215 /* Handle permutation of arguments.  */
216
217 /* Describe the part of ARGV that contains non-options that have
218    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
219    `last_nonopt' is the index after the last of them.  */
220
221 static int first_nonopt;
222 static int last_nonopt;
223
224 /* Bash 2.0 gives us an environment variable containing flags
225    indicating ARGV elements that should not be considered arguments.  */
226
227 static const char *nonoption_flags;
228 static int nonoption_flags_len;
229
230 /* Exchange two adjacent subsequences of ARGV.
231    One subsequence is elements [first_nonopt,last_nonopt)
232    which contains all the non-options that have been skipped so far.
233    The other is elements [last_nonopt,optind), which contains all
234    the options processed since those non-options were skipped.
235
236    `first_nonopt' and `last_nonopt' are relocated so that they describe
237    the new indices of the non-options in ARGV after they are moved.  */
238
239 #if defined (__STDC__) && __STDC__
240 static void exchange (char **);
241 #endif
242
243 static void
244 exchange (argv)
245      char **argv;
246 {
247   int bottom = first_nonopt;
248   int middle = last_nonopt;
249   int top = optind;
250   char *tem;
251
252   /* Exchange the shorter segment with the far end of the longer segment.
253      That puts the shorter segment into the right place.
254      It leaves the longer segment in the right place overall,
255      but it consists of two parts that need to be swapped next.  */
256
257   while (top > middle && middle > bottom)
258     {
259       if (top - middle > middle - bottom)
260         {
261           /* Bottom segment is the short one.  */
262           int len = middle - bottom;
263           register int i;
264
265           /* Swap it with the top part of the top segment.  */
266           for (i = 0; i < len; i++)
267             {
268               tem = argv[bottom + i];
269               argv[bottom + i] = argv[top - (middle - bottom) + i];
270               argv[top - (middle - bottom) + i] = tem;
271             }
272           /* Exclude the moved bottom segment from further swapping.  */
273           top -= len;
274         }
275       else
276         {
277           /* Top segment is the short one.  */
278           int len = top - middle;
279           register int i;
280
281           /* Swap it with the bottom part of the bottom segment.  */
282           for (i = 0; i < len; i++)
283             {
284               tem = argv[bottom + i];
285               argv[bottom + i] = argv[middle + i];
286               argv[middle + i] = tem;
287             }
288           /* Exclude the moved top segment from further swapping.  */
289           bottom += len;
290         }
291     }
292
293   /* Update records for the slots the non-options now occupy.  */
294
295   first_nonopt += (optind - last_nonopt);
296   last_nonopt = optind;
297 }
298
299 /* Initialize the internal data when the first call is made.  */
300
301 #if defined (__STDC__) && __STDC__
302 static const char *_getopt_initialize (const char *);
303 #endif
304 static const char *
305 _getopt_initialize (optstring)
306      const char *optstring;
307 {
308   /* Start processing options with ARGV-element 1 (since ARGV-element 0
309      is the program name); the sequence of previously skipped
310      non-option ARGV-elements is empty.  */
311
312   first_nonopt = last_nonopt = optind = 1;
313
314   nextchar = NULL;
315
316   posixly_correct = getenv ("POSIXLY_CORRECT");
317
318   /* Determine how to handle the ordering of options and nonoptions.  */
319
320   if (optstring[0] == '-')
321     {
322       ordering = RETURN_IN_ORDER;
323       ++optstring;
324     }
325   else if (optstring[0] == '+')
326     {
327       ordering = REQUIRE_ORDER;
328       ++optstring;
329     }
330   else if (posixly_correct != NULL)
331     ordering = REQUIRE_ORDER;
332   else
333     ordering = PERMUTE;
334
335   if (posixly_correct == NULL)
336     {
337       /* Bash 2.0 puts a special variable in the environment for each
338          command it runs, specifying which ARGV elements are the results of
339          file name wildcard expansion and therefore should not be
340          considered as options.  */
341       char var[100];
342       sprintf (var, "_%d_GNU_nonoption_argv_flags_", getpid ());
343       nonoption_flags = getenv (var);
344       if (nonoption_flags == NULL)
345         nonoption_flags_len = 0;
346       else
347         nonoption_flags_len = strlen (nonoption_flags);
348     }
349
350   return optstring;
351 }
352 \f
353 /* Scan elements of ARGV (whose length is ARGC) for option characters
354    given in OPTSTRING.
355
356    If an element of ARGV starts with '-', and is not exactly "-" or "--",
357    then it is an option element.  The characters of this element
358    (aside from the initial '-') are option characters.  If `getopt'
359    is called repeatedly, it returns successively each of the option characters
360    from each of the option elements.
361
362    If `getopt' finds another option character, it returns that character,
363    updating `optind' and `nextchar' so that the next call to `getopt' can
364    resume the scan with the following option character or ARGV-element.
365
366    If there are no more option characters, `getopt' returns `EOF'.
367    Then `optind' is the index in ARGV of the first ARGV-element
368    that is not an option.  (The ARGV-elements have been permuted
369    so that those that are not options now come last.)
370
371    OPTSTRING is a string containing the legitimate option characters.
372    If an option character is seen that is not listed in OPTSTRING,
373    return '?' after printing an error message.  If you set `opterr' to
374    zero, the error message is suppressed but we still return '?'.
375
376    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
377    so the following text in the same ARGV-element, or the text of the following
378    ARGV-element, is returned in `optarg'.  Two colons mean an option that
379    wants an optional arg; if there is text in the current ARGV-element,
380    it is returned in `optarg', otherwise `optarg' is set to zero.
381
382    If OPTSTRING starts with `-' or `+', it requests different methods of
383    handling the non-option ARGV-elements.
384    See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
385
386    Long-named options begin with `--' instead of `-'.
387    Their names may be abbreviated as long as the abbreviation is unique
388    or is an exact match for some defined option.  If they have an
389    argument, it follows the option name in the same ARGV-element, separated
390    from the option name by a `=', or else the in next ARGV-element.
391    When `getopt' finds a long-named option, it returns 0 if that option's
392    `flag' field is nonzero, the value of the option's `val' field
393    if the `flag' field is zero.
394
395    The elements of ARGV aren't really const, because we permute them.
396    But we pretend they're const in the prototype to be compatible
397    with other systems.
398
399    LONGOPTS is a vector of `struct option' terminated by an
400    element containing a name which is zero.
401
402    LONGIND returns the index in LONGOPT of the long-named option found.
403    It is only valid when a long-named option has been found by the most
404    recent call.
405
406    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
407    long-named options.  */
408
409 int
410 _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
411      int argc;
412      char *const *argv;
413      const char *optstring;
414      const struct option *longopts;
415      int *longind;
416      int long_only;
417 {
418   optarg = NULL;
419
420   if (optind == 0)
421     {
422       optstring = _getopt_initialize (optstring);
423       optind = 1;               /* Don't scan ARGV[0], the program name.  */
424     }
425
426   /* Test whether ARGV[optind] points to a non-option argument.
427      Either it does not have option syntax, or there is an environment flag
428      from the shell indicating it is not an option.  */
429 #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'        \
430                      || (optind < nonoption_flags_len                         \
431                          && nonoption_flags[optind] == '1'))
432
433   if (nextchar == NULL || *nextchar == '\0')
434     {
435       /* Advance to the next ARGV-element.  */
436
437       if (ordering == PERMUTE)
438         {
439           /* If we have just processed some options following some non-options,
440              exchange them so that the options come first.  */
441
442           if (first_nonopt != last_nonopt && last_nonopt != optind)
443             exchange ((char **) argv);
444           else if (last_nonopt != optind)
445             first_nonopt = optind;
446
447           /* Skip any additional non-options
448              and extend the range of non-options previously skipped.  */
449
450           while (optind < argc && NONOPTION_P)
451             optind++;
452           last_nonopt = optind;
453         }
454
455       /* The special ARGV-element `--' means premature end of options.
456          Skip it like a null option,
457          then exchange with previous non-options as if it were an option,
458          then skip everything else like a non-option.  */
459
460       if (optind != argc && !strcmp (argv[optind], "--"))
461         {
462           optind++;
463
464           if (first_nonopt != last_nonopt && last_nonopt != optind)
465             exchange ((char **) argv);
466           else if (first_nonopt == last_nonopt)
467             first_nonopt = optind;
468           last_nonopt = argc;
469
470           optind = argc;
471         }
472
473       /* If we have done all the ARGV-elements, stop the scan
474          and back over any non-options that we skipped and permuted.  */
475
476       if (optind == argc)
477         {
478           /* Set the next-arg-index to point at the non-options
479              that we previously skipped, so the caller will digest them.  */
480           if (first_nonopt != last_nonopt)
481             optind = first_nonopt;
482           return EOF;
483         }
484
485       /* If we have come to a non-option and did not permute it,
486          either stop the scan or describe it to the caller and pass it by.  */
487
488       if (NONOPTION_P)
489         {
490           if (ordering == REQUIRE_ORDER)
491             return EOF;
492           optarg = argv[optind++];
493           return 1;
494         }
495
496       /* We have found another option-ARGV-element.
497          Skip the initial punctuation.  */
498
499       nextchar = (argv[optind] + 1
500                   + (longopts != NULL && argv[optind][1] == '-'));
501     }
502
503   /* Decode the current option-ARGV-element.  */
504
505   /* Check whether the ARGV-element is a long option.
506
507      If long_only and the ARGV-element has the form "-f", where f is
508      a valid short option, don't consider it an abbreviated form of
509      a long option that starts with f.  Otherwise there would be no
510      way to give the -f short option.
511
512      On the other hand, if there's a long option "fubar" and
513      the ARGV-element is "-fu", do consider that an abbreviation of
514      the long option, just like "--fu", and not "-f" with arg "u".
515
516      This distinction seems to be the most useful approach.  */
517
518   if (longopts != NULL
519       && (argv[optind][1] == '-'
520           || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
521     {
522       char *nameend;
523       const struct option *p;
524       const struct option *pfound = NULL;
525       int exact = 0;
526       int ambig = 0;
527       int indfound;
528       int option_index;
529
530       for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
531         /* Do nothing.  */ ;
532
533       /* Test all long options for either exact match
534          or abbreviated matches.  */
535       for (p = longopts, option_index = 0; p->name; p++, option_index++)
536         if (!strncmp (p->name, nextchar, nameend - nextchar))
537           {
538             if (nameend - nextchar == strlen (p->name))
539               {
540                 /* Exact match found.  */
541                 pfound = p;
542                 indfound = option_index;
543                 exact = 1;
544                 break;
545               }
546             else if (pfound == NULL)
547               {
548                 /* First nonexact match found.  */
549                 pfound = p;
550                 indfound = option_index;
551               }
552             else
553               /* Second or later nonexact match found.  */
554               ambig = 1;
555           }
556
557       if (ambig && !exact)
558         {
559           if (opterr)
560             fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
561                      argv[0], argv[optind]);
562           nextchar += strlen (nextchar);
563           optind++;
564           return '?';
565         }
566
567       if (pfound != NULL)
568         {
569           option_index = indfound;
570           optind++;
571           if (*nameend)
572             {
573               /* Don't test has_arg with >, because some C compilers don't
574                  allow it to be used on enums.  */
575               if (pfound->has_arg)
576                 optarg = nameend + 1;
577               else
578                 {
579                   if (opterr)
580                    if (argv[optind - 1][1] == '-')
581                     /* --option */
582                     fprintf (stderr,
583                      _("%s: option `--%s' doesn't allow an argument\n"),
584                      argv[0], pfound->name);
585                    else
586                     /* +option or -option */
587                     fprintf (stderr,
588                      _("%s: option `%c%s' doesn't allow an argument\n"),
589                      argv[0], argv[optind - 1][0], pfound->name);
590
591                   nextchar += strlen (nextchar);
592                   return '?';
593                 }
594             }
595           else if (pfound->has_arg == 1)
596             {
597               if (optind < argc)
598                 optarg = argv[optind++];
599               else
600                 {
601                   if (opterr)
602                     fprintf (stderr,
603                            _("%s: option `%s' requires an argument\n"),
604                            argv[0], argv[optind - 1]);
605                   nextchar += strlen (nextchar);
606                   return optstring[0] == ':' ? ':' : '?';
607                 }
608             }
609           nextchar += strlen (nextchar);
610           if (longind != NULL)
611             *longind = option_index;
612           if (pfound->flag)
613             {
614               *(pfound->flag) = pfound->val;
615               return 0;
616             }
617           return pfound->val;
618         }
619
620       /* Can't find it as a long option.  If this is not getopt_long_only,
621          or the option starts with '--' or is not a valid short
622          option, then it's an error.
623          Otherwise interpret it as a short option.  */
624       if (!long_only || argv[optind][1] == '-'
625           || my_index (optstring, *nextchar) == NULL)
626         {
627           if (opterr)
628             {
629               if (argv[optind][1] == '-')
630                 /* --option */
631                 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
632                          argv[0], nextchar);
633               else
634                 /* +option or -option */
635                 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
636                          argv[0], argv[optind][0], nextchar);
637             }
638           nextchar = (char *) "";
639           optind++;
640           return '?';
641         }
642     }
643
644   /* Look at and handle the next short option-character.  */
645
646   {
647     char c = *nextchar++;
648     char *temp = my_index (optstring, c);
649
650     /* Increment `optind' when we start to process its last character.  */
651     if (*nextchar == '\0')
652       ++optind;
653
654     if (temp == NULL || c == ':')
655       {
656         if (opterr)
657           {
658             if (posixly_correct)
659               /* 1003.2 specifies the format of this message.  */
660               fprintf (stderr, _("%s: illegal option -- %c\n"),
661                        argv[0], c);
662             else
663               fprintf (stderr, _("%s: invalid option -- %c\n"),
664                        argv[0], c);
665           }
666         optopt = c;
667         return '?';
668       }
669     if (temp[1] == ':')
670       {
671         if (temp[2] == ':')
672           {
673             /* This is an option that accepts an argument optionally.  */
674             if (*nextchar != '\0')
675               {
676                 optarg = nextchar;
677                 optind++;
678               }
679             else
680               optarg = NULL;
681             nextchar = NULL;
682           }
683         else
684           {
685             /* This is an option that requires an argument.  */
686             if (*nextchar != '\0')
687               {
688                 optarg = nextchar;
689                 /* If we end this ARGV-element by taking the rest as an arg,
690                    we must advance to the next element now.  */
691                 optind++;
692               }
693             else if (optind == argc)
694               {
695                 if (opterr)
696                   {
697                     /* 1003.2 specifies the format of this message.  */
698                     fprintf (stderr,
699                            _("%s: option requires an argument -- %c\n"),
700                            argv[0], c);
701                   }
702                 optopt = c;
703                 if (optstring[0] == ':')
704                   c = ':';
705                 else
706                   c = '?';
707               }
708             else
709               /* We already incremented `optind' once;
710                  increment it again when taking next ARGV-elt as argument.  */
711               optarg = argv[optind++];
712             nextchar = NULL;
713           }
714       }
715     return c;
716   }
717 }
718
719 int
720 getopt (argc, argv, optstring)
721      int argc;
722      char *const *argv;
723      const char *optstring;
724 {
725   return _getopt_internal (argc, argv, optstring,
726                            (const struct option *) 0,
727                            (int *) 0,
728                            0);
729 }
730
731 #endif  /* _LIBC or not __GNU_LIBRARY__.  */
732 \f
733 #ifdef TEST
734
735 /* Compile with -DTEST to make an executable for use in testing
736    the above definition of `getopt'.  */
737
738 int
739 main (argc, argv)
740      int argc;
741      char **argv;
742 {
743   int c;
744   int digit_optind = 0;
745
746   while (1)
747     {
748       int this_option_optind = optind ? optind : 1;
749
750       c = getopt (argc, argv, "abc:d:0123456789");
751       if (c == EOF)
752         break;
753
754       switch (c)
755         {
756         case '0':
757         case '1':
758         case '2':
759         case '3':
760         case '4':
761         case '5':
762         case '6':
763         case '7':
764         case '8':
765         case '9':
766           if (digit_optind != 0 && digit_optind != this_option_optind)
767             printf ("digits occur in two different argv-elements.\n");
768           digit_optind = this_option_optind;
769           printf ("option %c\n", c);
770           break;
771
772         case 'a':
773           printf ("option a\n");
774           break;
775
776         case 'b':
777           printf ("option b\n");
778           break;
779
780         case 'c':
781           printf ("option c with value `%s'\n", optarg);
782           break;
783
784         case '?':
785           break;
786
787         default:
788           printf ("?? getopt returned character code 0%o ??\n", c);
789         }
790     }
791
792   if (optind < argc)
793     {
794       printf ("non-option ARGV-elements: ");
795       while (optind < argc)
796         printf ("%s ", argv[optind++]);
797       printf ("\n");
798     }
799
800   exit (0);
801 }
802
803 #endif /* TEST */