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