xref: /illumos-gate/usr/src/grub/grub-0.97/lib/getopt.c (revision 1b8adde7)
17c478bd9Sstevel@tonic-gate /* Getopt for GNU.
27c478bd9Sstevel@tonic-gate    NOTE: getopt is now part of the C library, so if you don't know what
37c478bd9Sstevel@tonic-gate    "Keep this file name-space clean" means, talk to drepper@gnu.org
47c478bd9Sstevel@tonic-gate    before changing it!
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate    Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98
77c478bd9Sstevel@tonic-gate    	Free Software Foundation, Inc.
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate    NOTE: The canonical source of this file is maintained with the GNU C Library.
107c478bd9Sstevel@tonic-gate    Bugs can be reported to bug-glibc@gnu.org.
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate    This program is free software; you can redistribute it and/or modify it
137c478bd9Sstevel@tonic-gate    under the terms of the GNU General Public License as published by the
147c478bd9Sstevel@tonic-gate    Free Software Foundation; either version 2, or (at your option) any
157c478bd9Sstevel@tonic-gate    later version.
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate    This program is distributed in the hope that it will be useful,
187c478bd9Sstevel@tonic-gate    but WITHOUT ANY WARRANTY; without even the implied warranty of
197c478bd9Sstevel@tonic-gate    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
207c478bd9Sstevel@tonic-gate    GNU General Public License for more details.
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate    You should have received a copy of the GNU General Public License
237c478bd9Sstevel@tonic-gate    along with this program; if not, write to the Free Software
247c478bd9Sstevel@tonic-gate    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
257c478bd9Sstevel@tonic-gate    USA.  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
287c478bd9Sstevel@tonic-gate    Ditto for AIX 3.2 and <stdlib.h>.  */
297c478bd9Sstevel@tonic-gate #ifndef _NO_PROTO
307c478bd9Sstevel@tonic-gate # define _NO_PROTO
317c478bd9Sstevel@tonic-gate #endif
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #ifdef HAVE_CONFIG_H
347c478bd9Sstevel@tonic-gate # include <config.h>
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #if !defined __STDC__ || !__STDC__
387c478bd9Sstevel@tonic-gate /* This is a separate conditional since some stdc systems
397c478bd9Sstevel@tonic-gate    reject `defined (const)'.  */
407c478bd9Sstevel@tonic-gate # ifndef const
417c478bd9Sstevel@tonic-gate #  define const
427c478bd9Sstevel@tonic-gate # endif
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* Comment out all this code if we are using the GNU C Library, and are not
487c478bd9Sstevel@tonic-gate    actually compiling the library itself.  This code is part of the GNU C
497c478bd9Sstevel@tonic-gate    Library, but also included in many other GNU distributions.  Compiling
507c478bd9Sstevel@tonic-gate    and linking in this code is a waste when using the GNU C library
517c478bd9Sstevel@tonic-gate    (especially if it is a shared library).  Rather than having every GNU
527c478bd9Sstevel@tonic-gate    program understand `configure --with-gnu-libc' and omit the object files,
537c478bd9Sstevel@tonic-gate    it is simpler to just do this in the source for each such file.  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define GETOPT_INTERFACE_VERSION 2
567c478bd9Sstevel@tonic-gate #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
577c478bd9Sstevel@tonic-gate # include <gnu-versions.h>
587c478bd9Sstevel@tonic-gate # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
597c478bd9Sstevel@tonic-gate #  define ELIDE_CODE
607c478bd9Sstevel@tonic-gate # endif
617c478bd9Sstevel@tonic-gate #endif
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #ifndef ELIDE_CODE
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* This needs to come after some library #include
677c478bd9Sstevel@tonic-gate    to get __GNU_LIBRARY__ defined.  */
687c478bd9Sstevel@tonic-gate #ifdef	__GNU_LIBRARY__
697c478bd9Sstevel@tonic-gate /* Don't include stdlib.h for non-GNU C libraries because some of them
707c478bd9Sstevel@tonic-gate    contain conflicting prototypes for getopt.  */
717c478bd9Sstevel@tonic-gate # include <stdlib.h>
727c478bd9Sstevel@tonic-gate # include <unistd.h>
737c478bd9Sstevel@tonic-gate #endif	/* GNU C library.  */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef VMS
767c478bd9Sstevel@tonic-gate # include <unixlib.h>
777c478bd9Sstevel@tonic-gate # if HAVE_STRING_H - 0
787c478bd9Sstevel@tonic-gate #  include <string.h>
797c478bd9Sstevel@tonic-gate # endif
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #ifndef _
837c478bd9Sstevel@tonic-gate /* This is for other GNU distributions with internationalized messages.
847c478bd9Sstevel@tonic-gate    When compiling libc, the _ macro is predefined.  */
857c478bd9Sstevel@tonic-gate # ifdef HAVE_LIBINTL_H
867c478bd9Sstevel@tonic-gate #  include <libintl.h>
877c478bd9Sstevel@tonic-gate #  define _(msgid)	gettext (msgid)
887c478bd9Sstevel@tonic-gate # else
897c478bd9Sstevel@tonic-gate #  define _(msgid)	(msgid)
907c478bd9Sstevel@tonic-gate # endif
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* This version of `getopt' appears to the caller like standard Unix `getopt'
947c478bd9Sstevel@tonic-gate    but it behaves differently for the user, since it allows the user
957c478bd9Sstevel@tonic-gate    to intersperse the options with the other arguments.
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate    As `getopt' works, it permutes the elements of ARGV so that,
987c478bd9Sstevel@tonic-gate    when it is done, all the options precede everything else.  Thus
997c478bd9Sstevel@tonic-gate    all application programs are extended to handle flexible argument order.
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate    Setting the environment variable POSIXLY_CORRECT disables permutation.
1027c478bd9Sstevel@tonic-gate    Then the behavior is completely standard.
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate    GNU application programs can use a third alternative mode in which
1057c478bd9Sstevel@tonic-gate    they can distinguish the relative order of options and other arguments.  */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #include "getopt.h"
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /* For communication from `getopt' to the caller.
1107c478bd9Sstevel@tonic-gate    When `getopt' finds an option that takes an argument,
1117c478bd9Sstevel@tonic-gate    the argument value is returned here.
1127c478bd9Sstevel@tonic-gate    Also, when `ordering' is RETURN_IN_ORDER,
1137c478bd9Sstevel@tonic-gate    each non-option ARGV-element is returned here.  */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate char *optarg = NULL;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* Index in ARGV of the next element to be scanned.
1187c478bd9Sstevel@tonic-gate    This is used for communication to and from the caller
1197c478bd9Sstevel@tonic-gate    and for communication between successive calls to `getopt'.
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate    On entry to `getopt', zero means this is the first call; initialize.
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate    When `getopt' returns -1, this is the index of the first of the
1247c478bd9Sstevel@tonic-gate    non-option elements that the caller should itself scan.
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate    Otherwise, `optind' communicates from one call to the next
1277c478bd9Sstevel@tonic-gate    how much of ARGV has been scanned so far.  */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* 1003.2 says this must be 1 before any call.  */
1307c478bd9Sstevel@tonic-gate int optind = 1;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* Formerly, initialization of getopt depended on optind==0, which
1337c478bd9Sstevel@tonic-gate    causes problems with re-calling getopt as programs generally don't
1347c478bd9Sstevel@tonic-gate    know that. */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate int __getopt_initialized = 0;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /* The next char to be scanned in the option-element
1397c478bd9Sstevel@tonic-gate    in which the last option character we returned was found.
1407c478bd9Sstevel@tonic-gate    This allows us to pick up the scan where we left off.
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate    If this is zero, or a null string, it means resume the scan
1437c478bd9Sstevel@tonic-gate    by advancing to the next ARGV-element.  */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static char *nextchar;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /* Callers store zero here to inhibit the error message
1487c478bd9Sstevel@tonic-gate    for unrecognized options.  */
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate int opterr = 1;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* Set to an option character which was unrecognized.
1537c478bd9Sstevel@tonic-gate    This must be initialized on some systems to avoid linking in the
1547c478bd9Sstevel@tonic-gate    system's own getopt implementation.  */
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int optopt = '?';
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /* Describe how to deal with options that follow non-option ARGV-elements.
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate    If the caller did not specify anything,
1617c478bd9Sstevel@tonic-gate    the default is REQUIRE_ORDER if the environment variable
1627c478bd9Sstevel@tonic-gate    POSIXLY_CORRECT is defined, PERMUTE otherwise.
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate    REQUIRE_ORDER means don't recognize them as options;
1657c478bd9Sstevel@tonic-gate    stop option processing when the first non-option is seen.
1667c478bd9Sstevel@tonic-gate    This is what Unix does.
1677c478bd9Sstevel@tonic-gate    This mode of operation is selected by either setting the environment
1687c478bd9Sstevel@tonic-gate    variable POSIXLY_CORRECT, or using `+' as the first character
1697c478bd9Sstevel@tonic-gate    of the list of option characters.
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate    PERMUTE is the default.  We permute the contents of ARGV as we scan,
1727c478bd9Sstevel@tonic-gate    so that eventually all the non-options are at the end.  This allows options
1737c478bd9Sstevel@tonic-gate    to be given in any order, even with programs that were not written to
1747c478bd9Sstevel@tonic-gate    expect this.
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate    RETURN_IN_ORDER is an option available to programs that were written
1777c478bd9Sstevel@tonic-gate    to expect options and other ARGV-elements in any order and that care about
1787c478bd9Sstevel@tonic-gate    the ordering of the two.  We describe each non-option ARGV-element
1797c478bd9Sstevel@tonic-gate    as if it were the argument of an option with character code 1.
1807c478bd9Sstevel@tonic-gate    Using `-' as the first character of the list of option characters
1817c478bd9Sstevel@tonic-gate    selects this mode of operation.
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate    The special argument `--' forces an end of option-scanning regardless
1847c478bd9Sstevel@tonic-gate    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
1857c478bd9Sstevel@tonic-gate    `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate static enum
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate   REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
1907c478bd9Sstevel@tonic-gate } ordering;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* Value of POSIXLY_CORRECT environment variable.  */
1937c478bd9Sstevel@tonic-gate static char *posixly_correct;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate #ifdef	__GNU_LIBRARY__
1967c478bd9Sstevel@tonic-gate /* We want to avoid inclusion of string.h with non-GNU libraries
1977c478bd9Sstevel@tonic-gate    because there are many ways it can cause trouble.
1987c478bd9Sstevel@tonic-gate    On some systems, it contains special magic macros that don't work
1997c478bd9Sstevel@tonic-gate    in GCC.  */
2007c478bd9Sstevel@tonic-gate # include <string.h>
2017c478bd9Sstevel@tonic-gate # define my_index	strchr
2027c478bd9Sstevel@tonic-gate #else
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate # if HAVE_STRING_H
2057c478bd9Sstevel@tonic-gate #  include <string.h>
2067c478bd9Sstevel@tonic-gate # else
2077c478bd9Sstevel@tonic-gate #  include <strings.h>
2087c478bd9Sstevel@tonic-gate # endif
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /* Avoid depending on library functions or files
2117c478bd9Sstevel@tonic-gate    whose names are inconsistent.  */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate #ifndef getenv
2147c478bd9Sstevel@tonic-gate extern char *getenv ();
2157c478bd9Sstevel@tonic-gate #endif
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static char *
my_index(str,chr)2187c478bd9Sstevel@tonic-gate my_index (str, chr)
2197c478bd9Sstevel@tonic-gate      const char *str;
2207c478bd9Sstevel@tonic-gate      int chr;
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate   while (*str)
2237c478bd9Sstevel@tonic-gate     {
2247c478bd9Sstevel@tonic-gate       if (*str == chr)
2257c478bd9Sstevel@tonic-gate 	return (char *) str;
2267c478bd9Sstevel@tonic-gate       str++;
2277c478bd9Sstevel@tonic-gate     }
2287c478bd9Sstevel@tonic-gate   return 0;
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /* If using GCC, we can safely declare strlen this way.
2327c478bd9Sstevel@tonic-gate    If not using GCC, it is ok not to declare it.  */
2337c478bd9Sstevel@tonic-gate #ifdef __GNUC__
2347c478bd9Sstevel@tonic-gate /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
2357c478bd9Sstevel@tonic-gate    That was relevant to code that was here before.  */
2367c478bd9Sstevel@tonic-gate # if (!defined __STDC__ || !__STDC__) && !defined strlen
2377c478bd9Sstevel@tonic-gate /* gcc with -traditional declares the built-in strlen to return int,
2387c478bd9Sstevel@tonic-gate    and has done so at least since version 2.4.5. -- rms.  */
2397c478bd9Sstevel@tonic-gate extern int strlen (const char *);
2407c478bd9Sstevel@tonic-gate # endif /* not __STDC__ */
2417c478bd9Sstevel@tonic-gate #endif /* __GNUC__ */
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate #endif /* not __GNU_LIBRARY__ */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /* Handle permutation of arguments.  */
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /* Describe the part of ARGV that contains non-options that have
2487c478bd9Sstevel@tonic-gate    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
2497c478bd9Sstevel@tonic-gate    `last_nonopt' is the index after the last of them.  */
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static int first_nonopt;
2527c478bd9Sstevel@tonic-gate static int last_nonopt;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #ifdef _LIBC
2557c478bd9Sstevel@tonic-gate /* Bash 2.0 gives us an environment variable containing flags
2567c478bd9Sstevel@tonic-gate    indicating ARGV elements that should not be considered arguments.  */
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /* Defined in getopt_init.c  */
2597c478bd9Sstevel@tonic-gate extern char *__getopt_nonoption_flags;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate static int nonoption_flags_max_len;
2627c478bd9Sstevel@tonic-gate static int nonoption_flags_len;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static int original_argc;
2657c478bd9Sstevel@tonic-gate static char *const *original_argv;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /* Make sure the environment variable bash 2.0 puts in the environment
2687c478bd9Sstevel@tonic-gate    is valid for the getopt call we must make sure that the ARGV passed
2697c478bd9Sstevel@tonic-gate    to getopt is that one passed to the process.  */
2707c478bd9Sstevel@tonic-gate static void
2717c478bd9Sstevel@tonic-gate __attribute__ ((unused))
store_args_and_env(int argc,char * const * argv)2727c478bd9Sstevel@tonic-gate store_args_and_env (int argc, char *const *argv)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate   /* XXX This is no good solution.  We should rather copy the args so
2757c478bd9Sstevel@tonic-gate      that we can compare them later.  But we must not use malloc(3).  */
2767c478bd9Sstevel@tonic-gate   original_argc = argc;
2777c478bd9Sstevel@tonic-gate   original_argv = argv;
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate # ifdef text_set_element
2807c478bd9Sstevel@tonic-gate text_set_element (__libc_subinit, store_args_and_env);
2817c478bd9Sstevel@tonic-gate # endif /* text_set_element */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate # define SWAP_FLAGS(ch1, ch2) \
2847c478bd9Sstevel@tonic-gate   if (nonoption_flags_len > 0)						      \
2857c478bd9Sstevel@tonic-gate     {									      \
2867c478bd9Sstevel@tonic-gate       char __tmp = __getopt_nonoption_flags[ch1];			      \
2877c478bd9Sstevel@tonic-gate       __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
2887c478bd9Sstevel@tonic-gate       __getopt_nonoption_flags[ch2] = __tmp;				      \
2897c478bd9Sstevel@tonic-gate     }
2907c478bd9Sstevel@tonic-gate #else	/* !_LIBC */
2917c478bd9Sstevel@tonic-gate # define SWAP_FLAGS(ch1, ch2)
2927c478bd9Sstevel@tonic-gate #endif	/* _LIBC */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate /* Exchange two adjacent subsequences of ARGV.
2957c478bd9Sstevel@tonic-gate    One subsequence is elements [first_nonopt,last_nonopt)
2967c478bd9Sstevel@tonic-gate    which contains all the non-options that have been skipped so far.
2977c478bd9Sstevel@tonic-gate    The other is elements [last_nonopt,optind), which contains all
2987c478bd9Sstevel@tonic-gate    the options processed since those non-options were skipped.
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate    `first_nonopt' and `last_nonopt' are relocated so that they describe
3017c478bd9Sstevel@tonic-gate    the new indices of the non-options in ARGV after they are moved.  */
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate #if defined __STDC__ && __STDC__
3047c478bd9Sstevel@tonic-gate static void exchange (char **);
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static void
exchange(argv)3087c478bd9Sstevel@tonic-gate exchange (argv)
3097c478bd9Sstevel@tonic-gate      char **argv;
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate   int bottom = first_nonopt;
3127c478bd9Sstevel@tonic-gate   int middle = last_nonopt;
3137c478bd9Sstevel@tonic-gate   int top = optind;
3147c478bd9Sstevel@tonic-gate   char *tem;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate   /* Exchange the shorter segment with the far end of the longer segment.
3177c478bd9Sstevel@tonic-gate      That puts the shorter segment into the right place.
3187c478bd9Sstevel@tonic-gate      It leaves the longer segment in the right place overall,
3197c478bd9Sstevel@tonic-gate      but it consists of two parts that need to be swapped next.  */
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate #ifdef _LIBC
3227c478bd9Sstevel@tonic-gate   /* First make sure the handling of the `__getopt_nonoption_flags'
3237c478bd9Sstevel@tonic-gate      string can work normally.  Our top argument must be in the range
3247c478bd9Sstevel@tonic-gate      of the string.  */
3257c478bd9Sstevel@tonic-gate   if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
3267c478bd9Sstevel@tonic-gate     {
3277c478bd9Sstevel@tonic-gate       /* We must extend the array.  The user plays games with us and
3287c478bd9Sstevel@tonic-gate 	 presents new arguments.  */
3297c478bd9Sstevel@tonic-gate       char *new_str = malloc (top + 1);
3307c478bd9Sstevel@tonic-gate       if (new_str == NULL)
3317c478bd9Sstevel@tonic-gate 	nonoption_flags_len = nonoption_flags_max_len = 0;
3327c478bd9Sstevel@tonic-gate       else
3337c478bd9Sstevel@tonic-gate 	{
3347c478bd9Sstevel@tonic-gate 	  memset (__mempcpy (new_str, __getopt_nonoption_flags,
3357c478bd9Sstevel@tonic-gate 			     nonoption_flags_max_len),
3367c478bd9Sstevel@tonic-gate 		  '\0', top + 1 - nonoption_flags_max_len);
3377c478bd9Sstevel@tonic-gate 	  nonoption_flags_max_len = top + 1;
3387c478bd9Sstevel@tonic-gate 	  __getopt_nonoption_flags = new_str;
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate     }
3417c478bd9Sstevel@tonic-gate #endif
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate   while (top > middle && middle > bottom)
3447c478bd9Sstevel@tonic-gate     {
3457c478bd9Sstevel@tonic-gate       if (top - middle > middle - bottom)
3467c478bd9Sstevel@tonic-gate 	{
3477c478bd9Sstevel@tonic-gate 	  /* Bottom segment is the short one.  */
3487c478bd9Sstevel@tonic-gate 	  int len = middle - bottom;
3497c478bd9Sstevel@tonic-gate 	  register int i;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	  /* Swap it with the top part of the top segment.  */
3527c478bd9Sstevel@tonic-gate 	  for (i = 0; i < len; i++)
3537c478bd9Sstevel@tonic-gate 	    {
3547c478bd9Sstevel@tonic-gate 	      tem = argv[bottom + i];
3557c478bd9Sstevel@tonic-gate 	      argv[bottom + i] = argv[top - (middle - bottom) + i];
3567c478bd9Sstevel@tonic-gate 	      argv[top - (middle - bottom) + i] = tem;
3577c478bd9Sstevel@tonic-gate 	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
3587c478bd9Sstevel@tonic-gate 	    }
3597c478bd9Sstevel@tonic-gate 	  /* Exclude the moved bottom segment from further swapping.  */
3607c478bd9Sstevel@tonic-gate 	  top -= len;
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate       else
3637c478bd9Sstevel@tonic-gate 	{
3647c478bd9Sstevel@tonic-gate 	  /* Top segment is the short one.  */
3657c478bd9Sstevel@tonic-gate 	  int len = top - middle;
3667c478bd9Sstevel@tonic-gate 	  register int i;
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	  /* Swap it with the bottom part of the bottom segment.  */
3697c478bd9Sstevel@tonic-gate 	  for (i = 0; i < len; i++)
3707c478bd9Sstevel@tonic-gate 	    {
3717c478bd9Sstevel@tonic-gate 	      tem = argv[bottom + i];
3727c478bd9Sstevel@tonic-gate 	      argv[bottom + i] = argv[middle + i];
3737c478bd9Sstevel@tonic-gate 	      argv[middle + i] = tem;
3747c478bd9Sstevel@tonic-gate 	      SWAP_FLAGS (bottom + i, middle + i);
3757c478bd9Sstevel@tonic-gate 	    }
3767c478bd9Sstevel@tonic-gate 	  /* Exclude the moved top segment from further swapping.  */
3777c478bd9Sstevel@tonic-gate 	  bottom += len;
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate     }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate   /* Update records for the slots the non-options now occupy.  */
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate   first_nonopt += (optind - last_nonopt);
3847c478bd9Sstevel@tonic-gate   last_nonopt = optind;
3857c478bd9Sstevel@tonic-gate }
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /* Initialize the internal data when the first call is made.  */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate #if defined __STDC__ && __STDC__
3907c478bd9Sstevel@tonic-gate static const char *_getopt_initialize (int, char *const *, const char *);
3917c478bd9Sstevel@tonic-gate #endif
3927c478bd9Sstevel@tonic-gate static const char *
_getopt_initialize(argc,argv,optstring)3937c478bd9Sstevel@tonic-gate _getopt_initialize (argc, argv, optstring)
3947c478bd9Sstevel@tonic-gate      int argc;
3957c478bd9Sstevel@tonic-gate      char *const *argv;
3967c478bd9Sstevel@tonic-gate      const char *optstring;
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate   /* Start processing options with ARGV-element 1 (since ARGV-element 0
3997c478bd9Sstevel@tonic-gate      is the program name); the sequence of previously skipped
4007c478bd9Sstevel@tonic-gate      non-option ARGV-elements is empty.  */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate   first_nonopt = last_nonopt = optind;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate   nextchar = NULL;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate   posixly_correct = getenv ("POSIXLY_CORRECT");
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate   /* Determine how to handle the ordering of options and nonoptions.  */
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate   if (optstring[0] == '-')
4117c478bd9Sstevel@tonic-gate     {
4127c478bd9Sstevel@tonic-gate       ordering = RETURN_IN_ORDER;
4137c478bd9Sstevel@tonic-gate       ++optstring;
4147c478bd9Sstevel@tonic-gate     }
4157c478bd9Sstevel@tonic-gate   else if (optstring[0] == '+')
4167c478bd9Sstevel@tonic-gate     {
4177c478bd9Sstevel@tonic-gate       ordering = REQUIRE_ORDER;
4187c478bd9Sstevel@tonic-gate       ++optstring;
4197c478bd9Sstevel@tonic-gate     }
4207c478bd9Sstevel@tonic-gate   else if (posixly_correct != NULL)
4217c478bd9Sstevel@tonic-gate     ordering = REQUIRE_ORDER;
4227c478bd9Sstevel@tonic-gate   else
4237c478bd9Sstevel@tonic-gate     ordering = PERMUTE;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate #ifdef _LIBC
4267c478bd9Sstevel@tonic-gate   if (posixly_correct == NULL
4277c478bd9Sstevel@tonic-gate       && argc == original_argc && argv == original_argv)
4287c478bd9Sstevel@tonic-gate     {
4297c478bd9Sstevel@tonic-gate       if (nonoption_flags_max_len == 0)
4307c478bd9Sstevel@tonic-gate 	{
4317c478bd9Sstevel@tonic-gate 	  if (__getopt_nonoption_flags == NULL
4327c478bd9Sstevel@tonic-gate 	      || __getopt_nonoption_flags[0] == '\0')
4337c478bd9Sstevel@tonic-gate 	    nonoption_flags_max_len = -1;
4347c478bd9Sstevel@tonic-gate 	  else
4357c478bd9Sstevel@tonic-gate 	    {
4367c478bd9Sstevel@tonic-gate 	      const char *orig_str = __getopt_nonoption_flags;
4377c478bd9Sstevel@tonic-gate 	      int len = nonoption_flags_max_len = strlen (orig_str);
4387c478bd9Sstevel@tonic-gate 	      if (nonoption_flags_max_len < argc)
4397c478bd9Sstevel@tonic-gate 		nonoption_flags_max_len = argc;
4407c478bd9Sstevel@tonic-gate 	      __getopt_nonoption_flags =
4417c478bd9Sstevel@tonic-gate 		(char *) malloc (nonoption_flags_max_len);
4427c478bd9Sstevel@tonic-gate 	      if (__getopt_nonoption_flags == NULL)
4437c478bd9Sstevel@tonic-gate 		nonoption_flags_max_len = -1;
4447c478bd9Sstevel@tonic-gate 	      else
4457c478bd9Sstevel@tonic-gate 		memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
4467c478bd9Sstevel@tonic-gate 			'\0', nonoption_flags_max_len - len);
4477c478bd9Sstevel@tonic-gate 	    }
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate       nonoption_flags_len = nonoption_flags_max_len;
4507c478bd9Sstevel@tonic-gate     }
4517c478bd9Sstevel@tonic-gate   else
4527c478bd9Sstevel@tonic-gate     nonoption_flags_len = 0;
4537c478bd9Sstevel@tonic-gate #endif
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate   return optstring;
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /* Scan elements of ARGV (whose length is ARGC) for option characters
4597c478bd9Sstevel@tonic-gate    given in OPTSTRING.
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate    If an element of ARGV starts with '-', and is not exactly "-" or "--",
4627c478bd9Sstevel@tonic-gate    then it is an option element.  The characters of this element
4637c478bd9Sstevel@tonic-gate    (aside from the initial '-') are option characters.  If `getopt'
4647c478bd9Sstevel@tonic-gate    is called repeatedly, it returns successively each of the option characters
4657c478bd9Sstevel@tonic-gate    from each of the option elements.
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate    If `getopt' finds another option character, it returns that character,
4687c478bd9Sstevel@tonic-gate    updating `optind' and `nextchar' so that the next call to `getopt' can
4697c478bd9Sstevel@tonic-gate    resume the scan with the following option character or ARGV-element.
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate    If there are no more option characters, `getopt' returns -1.
4727c478bd9Sstevel@tonic-gate    Then `optind' is the index in ARGV of the first ARGV-element
4737c478bd9Sstevel@tonic-gate    that is not an option.  (The ARGV-elements have been permuted
4747c478bd9Sstevel@tonic-gate    so that those that are not options now come last.)
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate    OPTSTRING is a string containing the legitimate option characters.
4777c478bd9Sstevel@tonic-gate    If an option character is seen that is not listed in OPTSTRING,
4787c478bd9Sstevel@tonic-gate    return '?' after printing an error message.  If you set `opterr' to
4797c478bd9Sstevel@tonic-gate    zero, the error message is suppressed but we still return '?'.
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
4827c478bd9Sstevel@tonic-gate    so the following text in the same ARGV-element, or the text of the following
4837c478bd9Sstevel@tonic-gate    ARGV-element, is returned in `optarg'.  Two colons mean an option that
4847c478bd9Sstevel@tonic-gate    wants an optional arg; if there is text in the current ARGV-element,
4857c478bd9Sstevel@tonic-gate    it is returned in `optarg', otherwise `optarg' is set to zero.
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate    If OPTSTRING starts with `-' or `+', it requests different methods of
4887c478bd9Sstevel@tonic-gate    handling the non-option ARGV-elements.
4897c478bd9Sstevel@tonic-gate    See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate    Long-named options begin with `--' instead of `-'.
4927c478bd9Sstevel@tonic-gate    Their names may be abbreviated as long as the abbreviation is unique
4937c478bd9Sstevel@tonic-gate    or is an exact match for some defined option.  If they have an
4947c478bd9Sstevel@tonic-gate    argument, it follows the option name in the same ARGV-element, separated
4957c478bd9Sstevel@tonic-gate    from the option name by a `=', or else the in next ARGV-element.
4967c478bd9Sstevel@tonic-gate    When `getopt' finds a long-named option, it returns 0 if that option's
4977c478bd9Sstevel@tonic-gate    `flag' field is nonzero, the value of the option's `val' field
4987c478bd9Sstevel@tonic-gate    if the `flag' field is zero.
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate    The elements of ARGV aren't really const, because we permute them.
5017c478bd9Sstevel@tonic-gate    But we pretend they're const in the prototype to be compatible
5027c478bd9Sstevel@tonic-gate    with other systems.
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate    LONGOPTS is a vector of `struct option' terminated by an
5057c478bd9Sstevel@tonic-gate    element containing a name which is zero.
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate    LONGIND returns the index in LONGOPT of the long-named option found.
5087c478bd9Sstevel@tonic-gate    It is only valid when a long-named option has been found by the most
5097c478bd9Sstevel@tonic-gate    recent call.
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
5127c478bd9Sstevel@tonic-gate    long-named options.  */
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate int
_getopt_internal(argc,argv,optstring,longopts,longind,long_only)5157c478bd9Sstevel@tonic-gate _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
5167c478bd9Sstevel@tonic-gate      int argc;
5177c478bd9Sstevel@tonic-gate      char *const *argv;
5187c478bd9Sstevel@tonic-gate      const char *optstring;
5197c478bd9Sstevel@tonic-gate      const struct option *longopts;
5207c478bd9Sstevel@tonic-gate      int *longind;
5217c478bd9Sstevel@tonic-gate      int long_only;
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate   optarg = NULL;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate   if (optind == 0 || !__getopt_initialized)
5267c478bd9Sstevel@tonic-gate     {
5277c478bd9Sstevel@tonic-gate       if (optind == 0)
5287c478bd9Sstevel@tonic-gate 	optind = 1;	/* Don't scan ARGV[0], the program name.  */
5297c478bd9Sstevel@tonic-gate       optstring = _getopt_initialize (argc, argv, optstring);
5307c478bd9Sstevel@tonic-gate       __getopt_initialized = 1;
5317c478bd9Sstevel@tonic-gate     }
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate   /* Test whether ARGV[optind] points to a non-option argument.
5347c478bd9Sstevel@tonic-gate      Either it does not have option syntax, or there is an environment flag
5357c478bd9Sstevel@tonic-gate      from the shell indicating it is not an option.  The later information
5367c478bd9Sstevel@tonic-gate      is only used when the used in the GNU libc.  */
5377c478bd9Sstevel@tonic-gate #ifdef _LIBC
5387c478bd9Sstevel@tonic-gate # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'	      \
5397c478bd9Sstevel@tonic-gate 		      || (optind < nonoption_flags_len			      \
5407c478bd9Sstevel@tonic-gate 			  && __getopt_nonoption_flags[optind] == '1'))
5417c478bd9Sstevel@tonic-gate #else
5427c478bd9Sstevel@tonic-gate # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate   if (nextchar == NULL || *nextchar == '\0')
5467c478bd9Sstevel@tonic-gate     {
5477c478bd9Sstevel@tonic-gate       /* Advance to the next ARGV-element.  */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate       /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
5507c478bd9Sstevel@tonic-gate 	 moved back by the user (who may also have changed the arguments).  */
5517c478bd9Sstevel@tonic-gate       if (last_nonopt > optind)
5527c478bd9Sstevel@tonic-gate 	last_nonopt = optind;
5537c478bd9Sstevel@tonic-gate       if (first_nonopt > optind)
5547c478bd9Sstevel@tonic-gate 	first_nonopt = optind;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate       if (ordering == PERMUTE)
5577c478bd9Sstevel@tonic-gate 	{
5587c478bd9Sstevel@tonic-gate 	  /* If we have just processed some options following some non-options,
5597c478bd9Sstevel@tonic-gate 	     exchange them so that the options come first.  */
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	  if (first_nonopt != last_nonopt && last_nonopt != optind)
5627c478bd9Sstevel@tonic-gate 	    exchange ((char **) argv);
5637c478bd9Sstevel@tonic-gate 	  else if (last_nonopt != optind)
5647c478bd9Sstevel@tonic-gate 	    first_nonopt = optind;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	  /* Skip any additional non-options
5677c478bd9Sstevel@tonic-gate 	     and extend the range of non-options previously skipped.  */
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	  while (optind < argc && NONOPTION_P)
5707c478bd9Sstevel@tonic-gate 	    optind++;
5717c478bd9Sstevel@tonic-gate 	  last_nonopt = optind;
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate       /* The special ARGV-element `--' means premature end of options.
5757c478bd9Sstevel@tonic-gate 	 Skip it like a null option,
5767c478bd9Sstevel@tonic-gate 	 then exchange with previous non-options as if it were an option,
5777c478bd9Sstevel@tonic-gate 	 then skip everything else like a non-option.  */
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate       if (optind != argc && !strcmp (argv[optind], "--"))
5807c478bd9Sstevel@tonic-gate 	{
5817c478bd9Sstevel@tonic-gate 	  optind++;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	  if (first_nonopt != last_nonopt && last_nonopt != optind)
5847c478bd9Sstevel@tonic-gate 	    exchange ((char **) argv);
5857c478bd9Sstevel@tonic-gate 	  else if (first_nonopt == last_nonopt)
5867c478bd9Sstevel@tonic-gate 	    first_nonopt = optind;
5877c478bd9Sstevel@tonic-gate 	  last_nonopt = argc;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	  optind = argc;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate       /* If we have done all the ARGV-elements, stop the scan
5937c478bd9Sstevel@tonic-gate 	 and back over any non-options that we skipped and permuted.  */
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate       if (optind == argc)
5967c478bd9Sstevel@tonic-gate 	{
5977c478bd9Sstevel@tonic-gate 	  /* Set the next-arg-index to point at the non-options
5987c478bd9Sstevel@tonic-gate 	     that we previously skipped, so the caller will digest them.  */
5997c478bd9Sstevel@tonic-gate 	  if (first_nonopt != last_nonopt)
6007c478bd9Sstevel@tonic-gate 	    optind = first_nonopt;
6017c478bd9Sstevel@tonic-gate 	  return -1;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate       /* If we have come to a non-option and did not permute it,
6057c478bd9Sstevel@tonic-gate 	 either stop the scan or describe it to the caller and pass it by.  */
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate       if (NONOPTION_P)
6087c478bd9Sstevel@tonic-gate 	{
6097c478bd9Sstevel@tonic-gate 	  if (ordering == REQUIRE_ORDER)
6107c478bd9Sstevel@tonic-gate 	    return -1;
6117c478bd9Sstevel@tonic-gate 	  optarg = argv[optind++];
6127c478bd9Sstevel@tonic-gate 	  return 1;
6137c478bd9Sstevel@tonic-gate 	}
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate       /* We have found another option-ARGV-element.
6167c478bd9Sstevel@tonic-gate 	 Skip the initial punctuation.  */
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate       nextchar = (argv[optind] + 1
6197c478bd9Sstevel@tonic-gate 		  + (longopts != NULL && argv[optind][1] == '-'));
6207c478bd9Sstevel@tonic-gate     }
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate   /* Decode the current option-ARGV-element.  */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate   /* Check whether the ARGV-element is a long option.
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate      If long_only and the ARGV-element has the form "-f", where f is
6277c478bd9Sstevel@tonic-gate      a valid short option, don't consider it an abbreviated form of
6287c478bd9Sstevel@tonic-gate      a long option that starts with f.  Otherwise there would be no
6297c478bd9Sstevel@tonic-gate      way to give the -f short option.
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate      On the other hand, if there's a long option "fubar" and
6327c478bd9Sstevel@tonic-gate      the ARGV-element is "-fu", do consider that an abbreviation of
6337c478bd9Sstevel@tonic-gate      the long option, just like "--fu", and not "-f" with arg "u".
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate      This distinction seems to be the most useful approach.  */
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate   if (longopts != NULL
6387c478bd9Sstevel@tonic-gate       && (argv[optind][1] == '-'
6397c478bd9Sstevel@tonic-gate 	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
6407c478bd9Sstevel@tonic-gate     {
6417c478bd9Sstevel@tonic-gate       char *nameend;
6427c478bd9Sstevel@tonic-gate       const struct option *p;
6437c478bd9Sstevel@tonic-gate       const struct option *pfound = NULL;
6447c478bd9Sstevel@tonic-gate       int exact = 0;
6457c478bd9Sstevel@tonic-gate       int ambig = 0;
6467c478bd9Sstevel@tonic-gate       int indfound = -1;
6477c478bd9Sstevel@tonic-gate       int option_index;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate       for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
6507c478bd9Sstevel@tonic-gate 	/* Do nothing.  */ ;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate       /* Test all long options for either exact match
6537c478bd9Sstevel@tonic-gate 	 or abbreviated matches.  */
6547c478bd9Sstevel@tonic-gate       for (p = longopts, option_index = 0; p->name; p++, option_index++)
6557c478bd9Sstevel@tonic-gate 	if (!strncmp (p->name, nextchar, nameend - nextchar))
6567c478bd9Sstevel@tonic-gate 	  {
6577c478bd9Sstevel@tonic-gate 	    if ((unsigned int) (nameend - nextchar)
6587c478bd9Sstevel@tonic-gate 		== (unsigned int) strlen (p->name))
6597c478bd9Sstevel@tonic-gate 	      {
6607c478bd9Sstevel@tonic-gate 		/* Exact match found.  */
6617c478bd9Sstevel@tonic-gate 		pfound = p;
6627c478bd9Sstevel@tonic-gate 		indfound = option_index;
6637c478bd9Sstevel@tonic-gate 		exact = 1;
6647c478bd9Sstevel@tonic-gate 		break;
6657c478bd9Sstevel@tonic-gate 	      }
6667c478bd9Sstevel@tonic-gate 	    else if (pfound == NULL)
6677c478bd9Sstevel@tonic-gate 	      {
6687c478bd9Sstevel@tonic-gate 		/* First nonexact match found.  */
6697c478bd9Sstevel@tonic-gate 		pfound = p;
6707c478bd9Sstevel@tonic-gate 		indfound = option_index;
6717c478bd9Sstevel@tonic-gate 	      }
6727c478bd9Sstevel@tonic-gate 	    else
6737c478bd9Sstevel@tonic-gate 	      /* Second or later nonexact match found.  */
6747c478bd9Sstevel@tonic-gate 	      ambig = 1;
6757c478bd9Sstevel@tonic-gate 	  }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate       if (ambig && !exact)
6787c478bd9Sstevel@tonic-gate 	{
6797c478bd9Sstevel@tonic-gate 	  if (opterr)
6807c478bd9Sstevel@tonic-gate 	    fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
6817c478bd9Sstevel@tonic-gate 		     argv[0], argv[optind]);
6827c478bd9Sstevel@tonic-gate 	  nextchar += strlen (nextchar);
6837c478bd9Sstevel@tonic-gate 	  optind++;
6847c478bd9Sstevel@tonic-gate 	  optopt = 0;
6857c478bd9Sstevel@tonic-gate 	  return '?';
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate       if (pfound != NULL)
6897c478bd9Sstevel@tonic-gate 	{
6907c478bd9Sstevel@tonic-gate 	  option_index = indfound;
6917c478bd9Sstevel@tonic-gate 	  optind++;
6927c478bd9Sstevel@tonic-gate 	  if (*nameend)
6937c478bd9Sstevel@tonic-gate 	    {
6947c478bd9Sstevel@tonic-gate 	      /* Don't test has_arg with >, because some C compilers don't
6957c478bd9Sstevel@tonic-gate 		 allow it to be used on enums.  */
6967c478bd9Sstevel@tonic-gate 	      if (pfound->has_arg)
6977c478bd9Sstevel@tonic-gate 		optarg = nameend + 1;
6987c478bd9Sstevel@tonic-gate 	      else
6997c478bd9Sstevel@tonic-gate 		{
7007c478bd9Sstevel@tonic-gate 		  if (opterr)
7017c478bd9Sstevel@tonic-gate 		    {
7027c478bd9Sstevel@tonic-gate 		      if (argv[optind - 1][1] == '-')
7037c478bd9Sstevel@tonic-gate 			/* --option */
7047c478bd9Sstevel@tonic-gate 			fprintf (stderr,
7057c478bd9Sstevel@tonic-gate 				 _("%s: option `--%s' doesn't allow an argument\n"),
7067c478bd9Sstevel@tonic-gate 				 argv[0], pfound->name);
7077c478bd9Sstevel@tonic-gate 		      else
7087c478bd9Sstevel@tonic-gate 			/* +option or -option */
7097c478bd9Sstevel@tonic-gate 			fprintf (stderr,
7107c478bd9Sstevel@tonic-gate 				 _("%s: option `%c%s' doesn't allow an argument\n"),
7117c478bd9Sstevel@tonic-gate 				 argv[0], argv[optind - 1][0], pfound->name);
7127c478bd9Sstevel@tonic-gate 		    }
7137c478bd9Sstevel@tonic-gate 		  nextchar += strlen (nextchar);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		  optopt = pfound->val;
7167c478bd9Sstevel@tonic-gate 		  return '?';
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 	    }
7197c478bd9Sstevel@tonic-gate 	  else if (pfound->has_arg == 1)
7207c478bd9Sstevel@tonic-gate 	    {
7217c478bd9Sstevel@tonic-gate 	      if (optind < argc)
7227c478bd9Sstevel@tonic-gate 		optarg = argv[optind++];
7237c478bd9Sstevel@tonic-gate 	      else
7247c478bd9Sstevel@tonic-gate 		{
7257c478bd9Sstevel@tonic-gate 		  if (opterr)
7267c478bd9Sstevel@tonic-gate 		    fprintf (stderr,
7277c478bd9Sstevel@tonic-gate 			   _("%s: option `%s' requires an argument\n"),
7287c478bd9Sstevel@tonic-gate 			   argv[0], argv[optind - 1]);
7297c478bd9Sstevel@tonic-gate 		  nextchar += strlen (nextchar);
7307c478bd9Sstevel@tonic-gate 		  optopt = pfound->val;
7317c478bd9Sstevel@tonic-gate 		  return optstring[0] == ':' ? ':' : '?';
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 	    }
7347c478bd9Sstevel@tonic-gate 	  nextchar += strlen (nextchar);
7357c478bd9Sstevel@tonic-gate 	  if (longind != NULL)
7367c478bd9Sstevel@tonic-gate 	    *longind = option_index;
7377c478bd9Sstevel@tonic-gate 	  if (pfound->flag)
7387c478bd9Sstevel@tonic-gate 	    {
7397c478bd9Sstevel@tonic-gate 	      *(pfound->flag) = pfound->val;
7407c478bd9Sstevel@tonic-gate 	      return 0;
7417c478bd9Sstevel@tonic-gate 	    }
7427c478bd9Sstevel@tonic-gate 	  return pfound->val;
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate       /* Can't find it as a long option.  If this is not getopt_long_only,
7467c478bd9Sstevel@tonic-gate 	 or the option starts with '--' or is not a valid short
7477c478bd9Sstevel@tonic-gate 	 option, then it's an error.
7487c478bd9Sstevel@tonic-gate 	 Otherwise interpret it as a short option.  */
7497c478bd9Sstevel@tonic-gate       if (!long_only || argv[optind][1] == '-'
7507c478bd9Sstevel@tonic-gate 	  || my_index (optstring, *nextchar) == NULL)
7517c478bd9Sstevel@tonic-gate 	{
7527c478bd9Sstevel@tonic-gate 	  if (opterr)
7537c478bd9Sstevel@tonic-gate 	    {
7547c478bd9Sstevel@tonic-gate 	      if (argv[optind][1] == '-')
7557c478bd9Sstevel@tonic-gate 		/* --option */
7567c478bd9Sstevel@tonic-gate 		fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
7577c478bd9Sstevel@tonic-gate 			 argv[0], nextchar);
7587c478bd9Sstevel@tonic-gate 	      else
7597c478bd9Sstevel@tonic-gate 		/* +option or -option */
7607c478bd9Sstevel@tonic-gate 		fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
7617c478bd9Sstevel@tonic-gate 			 argv[0], argv[optind][0], nextchar);
7627c478bd9Sstevel@tonic-gate 	    }
7637c478bd9Sstevel@tonic-gate 	  nextchar = (char *) "";
7647c478bd9Sstevel@tonic-gate 	  optind++;
7657c478bd9Sstevel@tonic-gate 	  optopt = 0;
7667c478bd9Sstevel@tonic-gate 	  return '?';
7677c478bd9Sstevel@tonic-gate 	}
7687c478bd9Sstevel@tonic-gate     }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate   /* Look at and handle the next short option-character.  */
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate   {
7737c478bd9Sstevel@tonic-gate     char c = *nextchar++;
7747c478bd9Sstevel@tonic-gate     char *temp = my_index (optstring, c);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate     /* Increment `optind' when we start to process its last character.  */
7777c478bd9Sstevel@tonic-gate     if (*nextchar == '\0')
7787c478bd9Sstevel@tonic-gate       ++optind;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate     if (temp == NULL || c == ':')
7817c478bd9Sstevel@tonic-gate       {
7827c478bd9Sstevel@tonic-gate 	if (opterr)
7837c478bd9Sstevel@tonic-gate 	  {
7847c478bd9Sstevel@tonic-gate 	    if (posixly_correct)
7857c478bd9Sstevel@tonic-gate 	      /* 1003.2 specifies the format of this message.  */
7867c478bd9Sstevel@tonic-gate 	      fprintf (stderr, _("%s: illegal option -- %c\n"),
7877c478bd9Sstevel@tonic-gate 		       argv[0], c);
7887c478bd9Sstevel@tonic-gate 	    else
7897c478bd9Sstevel@tonic-gate 	      fprintf (stderr, _("%s: invalid option -- %c\n"),
7907c478bd9Sstevel@tonic-gate 		       argv[0], c);
7917c478bd9Sstevel@tonic-gate 	  }
7927c478bd9Sstevel@tonic-gate 	optopt = c;
7937c478bd9Sstevel@tonic-gate 	return '?';
7947c478bd9Sstevel@tonic-gate       }
7957c478bd9Sstevel@tonic-gate     /* Convenience. Treat POSIX -W foo same as long option --foo */
7967c478bd9Sstevel@tonic-gate     if (temp[0] == 'W' && temp[1] == ';')
7977c478bd9Sstevel@tonic-gate       {
7987c478bd9Sstevel@tonic-gate 	char *nameend;
7997c478bd9Sstevel@tonic-gate 	const struct option *p;
8007c478bd9Sstevel@tonic-gate 	const struct option *pfound = NULL;
8017c478bd9Sstevel@tonic-gate 	int exact = 0;
8027c478bd9Sstevel@tonic-gate 	int ambig = 0;
8037c478bd9Sstevel@tonic-gate 	int indfound = 0;
8047c478bd9Sstevel@tonic-gate 	int option_index;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/* This is an option that requires an argument.  */
8077c478bd9Sstevel@tonic-gate 	if (*nextchar != '\0')
8087c478bd9Sstevel@tonic-gate 	  {
8097c478bd9Sstevel@tonic-gate 	    optarg = nextchar;
8107c478bd9Sstevel@tonic-gate 	    /* If we end this ARGV-element by taking the rest as an arg,
8117c478bd9Sstevel@tonic-gate 	       we must advance to the next element now.  */
8127c478bd9Sstevel@tonic-gate 	    optind++;
8137c478bd9Sstevel@tonic-gate 	  }
8147c478bd9Sstevel@tonic-gate 	else if (optind == argc)
8157c478bd9Sstevel@tonic-gate 	  {
8167c478bd9Sstevel@tonic-gate 	    if (opterr)
8177c478bd9Sstevel@tonic-gate 	      {
8187c478bd9Sstevel@tonic-gate 		/* 1003.2 specifies the format of this message.  */
8197c478bd9Sstevel@tonic-gate 		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
8207c478bd9Sstevel@tonic-gate 			 argv[0], c);
8217c478bd9Sstevel@tonic-gate 	      }
8227c478bd9Sstevel@tonic-gate 	    optopt = c;
8237c478bd9Sstevel@tonic-gate 	    if (optstring[0] == ':')
8247c478bd9Sstevel@tonic-gate 	      c = ':';
8257c478bd9Sstevel@tonic-gate 	    else
8267c478bd9Sstevel@tonic-gate 	      c = '?';
8277c478bd9Sstevel@tonic-gate 	    return c;
8287c478bd9Sstevel@tonic-gate 	  }
8297c478bd9Sstevel@tonic-gate 	else
8307c478bd9Sstevel@tonic-gate 	  /* We already incremented `optind' once;
8317c478bd9Sstevel@tonic-gate 	     increment it again when taking next ARGV-elt as argument.  */
8327c478bd9Sstevel@tonic-gate 	  optarg = argv[optind++];
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	/* optarg is now the argument, see if it's in the
8357c478bd9Sstevel@tonic-gate 	   table of longopts.  */
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
8387c478bd9Sstevel@tonic-gate 	  /* Do nothing.  */ ;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	/* Test all long options for either exact match
8417c478bd9Sstevel@tonic-gate 	   or abbreviated matches.  */
8427c478bd9Sstevel@tonic-gate 	for (p = longopts, option_index = 0; p->name; p++, option_index++)
8437c478bd9Sstevel@tonic-gate 	  if (!strncmp (p->name, nextchar, nameend - nextchar))
8447c478bd9Sstevel@tonic-gate 	    {
8457c478bd9Sstevel@tonic-gate 	      if ((unsigned int) (nameend - nextchar) == strlen (p->name))
8467c478bd9Sstevel@tonic-gate 		{
8477c478bd9Sstevel@tonic-gate 		  /* Exact match found.  */
8487c478bd9Sstevel@tonic-gate 		  pfound = p;
8497c478bd9Sstevel@tonic-gate 		  indfound = option_index;
8507c478bd9Sstevel@tonic-gate 		  exact = 1;
8517c478bd9Sstevel@tonic-gate 		  break;
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 	      else if (pfound == NULL)
8547c478bd9Sstevel@tonic-gate 		{
8557c478bd9Sstevel@tonic-gate 		  /* First nonexact match found.  */
8567c478bd9Sstevel@tonic-gate 		  pfound = p;
8577c478bd9Sstevel@tonic-gate 		  indfound = option_index;
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 	      else
8607c478bd9Sstevel@tonic-gate 		/* Second or later nonexact match found.  */
8617c478bd9Sstevel@tonic-gate 		ambig = 1;
8627c478bd9Sstevel@tonic-gate 	    }
8637c478bd9Sstevel@tonic-gate 	if (ambig && !exact)
8647c478bd9Sstevel@tonic-gate 	  {
8657c478bd9Sstevel@tonic-gate 	    if (opterr)
8667c478bd9Sstevel@tonic-gate 	      fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
8677c478bd9Sstevel@tonic-gate 		       argv[0], argv[optind]);
8687c478bd9Sstevel@tonic-gate 	    nextchar += strlen (nextchar);
8697c478bd9Sstevel@tonic-gate 	    optind++;
8707c478bd9Sstevel@tonic-gate 	    return '?';
8717c478bd9Sstevel@tonic-gate 	  }
8727c478bd9Sstevel@tonic-gate 	if (pfound != NULL)
8737c478bd9Sstevel@tonic-gate 	  {
8747c478bd9Sstevel@tonic-gate 	    option_index = indfound;
8757c478bd9Sstevel@tonic-gate 	    if (*nameend)
8767c478bd9Sstevel@tonic-gate 	      {
8777c478bd9Sstevel@tonic-gate 		/* Don't test has_arg with >, because some C compilers don't
8787c478bd9Sstevel@tonic-gate 		   allow it to be used on enums.  */
8797c478bd9Sstevel@tonic-gate 		if (pfound->has_arg)
8807c478bd9Sstevel@tonic-gate 		  optarg = nameend + 1;
8817c478bd9Sstevel@tonic-gate 		else
8827c478bd9Sstevel@tonic-gate 		  {
8837c478bd9Sstevel@tonic-gate 		    if (opterr)
8847c478bd9Sstevel@tonic-gate 		      fprintf (stderr, _("\
8857c478bd9Sstevel@tonic-gate %s: option `-W %s' doesn't allow an argument\n"),
8867c478bd9Sstevel@tonic-gate 			       argv[0], pfound->name);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		    nextchar += strlen (nextchar);
8897c478bd9Sstevel@tonic-gate 		    return '?';
8907c478bd9Sstevel@tonic-gate 		  }
8917c478bd9Sstevel@tonic-gate 	      }
8927c478bd9Sstevel@tonic-gate 	    else if (pfound->has_arg == 1)
8937c478bd9Sstevel@tonic-gate 	      {
8947c478bd9Sstevel@tonic-gate 		if (optind < argc)
8957c478bd9Sstevel@tonic-gate 		  optarg = argv[optind++];
8967c478bd9Sstevel@tonic-gate 		else
8977c478bd9Sstevel@tonic-gate 		  {
8987c478bd9Sstevel@tonic-gate 		    if (opterr)
8997c478bd9Sstevel@tonic-gate 		      fprintf (stderr,
9007c478bd9Sstevel@tonic-gate 			       _("%s: option `%s' requires an argument\n"),
9017c478bd9Sstevel@tonic-gate 			       argv[0], argv[optind - 1]);
9027c478bd9Sstevel@tonic-gate 		    nextchar += strlen (nextchar);
9037c478bd9Sstevel@tonic-gate 		    return optstring[0] == ':' ? ':' : '?';
9047c478bd9Sstevel@tonic-gate 		  }
9057c478bd9Sstevel@tonic-gate 	      }
9067c478bd9Sstevel@tonic-gate 	    nextchar += strlen (nextchar);
9077c478bd9Sstevel@tonic-gate 	    if (longind != NULL)
9087c478bd9Sstevel@tonic-gate 	      *longind = option_index;
9097c478bd9Sstevel@tonic-gate 	    if (pfound->flag)
9107c478bd9Sstevel@tonic-gate 	      {
9117c478bd9Sstevel@tonic-gate 		*(pfound->flag) = pfound->val;
9127c478bd9Sstevel@tonic-gate 		return 0;
9137c478bd9Sstevel@tonic-gate 	      }
9147c478bd9Sstevel@tonic-gate 	    return pfound->val;
9157c478bd9Sstevel@tonic-gate 	  }
9167c478bd9Sstevel@tonic-gate 	  nextchar = NULL;
9177c478bd9Sstevel@tonic-gate 	  return 'W';	/* Let the application handle it.   */
9187c478bd9Sstevel@tonic-gate       }
9197c478bd9Sstevel@tonic-gate     if (temp[1] == ':')
9207c478bd9Sstevel@tonic-gate       {
9217c478bd9Sstevel@tonic-gate 	if (temp[2] == ':')
9227c478bd9Sstevel@tonic-gate 	  {
9237c478bd9Sstevel@tonic-gate 	    /* This is an option that accepts an argument optionally.  */
9247c478bd9Sstevel@tonic-gate 	    if (*nextchar != '\0')
9257c478bd9Sstevel@tonic-gate 	      {
9267c478bd9Sstevel@tonic-gate 		optarg = nextchar;
9277c478bd9Sstevel@tonic-gate 		optind++;
9287c478bd9Sstevel@tonic-gate 	      }
9297c478bd9Sstevel@tonic-gate 	    else
9307c478bd9Sstevel@tonic-gate 	      optarg = NULL;
9317c478bd9Sstevel@tonic-gate 	    nextchar = NULL;
9327c478bd9Sstevel@tonic-gate 	  }
9337c478bd9Sstevel@tonic-gate 	else
9347c478bd9Sstevel@tonic-gate 	  {
9357c478bd9Sstevel@tonic-gate 	    /* This is an option that requires an argument.  */
9367c478bd9Sstevel@tonic-gate 	    if (*nextchar != '\0')
9377c478bd9Sstevel@tonic-gate 	      {
9387c478bd9Sstevel@tonic-gate 		optarg = nextchar;
9397c478bd9Sstevel@tonic-gate 		/* If we end this ARGV-element by taking the rest as an arg,
9407c478bd9Sstevel@tonic-gate 		   we must advance to the next element now.  */
9417c478bd9Sstevel@tonic-gate 		optind++;
9427c478bd9Sstevel@tonic-gate 	      }
9437c478bd9Sstevel@tonic-gate 	    else if (optind == argc)
9447c478bd9Sstevel@tonic-gate 	      {
9457c478bd9Sstevel@tonic-gate 		if (opterr)
9467c478bd9Sstevel@tonic-gate 		  {
9477c478bd9Sstevel@tonic-gate 		    /* 1003.2 specifies the format of this message.  */
9487c478bd9Sstevel@tonic-gate 		    fprintf (stderr,
9497c478bd9Sstevel@tonic-gate 			   _("%s: option requires an argument -- %c\n"),
9507c478bd9Sstevel@tonic-gate 			   argv[0], c);
9517c478bd9Sstevel@tonic-gate 		  }
9527c478bd9Sstevel@tonic-gate 		optopt = c;
9537c478bd9Sstevel@tonic-gate 		if (optstring[0] == ':')
9547c478bd9Sstevel@tonic-gate 		  c = ':';
9557c478bd9Sstevel@tonic-gate 		else
9567c478bd9Sstevel@tonic-gate 		  c = '?';
9577c478bd9Sstevel@tonic-gate 	      }
9587c478bd9Sstevel@tonic-gate 	    else
9597c478bd9Sstevel@tonic-gate 	      /* We already incremented `optind' once;
9607c478bd9Sstevel@tonic-gate 		 increment it again when taking next ARGV-elt as argument.  */
9617c478bd9Sstevel@tonic-gate 	      optarg = argv[optind++];
9627c478bd9Sstevel@tonic-gate 	    nextchar = NULL;
9637c478bd9Sstevel@tonic-gate 	  }
9647c478bd9Sstevel@tonic-gate       }
9657c478bd9Sstevel@tonic-gate     return c;
9667c478bd9Sstevel@tonic-gate   }
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate int
getopt(argc,argv,optstring)9707c478bd9Sstevel@tonic-gate getopt (argc, argv, optstring)
9717c478bd9Sstevel@tonic-gate      int argc;
9727c478bd9Sstevel@tonic-gate      char *const *argv;
9737c478bd9Sstevel@tonic-gate      const char *optstring;
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate   return _getopt_internal (argc, argv, optstring,
9767c478bd9Sstevel@tonic-gate 			   (const struct option *) 0,
9777c478bd9Sstevel@tonic-gate 			   (int *) 0,
9787c478bd9Sstevel@tonic-gate 			   0);
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate #endif	/* Not ELIDE_CODE.  */
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate #ifdef TEST
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate /* Compile with -DTEST to make an executable for use in testing
9867c478bd9Sstevel@tonic-gate    the above definition of `getopt'.  */
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate int
main(argc,argv)9897c478bd9Sstevel@tonic-gate main (argc, argv)
9907c478bd9Sstevel@tonic-gate      int argc;
9917c478bd9Sstevel@tonic-gate      char **argv;
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate   int c;
9947c478bd9Sstevel@tonic-gate   int digit_optind = 0;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate   while (1)
9977c478bd9Sstevel@tonic-gate     {
9987c478bd9Sstevel@tonic-gate       int this_option_optind = optind ? optind : 1;
9997c478bd9Sstevel@tonic-gate 
10007c478bd9Sstevel@tonic-gate       c = getopt (argc, argv, "abc:d:0123456789");
10017c478bd9Sstevel@tonic-gate       if (c == -1)
10027c478bd9Sstevel@tonic-gate 	break;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate       switch (c)
10057c478bd9Sstevel@tonic-gate 	{
10067c478bd9Sstevel@tonic-gate 	case '0':
10077c478bd9Sstevel@tonic-gate 	case '1':
10087c478bd9Sstevel@tonic-gate 	case '2':
10097c478bd9Sstevel@tonic-gate 	case '3':
10107c478bd9Sstevel@tonic-gate 	case '4':
10117c478bd9Sstevel@tonic-gate 	case '5':
10127c478bd9Sstevel@tonic-gate 	case '6':
10137c478bd9Sstevel@tonic-gate 	case '7':
10147c478bd9Sstevel@tonic-gate 	case '8':
10157c478bd9Sstevel@tonic-gate 	case '9':
10167c478bd9Sstevel@tonic-gate 	  if (digit_optind != 0 && digit_optind != this_option_optind)
10177c478bd9Sstevel@tonic-gate 	    printf ("digits occur in two different argv-elements.\n");
10187c478bd9Sstevel@tonic-gate 	  digit_optind = this_option_optind;
10197c478bd9Sstevel@tonic-gate 	  printf ("option %c\n", c);
10207c478bd9Sstevel@tonic-gate 	  break;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	case 'a':
10237c478bd9Sstevel@tonic-gate 	  printf ("option a\n");
10247c478bd9Sstevel@tonic-gate 	  break;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	case 'b':
10277c478bd9Sstevel@tonic-gate 	  printf ("option b\n");
10287c478bd9Sstevel@tonic-gate 	  break;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	case 'c':
10317c478bd9Sstevel@tonic-gate 	  printf ("option c with value `%s'\n", optarg);
10327c478bd9Sstevel@tonic-gate 	  break;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	case '?':
10357c478bd9Sstevel@tonic-gate 	  break;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	default:
10387c478bd9Sstevel@tonic-gate 	  printf ("?? getopt returned character code 0%o ??\n", c);
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate     }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate   if (optind < argc)
10437c478bd9Sstevel@tonic-gate     {
10447c478bd9Sstevel@tonic-gate       printf ("non-option ARGV-elements: ");
10457c478bd9Sstevel@tonic-gate       while (optind < argc)
10467c478bd9Sstevel@tonic-gate 	printf ("%s ", argv[optind++]);
10477c478bd9Sstevel@tonic-gate       printf ("\n");
10487c478bd9Sstevel@tonic-gate     }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate   exit (0);
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate #endif /* TEST */
1054