1141cc406Sopenharmony_ci/* Getopt for GNU.
2141cc406Sopenharmony_ci   NOTE: getopt is now part of the C library, so if you don't know what
3141cc406Sopenharmony_ci   "Keep this file name-space clean" means, talk to drepper@gnu.org
4141cc406Sopenharmony_ci   before changing it!
5141cc406Sopenharmony_ci   Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002
6141cc406Sopenharmony_ci	Free Software Foundation, Inc.
7141cc406Sopenharmony_ci   This file is part of the GNU C Library.
8141cc406Sopenharmony_ci
9141cc406Sopenharmony_ci   The GNU C Library is free software; you can redistribute it and/or
10141cc406Sopenharmony_ci   modify it under the terms of the GNU Lesser General Public
11141cc406Sopenharmony_ci   License as published by the Free Software Foundation; either
12141cc406Sopenharmony_ci   version 2.1 of the License, or (at your option) any later version.
13141cc406Sopenharmony_ci
14141cc406Sopenharmony_ci   The GNU C Library is distributed in the hope that it will be useful,
15141cc406Sopenharmony_ci   but WITHOUT ANY WARRANTY; without even the implied warranty of
16141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17141cc406Sopenharmony_ci   Lesser General Public License for more details.
18141cc406Sopenharmony_ci
19141cc406Sopenharmony_ci   You should have received a copy of the GNU Lesser General Public
20141cc406Sopenharmony_ci   License along with the GNU C Library.
21141cc406Sopenharmony_ci   If not, see <https://www.gnu.org/licenses/>.  */
22141cc406Sopenharmony_ci# include <config.h>
23141cc406Sopenharmony_ci#if !defined(HAVE_GETOPT_H) || !defined(HAVE_GETOPT_LONG)
24141cc406Sopenharmony_ci
25141cc406Sopenharmony_ci/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
26141cc406Sopenharmony_ci   Ditto for AIX 3.2 and <stdlib.h>.  */
27141cc406Sopenharmony_ci#ifndef _NO_PROTO
28141cc406Sopenharmony_ci# define _NO_PROTO
29141cc406Sopenharmony_ci#endif
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_ci#if 0
32141cc406Sopenharmony_ci#ifdef HAVE_CONFIG_H
33141cc406Sopenharmony_ci# include <config.h>
34141cc406Sopenharmony_ci#endif
35141cc406Sopenharmony_ci#endif
36141cc406Sopenharmony_ci
37141cc406Sopenharmony_ci#if !defined __STDC__ || !__STDC__
38141cc406Sopenharmony_ci/* This is a separate conditional since some stdc systems
39141cc406Sopenharmony_ci   reject `defined (const)'.  */
40141cc406Sopenharmony_ci# ifndef const
41141cc406Sopenharmony_ci#  define const
42141cc406Sopenharmony_ci# endif
43141cc406Sopenharmony_ci#endif
44141cc406Sopenharmony_ci
45141cc406Sopenharmony_ci#include <stdio.h>
46141cc406Sopenharmony_ci
47141cc406Sopenharmony_ci/* Comment out all this code if we are using the GNU C Library, and are not
48141cc406Sopenharmony_ci   actually compiling the library itself.  This code is part of the GNU C
49141cc406Sopenharmony_ci   Library, but also included in many other GNU distributions.  Compiling
50141cc406Sopenharmony_ci   and linking in this code is a waste when using the GNU C library
51141cc406Sopenharmony_ci   (especially if it is a shared library).  Rather than having every GNU
52141cc406Sopenharmony_ci   program understand `configure --with-gnu-libc' and omit the object files,
53141cc406Sopenharmony_ci   it is simpler to just do this in the source for each such file.  */
54141cc406Sopenharmony_ci
55141cc406Sopenharmony_ci#define GETOPT_INTERFACE_VERSION 2
56141cc406Sopenharmony_ci#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
57141cc406Sopenharmony_ci# include <gnu-versions.h>
58141cc406Sopenharmony_ci# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
59141cc406Sopenharmony_ci#  define ELIDE_CODE
60141cc406Sopenharmony_ci# endif
61141cc406Sopenharmony_ci#endif
62141cc406Sopenharmony_ci
63141cc406Sopenharmony_ci#ifndef ELIDE_CODE
64141cc406Sopenharmony_ci
65141cc406Sopenharmony_ci
66141cc406Sopenharmony_ci/* This needs to come after some library #include
67141cc406Sopenharmony_ci   to get __GNU_LIBRARY__ defined.  */
68141cc406Sopenharmony_ci#ifdef	__GNU_LIBRARY__
69141cc406Sopenharmony_ci/* Don't include stdlib.h for non-GNU C libraries because some of them
70141cc406Sopenharmony_ci   contain conflicting prototypes for getopt.  */
71141cc406Sopenharmony_ci# include <stdlib.h>
72141cc406Sopenharmony_ci# include <unistd.h>
73141cc406Sopenharmony_ci#endif	/* GNU C library.  */
74141cc406Sopenharmony_ci
75141cc406Sopenharmony_ci#ifdef VMS
76141cc406Sopenharmony_ci# include <unixlib.h>
77141cc406Sopenharmony_ci# if HAVE_STRING_H - 0
78141cc406Sopenharmony_ci#  include <string.h>
79141cc406Sopenharmony_ci# endif
80141cc406Sopenharmony_ci#endif
81141cc406Sopenharmony_ci
82141cc406Sopenharmony_ci#ifndef _
83141cc406Sopenharmony_ci/* This is for other GNU distributions with internationalized messages.  */
84141cc406Sopenharmony_ci# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
85141cc406Sopenharmony_ci#  include <libintl.h>
86141cc406Sopenharmony_ci#  ifndef _
87141cc406Sopenharmony_ci#   define _(msgid)	gettext (msgid)
88141cc406Sopenharmony_ci#  endif
89141cc406Sopenharmony_ci# else
90141cc406Sopenharmony_ci#  define _(msgid)	(msgid)
91141cc406Sopenharmony_ci# endif
92141cc406Sopenharmony_ci# if defined _LIBC && defined USE_IN_LIBIO
93141cc406Sopenharmony_ci#  include <wchar.h>
94141cc406Sopenharmony_ci# endif
95141cc406Sopenharmony_ci#endif
96141cc406Sopenharmony_ci
97141cc406Sopenharmony_ci#ifndef attribute_hidden
98141cc406Sopenharmony_ci# define attribute_hidden
99141cc406Sopenharmony_ci#endif
100141cc406Sopenharmony_ci
101141cc406Sopenharmony_ci/* This version of `getopt' appears to the caller like standard Unix `getopt'
102141cc406Sopenharmony_ci   but it behaves differently for the user, since it allows the user
103141cc406Sopenharmony_ci   to intersperse the options with the other arguments.
104141cc406Sopenharmony_ci
105141cc406Sopenharmony_ci   As `getopt' works, it permutes the elements of ARGV so that,
106141cc406Sopenharmony_ci   when it is done, all the options precede everything else.  Thus
107141cc406Sopenharmony_ci   all application programs are extended to handle flexible argument order.
108141cc406Sopenharmony_ci
109141cc406Sopenharmony_ci   Setting the environment variable POSIXLY_CORRECT disables permutation.
110141cc406Sopenharmony_ci   Then the behavior is completely standard.
111141cc406Sopenharmony_ci
112141cc406Sopenharmony_ci   GNU application programs can use a third alternative mode in which
113141cc406Sopenharmony_ci   they can distinguish the relative order of options and other arguments.  */
114141cc406Sopenharmony_ci
115141cc406Sopenharmony_ci#include "../include/lgetopt.h"
116141cc406Sopenharmony_ci
117141cc406Sopenharmony_ci/* For communication from `getopt' to the caller.
118141cc406Sopenharmony_ci   When `getopt' finds an option that takes an argument,
119141cc406Sopenharmony_ci   the argument value is returned here.
120141cc406Sopenharmony_ci   Also, when `ordering' is RETURN_IN_ORDER,
121141cc406Sopenharmony_ci   each non-option ARGV-element is returned here.  */
122141cc406Sopenharmony_ci
123141cc406Sopenharmony_cichar *optarg = NULL;
124141cc406Sopenharmony_ci
125141cc406Sopenharmony_ci/* Index in ARGV of the next element to be scanned.
126141cc406Sopenharmony_ci   This is used for communication to and from the caller
127141cc406Sopenharmony_ci   and for communication between successive calls to `getopt'.
128141cc406Sopenharmony_ci
129141cc406Sopenharmony_ci   On entry to `getopt', zero means this is the first call; initialize.
130141cc406Sopenharmony_ci
131141cc406Sopenharmony_ci   When `getopt' returns -1, this is the index of the first of the
132141cc406Sopenharmony_ci   non-option elements that the caller should itself scan.
133141cc406Sopenharmony_ci
134141cc406Sopenharmony_ci   Otherwise, `optind' communicates from one call to the next
135141cc406Sopenharmony_ci   how much of ARGV has been scanned so far.  */
136141cc406Sopenharmony_ci
137141cc406Sopenharmony_ci/* 1003.2 says this must be 1 before any call.  */
138141cc406Sopenharmony_ciint optind = 1;
139141cc406Sopenharmony_ci
140141cc406Sopenharmony_ci/* Formerly, initialization of getopt depended on optind==0, which
141141cc406Sopenharmony_ci   causes problems with re-calling getopt as programs generally don't
142141cc406Sopenharmony_ci   know that. */
143141cc406Sopenharmony_ci
144141cc406Sopenharmony_ciint __getopt_initialized attribute_hidden;
145141cc406Sopenharmony_ci
146141cc406Sopenharmony_ci/* The next char to be scanned in the option-element
147141cc406Sopenharmony_ci   in which the last option character we returned was found.
148141cc406Sopenharmony_ci   This allows us to pick up the scan where we left off.
149141cc406Sopenharmony_ci
150141cc406Sopenharmony_ci   If this is zero, or a null string, it means resume the scan
151141cc406Sopenharmony_ci   by advancing to the next ARGV-element.  */
152141cc406Sopenharmony_ci
153141cc406Sopenharmony_cistatic char *nextchar;
154141cc406Sopenharmony_ci
155141cc406Sopenharmony_ci/* Callers store zero here to inhibit the error message
156141cc406Sopenharmony_ci   for unrecognized options.  */
157141cc406Sopenharmony_ci
158141cc406Sopenharmony_ciint opterr = 1;
159141cc406Sopenharmony_ci
160141cc406Sopenharmony_ci/* Set to an option character which was unrecognized.
161141cc406Sopenharmony_ci   This must be initialized on some systems to avoid linking in the
162141cc406Sopenharmony_ci   system's own getopt implementation.  */
163141cc406Sopenharmony_ci
164141cc406Sopenharmony_ciint optopt = '?';
165141cc406Sopenharmony_ci
166141cc406Sopenharmony_ci/* Describe how to deal with options that follow non-option ARGV-elements.
167141cc406Sopenharmony_ci
168141cc406Sopenharmony_ci   If the caller did not specify anything,
169141cc406Sopenharmony_ci   the default is REQUIRE_ORDER if the environment variable
170141cc406Sopenharmony_ci   POSIXLY_CORRECT is defined, PERMUTE otherwise.
171141cc406Sopenharmony_ci
172141cc406Sopenharmony_ci   REQUIRE_ORDER means don't recognize them as options;
173141cc406Sopenharmony_ci   stop option processing when the first non-option is seen.
174141cc406Sopenharmony_ci   This is what Unix does.
175141cc406Sopenharmony_ci   This mode of operation is selected by either setting the environment
176141cc406Sopenharmony_ci   variable POSIXLY_CORRECT, or using `+' as the first character
177141cc406Sopenharmony_ci   of the list of option characters.
178141cc406Sopenharmony_ci
179141cc406Sopenharmony_ci   PERMUTE is the default.  We permute the contents of ARGV as we scan,
180141cc406Sopenharmony_ci   so that eventually all the non-options are at the end.  This allows options
181141cc406Sopenharmony_ci   to be given in any order, even with programs that were not written to
182141cc406Sopenharmony_ci   expect this.
183141cc406Sopenharmony_ci
184141cc406Sopenharmony_ci   RETURN_IN_ORDER is an option available to programs that were written
185141cc406Sopenharmony_ci   to expect options and other ARGV-elements in any order and that care about
186141cc406Sopenharmony_ci   the ordering of the two.  We describe each non-option ARGV-element
187141cc406Sopenharmony_ci   as if it were the argument of an option with character code 1.
188141cc406Sopenharmony_ci   Using `-' as the first character of the list of option characters
189141cc406Sopenharmony_ci   selects this mode of operation.
190141cc406Sopenharmony_ci
191141cc406Sopenharmony_ci   The special argument `--' forces an end of option-scanning regardless
192141cc406Sopenharmony_ci   of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
193141cc406Sopenharmony_ci   `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
194141cc406Sopenharmony_ci
195141cc406Sopenharmony_cistatic enum
196141cc406Sopenharmony_ci{
197141cc406Sopenharmony_ci  REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
198141cc406Sopenharmony_ci} ordering;
199141cc406Sopenharmony_ci
200141cc406Sopenharmony_ci/* Value of POSIXLY_CORRECT environment variable.  */
201141cc406Sopenharmony_cistatic char *posixly_correct;
202141cc406Sopenharmony_ci
203141cc406Sopenharmony_ci#ifdef	__GNU_LIBRARY__
204141cc406Sopenharmony_ci/* We want to avoid inclusion of string.h with non-GNU libraries
205141cc406Sopenharmony_ci   because there are many ways it can cause trouble.
206141cc406Sopenharmony_ci   On some systems, it contains special magic macros that don't work
207141cc406Sopenharmony_ci   in GCC.  */
208141cc406Sopenharmony_ci# include <string.h>
209141cc406Sopenharmony_ci# define my_index	strchr
210141cc406Sopenharmony_ci#else
211141cc406Sopenharmony_ci
212141cc406Sopenharmony_ci# if HAVE_STRING_H
213141cc406Sopenharmony_ci#  include <string.h>
214141cc406Sopenharmony_ci# else
215141cc406Sopenharmony_ci#  include <strings.h>
216141cc406Sopenharmony_ci# endif
217141cc406Sopenharmony_ci
218141cc406Sopenharmony_ci/* Avoid depending on library functions or files
219141cc406Sopenharmony_ci   whose names are inconsistent.  */
220141cc406Sopenharmony_ci
221141cc406Sopenharmony_ci#ifndef getenv
222141cc406Sopenharmony_ciextern char *getenv ();
223141cc406Sopenharmony_ci#endif
224141cc406Sopenharmony_ci
225141cc406Sopenharmony_cistatic char *
226141cc406Sopenharmony_cimy_index (str, chr)
227141cc406Sopenharmony_ci     const char *str;
228141cc406Sopenharmony_ci     int chr;
229141cc406Sopenharmony_ci{
230141cc406Sopenharmony_ci  while (*str)
231141cc406Sopenharmony_ci    {
232141cc406Sopenharmony_ci      if (*str == chr)
233141cc406Sopenharmony_ci	return (char *) str;
234141cc406Sopenharmony_ci      str++;
235141cc406Sopenharmony_ci    }
236141cc406Sopenharmony_ci  return 0;
237141cc406Sopenharmony_ci}
238141cc406Sopenharmony_ci
239141cc406Sopenharmony_ci/* If using GCC, we can safely declare strlen this way.
240141cc406Sopenharmony_ci   If not using GCC, it is ok not to declare it.  */
241141cc406Sopenharmony_ci#ifdef __GNUC__
242141cc406Sopenharmony_ci/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
243141cc406Sopenharmony_ci   That was relevant to code that was here before.  */
244141cc406Sopenharmony_ci# if (!defined __STDC__ || !__STDC__) && !defined strlen
245141cc406Sopenharmony_ci/* gcc with -traditional declares the built-in strlen to return int,
246141cc406Sopenharmony_ci   and has done so at least since version 2.4.5. -- rms.  */
247141cc406Sopenharmony_ciextern int strlen (const char *);
248141cc406Sopenharmony_ci# endif /* not __STDC__ */
249141cc406Sopenharmony_ci#endif /* __GNUC__ */
250141cc406Sopenharmony_ci
251141cc406Sopenharmony_ci#endif /* not __GNU_LIBRARY__ */
252141cc406Sopenharmony_ci
253141cc406Sopenharmony_ci/* Handle permutation of arguments.  */
254141cc406Sopenharmony_ci
255141cc406Sopenharmony_ci/* Describe the part of ARGV that contains non-options that have
256141cc406Sopenharmony_ci   been skipped.  `first_nonopt' is the index in ARGV of the first of them;
257141cc406Sopenharmony_ci   `last_nonopt' is the index after the last of them.  */
258141cc406Sopenharmony_ci
259141cc406Sopenharmony_cistatic int first_nonopt;
260141cc406Sopenharmony_cistatic int last_nonopt;
261141cc406Sopenharmony_ci
262141cc406Sopenharmony_ci#ifdef _LIBC
263141cc406Sopenharmony_ci/* Stored original parameters.
264141cc406Sopenharmony_ci   XXX This is no good solution.  We should rather copy the args so
265141cc406Sopenharmony_ci   that we can compare them later.  But we must not use malloc(3).  */
266141cc406Sopenharmony_ciextern int __libc_argc;
267141cc406Sopenharmony_ciextern char **__libc_argv;
268141cc406Sopenharmony_ci
269141cc406Sopenharmony_ci/* Bash 2.0 gives us an environment variable containing flags
270141cc406Sopenharmony_ci   indicating ARGV elements that should not be considered arguments.  */
271141cc406Sopenharmony_ci
272141cc406Sopenharmony_ci# ifdef USE_NONOPTION_FLAGS
273141cc406Sopenharmony_ci/* Defined in getopt_init.c  */
274141cc406Sopenharmony_ciextern char *__getopt_nonoption_flags;
275141cc406Sopenharmony_ci
276141cc406Sopenharmony_cistatic int nonoption_flags_max_len;
277141cc406Sopenharmony_cistatic int nonoption_flags_len;
278141cc406Sopenharmony_ci# endif
279141cc406Sopenharmony_ci
280141cc406Sopenharmony_ci# ifdef USE_NONOPTION_FLAGS
281141cc406Sopenharmony_ci#  define SWAP_FLAGS(ch1, ch2) \
282141cc406Sopenharmony_ci  if (nonoption_flags_len > 0)						      \
283141cc406Sopenharmony_ci    {									      \
284141cc406Sopenharmony_ci      char __tmp = __getopt_nonoption_flags[ch1];			      \
285141cc406Sopenharmony_ci      __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];	      \
286141cc406Sopenharmony_ci      __getopt_nonoption_flags[ch2] = __tmp;				      \
287141cc406Sopenharmony_ci    }
288141cc406Sopenharmony_ci# else
289141cc406Sopenharmony_ci#  define SWAP_FLAGS(ch1, ch2)
290141cc406Sopenharmony_ci# endif
291141cc406Sopenharmony_ci#else	/* !_LIBC */
292141cc406Sopenharmony_ci# define SWAP_FLAGS(ch1, ch2)
293141cc406Sopenharmony_ci#endif	/* _LIBC */
294141cc406Sopenharmony_ci
295141cc406Sopenharmony_ci/* Exchange two adjacent subsequences of ARGV.
296141cc406Sopenharmony_ci   One subsequence is elements [first_nonopt,last_nonopt)
297141cc406Sopenharmony_ci   which contains all the non-options that have been skipped so far.
298141cc406Sopenharmony_ci   The other is elements [last_nonopt,optind), which contains all
299141cc406Sopenharmony_ci   the options processed since those non-options were skipped.
300141cc406Sopenharmony_ci
301141cc406Sopenharmony_ci   `first_nonopt' and `last_nonopt' are relocated so that they describe
302141cc406Sopenharmony_ci   the new indices of the non-options in ARGV after they are moved.  */
303141cc406Sopenharmony_ci
304141cc406Sopenharmony_ci#if defined __STDC__ && __STDC__
305141cc406Sopenharmony_cistatic void exchange (char **);
306141cc406Sopenharmony_ci#endif
307141cc406Sopenharmony_ci
308141cc406Sopenharmony_cistatic void
309141cc406Sopenharmony_ciexchange (argv)
310141cc406Sopenharmony_ci     char **argv;
311141cc406Sopenharmony_ci{
312141cc406Sopenharmony_ci  int bottom = first_nonopt;
313141cc406Sopenharmony_ci  int middle = last_nonopt;
314141cc406Sopenharmony_ci  int top = optind;
315141cc406Sopenharmony_ci  char *tem;
316141cc406Sopenharmony_ci
317141cc406Sopenharmony_ci  /* Exchange the shorter segment with the far end of the longer segment.
318141cc406Sopenharmony_ci     That puts the shorter segment into the right place.
319141cc406Sopenharmony_ci     It leaves the longer segment in the right place overall,
320141cc406Sopenharmony_ci     but it consists of two parts that need to be swapped next.  */
321141cc406Sopenharmony_ci
322141cc406Sopenharmony_ci#if defined _LIBC && defined USE_NONOPTION_FLAGS
323141cc406Sopenharmony_ci  /* First make sure the handling of the `__getopt_nonoption_flags'
324141cc406Sopenharmony_ci     string can work normally.  Our top argument must be in the range
325141cc406Sopenharmony_ci     of the string.  */
326141cc406Sopenharmony_ci  if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
327141cc406Sopenharmony_ci    {
328141cc406Sopenharmony_ci      /* We must extend the array.  The user plays games with us and
329141cc406Sopenharmony_ci	 presents new arguments.  */
330141cc406Sopenharmony_ci      char *new_str = malloc (top + 1);
331141cc406Sopenharmony_ci      if (new_str == NULL)
332141cc406Sopenharmony_ci	nonoption_flags_len = nonoption_flags_max_len = 0;
333141cc406Sopenharmony_ci      else
334141cc406Sopenharmony_ci	{
335141cc406Sopenharmony_ci	  memset (__mempcpy (new_str, __getopt_nonoption_flags,
336141cc406Sopenharmony_ci			     nonoption_flags_max_len),
337141cc406Sopenharmony_ci		  '\0', top + 1 - nonoption_flags_max_len);
338141cc406Sopenharmony_ci	  nonoption_flags_max_len = top + 1;
339141cc406Sopenharmony_ci	  __getopt_nonoption_flags = new_str;
340141cc406Sopenharmony_ci	}
341141cc406Sopenharmony_ci    }
342141cc406Sopenharmony_ci#endif
343141cc406Sopenharmony_ci
344141cc406Sopenharmony_ci  while (top > middle && middle > bottom)
345141cc406Sopenharmony_ci    {
346141cc406Sopenharmony_ci      if (top - middle > middle - bottom)
347141cc406Sopenharmony_ci	{
348141cc406Sopenharmony_ci	  /* Bottom segment is the short one.  */
349141cc406Sopenharmony_ci	  int len = middle - bottom;
350141cc406Sopenharmony_ci	  register int i;
351141cc406Sopenharmony_ci
352141cc406Sopenharmony_ci	  /* Swap it with the top part of the top segment.  */
353141cc406Sopenharmony_ci	  for (i = 0; i < len; i++)
354141cc406Sopenharmony_ci	    {
355141cc406Sopenharmony_ci	      tem = argv[bottom + i];
356141cc406Sopenharmony_ci	      argv[bottom + i] = argv[top - (middle - bottom) + i];
357141cc406Sopenharmony_ci	      argv[top - (middle - bottom) + i] = tem;
358141cc406Sopenharmony_ci	      SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
359141cc406Sopenharmony_ci	    }
360141cc406Sopenharmony_ci	  /* Exclude the moved bottom segment from further swapping.  */
361141cc406Sopenharmony_ci	  top -= len;
362141cc406Sopenharmony_ci	}
363141cc406Sopenharmony_ci      else
364141cc406Sopenharmony_ci	{
365141cc406Sopenharmony_ci	  /* Top segment is the short one.  */
366141cc406Sopenharmony_ci	  int len = top - middle;
367141cc406Sopenharmony_ci	  register int i;
368141cc406Sopenharmony_ci
369141cc406Sopenharmony_ci	  /* Swap it with the bottom part of the bottom segment.  */
370141cc406Sopenharmony_ci	  for (i = 0; i < len; i++)
371141cc406Sopenharmony_ci	    {
372141cc406Sopenharmony_ci	      tem = argv[bottom + i];
373141cc406Sopenharmony_ci	      argv[bottom + i] = argv[middle + i];
374141cc406Sopenharmony_ci	      argv[middle + i] = tem;
375141cc406Sopenharmony_ci	      SWAP_FLAGS (bottom + i, middle + i);
376141cc406Sopenharmony_ci	    }
377141cc406Sopenharmony_ci	  /* Exclude the moved top segment from further swapping.  */
378141cc406Sopenharmony_ci	  bottom += len;
379141cc406Sopenharmony_ci	}
380141cc406Sopenharmony_ci    }
381141cc406Sopenharmony_ci
382141cc406Sopenharmony_ci  /* Update records for the slots the non-options now occupy.  */
383141cc406Sopenharmony_ci
384141cc406Sopenharmony_ci  first_nonopt += (optind - last_nonopt);
385141cc406Sopenharmony_ci  last_nonopt = optind;
386141cc406Sopenharmony_ci}
387141cc406Sopenharmony_ci
388141cc406Sopenharmony_ci/* Initialize the internal data when the first call is made.  */
389141cc406Sopenharmony_ci
390141cc406Sopenharmony_ci#if defined __STDC__ && __STDC__
391141cc406Sopenharmony_cistatic const char *_getopt_initialize (int, char *const *, const char *);
392141cc406Sopenharmony_ci#endif
393141cc406Sopenharmony_cistatic const char *
394141cc406Sopenharmony_ci_getopt_initialize (argc, argv, optstring)
395141cc406Sopenharmony_ci     int argc;
396141cc406Sopenharmony_ci     char *const *argv;
397141cc406Sopenharmony_ci     const char *optstring;
398141cc406Sopenharmony_ci{
399141cc406Sopenharmony_ci  /* Start processing options with ARGV-element 1 (since ARGV-element 0
400141cc406Sopenharmony_ci     is the program name); the sequence of previously skipped
401141cc406Sopenharmony_ci     non-option ARGV-elements is empty.  */
402141cc406Sopenharmony_ci
403141cc406Sopenharmony_ci  first_nonopt = last_nonopt = optind;
404141cc406Sopenharmony_ci
405141cc406Sopenharmony_ci  nextchar = NULL;
406141cc406Sopenharmony_ci
407141cc406Sopenharmony_ci  posixly_correct = getenv ("POSIXLY_CORRECT");
408141cc406Sopenharmony_ci
409141cc406Sopenharmony_ci  /* Determine how to handle the ordering of options and nonoptions.  */
410141cc406Sopenharmony_ci
411141cc406Sopenharmony_ci  if (optstring[0] == '-')
412141cc406Sopenharmony_ci    {
413141cc406Sopenharmony_ci      ordering = RETURN_IN_ORDER;
414141cc406Sopenharmony_ci      ++optstring;
415141cc406Sopenharmony_ci    }
416141cc406Sopenharmony_ci  else if (optstring[0] == '+')
417141cc406Sopenharmony_ci    {
418141cc406Sopenharmony_ci      ordering = REQUIRE_ORDER;
419141cc406Sopenharmony_ci      ++optstring;
420141cc406Sopenharmony_ci    }
421141cc406Sopenharmony_ci  else if (posixly_correct != NULL)
422141cc406Sopenharmony_ci    ordering = REQUIRE_ORDER;
423141cc406Sopenharmony_ci  else
424141cc406Sopenharmony_ci    ordering = PERMUTE;
425141cc406Sopenharmony_ci
426141cc406Sopenharmony_ci#if defined _LIBC && defined USE_NONOPTION_FLAGS
427141cc406Sopenharmony_ci  if (posixly_correct == NULL
428141cc406Sopenharmony_ci      && argc == __libc_argc && argv == __libc_argv)
429141cc406Sopenharmony_ci    {
430141cc406Sopenharmony_ci      if (nonoption_flags_max_len == 0)
431141cc406Sopenharmony_ci	{
432141cc406Sopenharmony_ci	  if (__getopt_nonoption_flags == NULL
433141cc406Sopenharmony_ci	      || __getopt_nonoption_flags[0] == '\0')
434141cc406Sopenharmony_ci	    nonoption_flags_max_len = -1;
435141cc406Sopenharmony_ci	  else
436141cc406Sopenharmony_ci	    {
437141cc406Sopenharmony_ci	      const char *orig_str = __getopt_nonoption_flags;
438141cc406Sopenharmony_ci	      int len = nonoption_flags_max_len = strlen (orig_str);
439141cc406Sopenharmony_ci	      if (nonoption_flags_max_len < argc)
440141cc406Sopenharmony_ci		nonoption_flags_max_len = argc;
441141cc406Sopenharmony_ci	      __getopt_nonoption_flags =
442141cc406Sopenharmony_ci		(char *) malloc (nonoption_flags_max_len);
443141cc406Sopenharmony_ci	      if (__getopt_nonoption_flags == NULL)
444141cc406Sopenharmony_ci		nonoption_flags_max_len = -1;
445141cc406Sopenharmony_ci	      else
446141cc406Sopenharmony_ci		memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
447141cc406Sopenharmony_ci			'\0', nonoption_flags_max_len - len);
448141cc406Sopenharmony_ci	    }
449141cc406Sopenharmony_ci	}
450141cc406Sopenharmony_ci      nonoption_flags_len = nonoption_flags_max_len;
451141cc406Sopenharmony_ci    }
452141cc406Sopenharmony_ci  else
453141cc406Sopenharmony_ci    nonoption_flags_len = 0;
454141cc406Sopenharmony_ci#endif
455141cc406Sopenharmony_ci
456141cc406Sopenharmony_ci  return optstring;
457141cc406Sopenharmony_ci}
458141cc406Sopenharmony_ci
459141cc406Sopenharmony_ci/* Scan elements of ARGV (whose length is ARGC) for option characters
460141cc406Sopenharmony_ci   given in OPTSTRING.
461141cc406Sopenharmony_ci
462141cc406Sopenharmony_ci   If an element of ARGV starts with '-', and is not exactly "-" or "--",
463141cc406Sopenharmony_ci   then it is an option element.  The characters of this element
464141cc406Sopenharmony_ci   (aside from the initial '-') are option characters.  If `getopt'
465141cc406Sopenharmony_ci   is called repeatedly, it returns successively each of the option characters
466141cc406Sopenharmony_ci   from each of the option elements.
467141cc406Sopenharmony_ci
468141cc406Sopenharmony_ci   If `getopt' finds another option character, it returns that character,
469141cc406Sopenharmony_ci   updating `optind' and `nextchar' so that the next call to `getopt' can
470141cc406Sopenharmony_ci   resume the scan with the following option character or ARGV-element.
471141cc406Sopenharmony_ci
472141cc406Sopenharmony_ci   If there are no more option characters, `getopt' returns -1.
473141cc406Sopenharmony_ci   Then `optind' is the index in ARGV of the first ARGV-element
474141cc406Sopenharmony_ci   that is not an option.  (The ARGV-elements have been permuted
475141cc406Sopenharmony_ci   so that those that are not options now come last.)
476141cc406Sopenharmony_ci
477141cc406Sopenharmony_ci   OPTSTRING is a string containing the legitimate option characters.
478141cc406Sopenharmony_ci   If an option character is seen that is not listed in OPTSTRING,
479141cc406Sopenharmony_ci   return '?' after printing an error message.  If you set `opterr' to
480141cc406Sopenharmony_ci   zero, the error message is suppressed but we still return '?'.
481141cc406Sopenharmony_ci
482141cc406Sopenharmony_ci   If a char in OPTSTRING is followed by a colon, that means it wants an arg,
483141cc406Sopenharmony_ci   so the following text in the same ARGV-element, or the text of the following
484141cc406Sopenharmony_ci   ARGV-element, is returned in `optarg'.  Two colons mean an option that
485141cc406Sopenharmony_ci   wants an optional arg; if there is text in the current ARGV-element,
486141cc406Sopenharmony_ci   it is returned in `optarg', otherwise `optarg' is set to zero.
487141cc406Sopenharmony_ci
488141cc406Sopenharmony_ci   If OPTSTRING starts with `-' or `+', it requests different methods of
489141cc406Sopenharmony_ci   handling the non-option ARGV-elements.
490141cc406Sopenharmony_ci   See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
491141cc406Sopenharmony_ci
492141cc406Sopenharmony_ci   Long-named options begin with `--' instead of `-'.
493141cc406Sopenharmony_ci   Their names may be abbreviated as long as the abbreviation is unique
494141cc406Sopenharmony_ci   or is an exact match for some defined option.  If they have an
495141cc406Sopenharmony_ci   argument, it follows the option name in the same ARGV-element, separated
496141cc406Sopenharmony_ci   from the option name by a `=', or else the in next ARGV-element.
497141cc406Sopenharmony_ci   When `getopt' finds a long-named option, it returns 0 if that option's
498141cc406Sopenharmony_ci   `flag' field is nonzero, the value of the option's `val' field
499141cc406Sopenharmony_ci   if the `flag' field is zero.
500141cc406Sopenharmony_ci
501141cc406Sopenharmony_ci   The elements of ARGV aren't really const, because we permute them.
502141cc406Sopenharmony_ci   But we pretend they're const in the prototype to be compatible
503141cc406Sopenharmony_ci   with other systems.
504141cc406Sopenharmony_ci
505141cc406Sopenharmony_ci   LONGOPTS is a vector of `struct option' terminated by an
506141cc406Sopenharmony_ci   element containing a name which is zero.
507141cc406Sopenharmony_ci
508141cc406Sopenharmony_ci   LONGIND returns the index in LONGOPT of the long-named option found.
509141cc406Sopenharmony_ci   It is only valid when a long-named option has been found by the most
510141cc406Sopenharmony_ci   recent call.
511141cc406Sopenharmony_ci
512141cc406Sopenharmony_ci   If LONG_ONLY is nonzero, '-' as well as '--' can introduce
513141cc406Sopenharmony_ci   long-named options.  */
514141cc406Sopenharmony_ci
515141cc406Sopenharmony_ciint
516141cc406Sopenharmony_ci_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
517141cc406Sopenharmony_ci     int argc;
518141cc406Sopenharmony_ci     char *const *argv;
519141cc406Sopenharmony_ci     const char *optstring;
520141cc406Sopenharmony_ci     const struct option *longopts;
521141cc406Sopenharmony_ci     int *longind;
522141cc406Sopenharmony_ci     int long_only;
523141cc406Sopenharmony_ci{
524141cc406Sopenharmony_ci  int print_errors = opterr;
525141cc406Sopenharmony_ci  if (optstring[0] == ':')
526141cc406Sopenharmony_ci    print_errors = 0;
527141cc406Sopenharmony_ci
528141cc406Sopenharmony_ci  if (argc < 1)
529141cc406Sopenharmony_ci    return -1;
530141cc406Sopenharmony_ci
531141cc406Sopenharmony_ci  optarg = NULL;
532141cc406Sopenharmony_ci
533141cc406Sopenharmony_ci  if (optind == 0 || !__getopt_initialized)
534141cc406Sopenharmony_ci    {
535141cc406Sopenharmony_ci      if (optind == 0)
536141cc406Sopenharmony_ci	optind = 1;	/* Don't scan ARGV[0], the program name.  */
537141cc406Sopenharmony_ci      optstring = _getopt_initialize (argc, argv, optstring);
538141cc406Sopenharmony_ci      __getopt_initialized = 1;
539141cc406Sopenharmony_ci    }
540141cc406Sopenharmony_ci
541141cc406Sopenharmony_ci  /* Test whether ARGV[optind] points to a non-option argument.
542141cc406Sopenharmony_ci     Either it does not have option syntax, or there is an environment flag
543141cc406Sopenharmony_ci     from the shell indicating it is not an option.  The later information
544141cc406Sopenharmony_ci     is only used when the used in the GNU libc.  */
545141cc406Sopenharmony_ci#if defined _LIBC && defined USE_NONOPTION_FLAGS
546141cc406Sopenharmony_ci# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'	      \
547141cc406Sopenharmony_ci		      || (optind < nonoption_flags_len			      \
548141cc406Sopenharmony_ci			  && __getopt_nonoption_flags[optind] == '1'))
549141cc406Sopenharmony_ci#else
550141cc406Sopenharmony_ci# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
551141cc406Sopenharmony_ci#endif
552141cc406Sopenharmony_ci
553141cc406Sopenharmony_ci  if (nextchar == NULL || *nextchar == '\0')
554141cc406Sopenharmony_ci    {
555141cc406Sopenharmony_ci      /* Advance to the next ARGV-element.  */
556141cc406Sopenharmony_ci
557141cc406Sopenharmony_ci      /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
558141cc406Sopenharmony_ci	 moved back by the user (who may also have changed the arguments).  */
559141cc406Sopenharmony_ci      if (last_nonopt > optind)
560141cc406Sopenharmony_ci	last_nonopt = optind;
561141cc406Sopenharmony_ci      if (first_nonopt > optind)
562141cc406Sopenharmony_ci	first_nonopt = optind;
563141cc406Sopenharmony_ci
564141cc406Sopenharmony_ci      if (ordering == PERMUTE)
565141cc406Sopenharmony_ci	{
566141cc406Sopenharmony_ci	  /* If we have just processed some options following some non-options,
567141cc406Sopenharmony_ci	     exchange them so that the options come first.  */
568141cc406Sopenharmony_ci
569141cc406Sopenharmony_ci	  if (first_nonopt != last_nonopt && last_nonopt != optind)
570141cc406Sopenharmony_ci	    exchange ((char **) argv);
571141cc406Sopenharmony_ci	  else if (last_nonopt != optind)
572141cc406Sopenharmony_ci	    first_nonopt = optind;
573141cc406Sopenharmony_ci
574141cc406Sopenharmony_ci	  /* Skip any additional non-options
575141cc406Sopenharmony_ci	     and extend the range of non-options previously skipped.  */
576141cc406Sopenharmony_ci
577141cc406Sopenharmony_ci	  while (optind < argc && NONOPTION_P)
578141cc406Sopenharmony_ci	    optind++;
579141cc406Sopenharmony_ci	  last_nonopt = optind;
580141cc406Sopenharmony_ci	}
581141cc406Sopenharmony_ci
582141cc406Sopenharmony_ci      /* The special ARGV-element `--' means premature end of options.
583141cc406Sopenharmony_ci	 Skip it like a null option,
584141cc406Sopenharmony_ci	 then exchange with previous non-options as if it were an option,
585141cc406Sopenharmony_ci	 then skip everything else like a non-option.  */
586141cc406Sopenharmony_ci
587141cc406Sopenharmony_ci      if (optind != argc && !strcmp (argv[optind], "--"))
588141cc406Sopenharmony_ci	{
589141cc406Sopenharmony_ci	  optind++;
590141cc406Sopenharmony_ci
591141cc406Sopenharmony_ci	  if (first_nonopt != last_nonopt && last_nonopt != optind)
592141cc406Sopenharmony_ci	    exchange ((char **) argv);
593141cc406Sopenharmony_ci	  else if (first_nonopt == last_nonopt)
594141cc406Sopenharmony_ci	    first_nonopt = optind;
595141cc406Sopenharmony_ci	  last_nonopt = argc;
596141cc406Sopenharmony_ci
597141cc406Sopenharmony_ci	  optind = argc;
598141cc406Sopenharmony_ci	}
599141cc406Sopenharmony_ci
600141cc406Sopenharmony_ci      /* If we have done all the ARGV-elements, stop the scan
601141cc406Sopenharmony_ci	 and back over any non-options that we skipped and permuted.  */
602141cc406Sopenharmony_ci
603141cc406Sopenharmony_ci      if (optind == argc)
604141cc406Sopenharmony_ci	{
605141cc406Sopenharmony_ci	  /* Set the next-arg-index to point at the non-options
606141cc406Sopenharmony_ci	     that we previously skipped, so the caller will digest them.  */
607141cc406Sopenharmony_ci	  if (first_nonopt != last_nonopt)
608141cc406Sopenharmony_ci	    optind = first_nonopt;
609141cc406Sopenharmony_ci	  return -1;
610141cc406Sopenharmony_ci	}
611141cc406Sopenharmony_ci
612141cc406Sopenharmony_ci      /* If we have come to a non-option and did not permute it,
613141cc406Sopenharmony_ci	 either stop the scan or describe it to the caller and pass it by.  */
614141cc406Sopenharmony_ci
615141cc406Sopenharmony_ci      if (NONOPTION_P)
616141cc406Sopenharmony_ci	{
617141cc406Sopenharmony_ci	  if (ordering == REQUIRE_ORDER)
618141cc406Sopenharmony_ci	    return -1;
619141cc406Sopenharmony_ci	  optarg = argv[optind++];
620141cc406Sopenharmony_ci	  return 1;
621141cc406Sopenharmony_ci	}
622141cc406Sopenharmony_ci
623141cc406Sopenharmony_ci      /* We have found another option-ARGV-element.
624141cc406Sopenharmony_ci	 Skip the initial punctuation.  */
625141cc406Sopenharmony_ci
626141cc406Sopenharmony_ci      nextchar = (argv[optind] + 1
627141cc406Sopenharmony_ci		  + (longopts != NULL && argv[optind][1] == '-'));
628141cc406Sopenharmony_ci    }
629141cc406Sopenharmony_ci
630141cc406Sopenharmony_ci  /* Decode the current option-ARGV-element.  */
631141cc406Sopenharmony_ci
632141cc406Sopenharmony_ci  /* Check whether the ARGV-element is a long option.
633141cc406Sopenharmony_ci
634141cc406Sopenharmony_ci     If long_only and the ARGV-element has the form "-f", where f is
635141cc406Sopenharmony_ci     a valid short option, don't consider it an abbreviated form of
636141cc406Sopenharmony_ci     a long option that starts with f.  Otherwise there would be no
637141cc406Sopenharmony_ci     way to give the -f short option.
638141cc406Sopenharmony_ci
639141cc406Sopenharmony_ci     On the other hand, if there's a long option "fubar" and
640141cc406Sopenharmony_ci     the ARGV-element is "-fu", do consider that an abbreviation of
641141cc406Sopenharmony_ci     the long option, just like "--fu", and not "-f" with arg "u".
642141cc406Sopenharmony_ci
643141cc406Sopenharmony_ci     This distinction seems to be the most useful approach.  */
644141cc406Sopenharmony_ci
645141cc406Sopenharmony_ci  if (longopts != NULL
646141cc406Sopenharmony_ci      && (argv[optind][1] == '-'
647141cc406Sopenharmony_ci	  || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
648141cc406Sopenharmony_ci    {
649141cc406Sopenharmony_ci      char *nameend;
650141cc406Sopenharmony_ci      const struct option *p;
651141cc406Sopenharmony_ci      const struct option *pfound = NULL;
652141cc406Sopenharmony_ci      int exact = 0;
653141cc406Sopenharmony_ci      int ambig = 0;
654141cc406Sopenharmony_ci      int indfound = -1;
655141cc406Sopenharmony_ci      int option_index;
656141cc406Sopenharmony_ci
657141cc406Sopenharmony_ci      for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
658141cc406Sopenharmony_ci	/* Do nothing.  */ ;
659141cc406Sopenharmony_ci
660141cc406Sopenharmony_ci      /* Test all long options for either exact match
661141cc406Sopenharmony_ci	 or abbreviated matches.  */
662141cc406Sopenharmony_ci      for (p = longopts, option_index = 0; p->name; p++, option_index++)
663141cc406Sopenharmony_ci	if (!strncmp (p->name, nextchar, nameend - nextchar))
664141cc406Sopenharmony_ci	  {
665141cc406Sopenharmony_ci	    if ((unsigned int) (nameend - nextchar)
666141cc406Sopenharmony_ci		== (unsigned int) strlen (p->name))
667141cc406Sopenharmony_ci	      {
668141cc406Sopenharmony_ci		/* Exact match found.  */
669141cc406Sopenharmony_ci		pfound = p;
670141cc406Sopenharmony_ci		indfound = option_index;
671141cc406Sopenharmony_ci		exact = 1;
672141cc406Sopenharmony_ci		break;
673141cc406Sopenharmony_ci	      }
674141cc406Sopenharmony_ci	    else if (pfound == NULL)
675141cc406Sopenharmony_ci	      {
676141cc406Sopenharmony_ci		/* First nonexact match found.  */
677141cc406Sopenharmony_ci		pfound = p;
678141cc406Sopenharmony_ci		indfound = option_index;
679141cc406Sopenharmony_ci	      }
680141cc406Sopenharmony_ci	    else if (long_only
681141cc406Sopenharmony_ci		     || pfound->has_arg != p->has_arg
682141cc406Sopenharmony_ci		     || pfound->flag != p->flag
683141cc406Sopenharmony_ci		     || pfound->val != p->val)
684141cc406Sopenharmony_ci	      /* Second or later nonexact match found.  */
685141cc406Sopenharmony_ci	      ambig = 1;
686141cc406Sopenharmony_ci	  }
687141cc406Sopenharmony_ci
688141cc406Sopenharmony_ci      if (ambig && !exact)
689141cc406Sopenharmony_ci	{
690141cc406Sopenharmony_ci	  if (print_errors)
691141cc406Sopenharmony_ci	    {
692141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
693141cc406Sopenharmony_ci	      char *buf;
694141cc406Sopenharmony_ci
695141cc406Sopenharmony_ci	      if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
696141cc406Sopenharmony_ci			      argv[0], argv[optind]) >= 0)
697141cc406Sopenharmony_ci		{
698141cc406Sopenharmony_ci
699141cc406Sopenharmony_ci		  if (_IO_fwide (stderr, 0) > 0)
700141cc406Sopenharmony_ci		    __fwprintf (stderr, L"%s", buf);
701141cc406Sopenharmony_ci		  else
702141cc406Sopenharmony_ci		    fputs (buf, stderr);
703141cc406Sopenharmony_ci
704141cc406Sopenharmony_ci		  free (buf);
705141cc406Sopenharmony_ci		}
706141cc406Sopenharmony_ci#else
707141cc406Sopenharmony_ci	      fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
708141cc406Sopenharmony_ci		       argv[0], argv[optind]);
709141cc406Sopenharmony_ci#endif
710141cc406Sopenharmony_ci	    }
711141cc406Sopenharmony_ci	  nextchar += strlen (nextchar);
712141cc406Sopenharmony_ci	  optind++;
713141cc406Sopenharmony_ci	  optopt = 0;
714141cc406Sopenharmony_ci	  return '?';
715141cc406Sopenharmony_ci	}
716141cc406Sopenharmony_ci
717141cc406Sopenharmony_ci      if (pfound != NULL)
718141cc406Sopenharmony_ci	{
719141cc406Sopenharmony_ci	  option_index = indfound;
720141cc406Sopenharmony_ci	  optind++;
721141cc406Sopenharmony_ci	  if (*nameend)
722141cc406Sopenharmony_ci	    {
723141cc406Sopenharmony_ci	      /* Don't test has_arg with >, because some C compilers don't
724141cc406Sopenharmony_ci		 allow it to be used on enums.  */
725141cc406Sopenharmony_ci	      if (pfound->has_arg)
726141cc406Sopenharmony_ci		optarg = nameend + 1;
727141cc406Sopenharmony_ci	      else
728141cc406Sopenharmony_ci		{
729141cc406Sopenharmony_ci		  if (print_errors)
730141cc406Sopenharmony_ci		    {
731141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
732141cc406Sopenharmony_ci		      char *buf;
733141cc406Sopenharmony_ci		      int n;
734141cc406Sopenharmony_ci#endif
735141cc406Sopenharmony_ci
736141cc406Sopenharmony_ci		      if (argv[optind - 1][1] == '-')
737141cc406Sopenharmony_ci			{
738141cc406Sopenharmony_ci			  /* --option */
739141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
740141cc406Sopenharmony_ci			  n = __asprintf (&buf, _("\
741141cc406Sopenharmony_ci%s: option `--%s' doesn't allow an argument\n"),
742141cc406Sopenharmony_ci					  argv[0], pfound->name);
743141cc406Sopenharmony_ci#else
744141cc406Sopenharmony_ci			  fprintf (stderr, _("\
745141cc406Sopenharmony_ci%s: option `--%s' doesn't allow an argument\n"),
746141cc406Sopenharmony_ci				   argv[0], pfound->name);
747141cc406Sopenharmony_ci#endif
748141cc406Sopenharmony_ci			}
749141cc406Sopenharmony_ci		      else
750141cc406Sopenharmony_ci			{
751141cc406Sopenharmony_ci			  /* +option or -option */
752141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
753141cc406Sopenharmony_ci			  n = __asprintf (&buf, _("\
754141cc406Sopenharmony_ci%s: option `%c%s' doesn't allow an argument\n"),
755141cc406Sopenharmony_ci					  argv[0], argv[optind - 1][0],
756141cc406Sopenharmony_ci					  pfound->name);
757141cc406Sopenharmony_ci#else
758141cc406Sopenharmony_ci			  fprintf (stderr, _("\
759141cc406Sopenharmony_ci%s: option `%c%s' doesn't allow an argument\n"),
760141cc406Sopenharmony_ci				   argv[0], argv[optind - 1][0], pfound->name);
761141cc406Sopenharmony_ci#endif
762141cc406Sopenharmony_ci			}
763141cc406Sopenharmony_ci
764141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
765141cc406Sopenharmony_ci		      if (n >= 0)
766141cc406Sopenharmony_ci			{
767141cc406Sopenharmony_ci			  if (_IO_fwide (stderr, 0) > 0)
768141cc406Sopenharmony_ci			    __fwprintf (stderr, L"%s", buf);
769141cc406Sopenharmony_ci			  else
770141cc406Sopenharmony_ci			    fputs (buf, stderr);
771141cc406Sopenharmony_ci
772141cc406Sopenharmony_ci			  free (buf);
773141cc406Sopenharmony_ci			}
774141cc406Sopenharmony_ci#endif
775141cc406Sopenharmony_ci		    }
776141cc406Sopenharmony_ci
777141cc406Sopenharmony_ci		  nextchar += strlen (nextchar);
778141cc406Sopenharmony_ci
779141cc406Sopenharmony_ci		  optopt = pfound->val;
780141cc406Sopenharmony_ci		  return '?';
781141cc406Sopenharmony_ci		}
782141cc406Sopenharmony_ci	    }
783141cc406Sopenharmony_ci	  else if (pfound->has_arg == 1)
784141cc406Sopenharmony_ci	    {
785141cc406Sopenharmony_ci	      if (optind < argc)
786141cc406Sopenharmony_ci		optarg = argv[optind++];
787141cc406Sopenharmony_ci	      else
788141cc406Sopenharmony_ci		{
789141cc406Sopenharmony_ci		  if (print_errors)
790141cc406Sopenharmony_ci		    {
791141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
792141cc406Sopenharmony_ci		      char *buf;
793141cc406Sopenharmony_ci
794141cc406Sopenharmony_ci		      if (__asprintf (&buf, _("\
795141cc406Sopenharmony_ci%s: option `%s' requires an argument\n"),
796141cc406Sopenharmony_ci				      argv[0], argv[optind - 1]) >= 0)
797141cc406Sopenharmony_ci			{
798141cc406Sopenharmony_ci			  if (_IO_fwide (stderr, 0) > 0)
799141cc406Sopenharmony_ci			    __fwprintf (stderr, L"%s", buf);
800141cc406Sopenharmony_ci			  else
801141cc406Sopenharmony_ci			    fputs (buf, stderr);
802141cc406Sopenharmony_ci
803141cc406Sopenharmony_ci			  free (buf);
804141cc406Sopenharmony_ci			}
805141cc406Sopenharmony_ci#else
806141cc406Sopenharmony_ci		      fprintf (stderr,
807141cc406Sopenharmony_ci			       _("%s: option `%s' requires an argument\n"),
808141cc406Sopenharmony_ci			       argv[0], argv[optind - 1]);
809141cc406Sopenharmony_ci#endif
810141cc406Sopenharmony_ci		    }
811141cc406Sopenharmony_ci		  nextchar += strlen (nextchar);
812141cc406Sopenharmony_ci		  optopt = pfound->val;
813141cc406Sopenharmony_ci		  return optstring[0] == ':' ? ':' : '?';
814141cc406Sopenharmony_ci		}
815141cc406Sopenharmony_ci	    }
816141cc406Sopenharmony_ci	  nextchar += strlen (nextchar);
817141cc406Sopenharmony_ci	  if (longind != NULL)
818141cc406Sopenharmony_ci	    *longind = option_index;
819141cc406Sopenharmony_ci	  if (pfound->flag)
820141cc406Sopenharmony_ci	    {
821141cc406Sopenharmony_ci	      *(pfound->flag) = pfound->val;
822141cc406Sopenharmony_ci	      return 0;
823141cc406Sopenharmony_ci	    }
824141cc406Sopenharmony_ci	  return pfound->val;
825141cc406Sopenharmony_ci	}
826141cc406Sopenharmony_ci
827141cc406Sopenharmony_ci      /* Can't find it as a long option.  If this is not getopt_long_only,
828141cc406Sopenharmony_ci	 or the option starts with '--' or is not a valid short
829141cc406Sopenharmony_ci	 option, then it's an error.
830141cc406Sopenharmony_ci	 Otherwise interpret it as a short option.  */
831141cc406Sopenharmony_ci      if (!long_only || argv[optind][1] == '-'
832141cc406Sopenharmony_ci	  || my_index (optstring, *nextchar) == NULL)
833141cc406Sopenharmony_ci	{
834141cc406Sopenharmony_ci	  if (print_errors)
835141cc406Sopenharmony_ci	    {
836141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
837141cc406Sopenharmony_ci	      char *buf;
838141cc406Sopenharmony_ci	      int n;
839141cc406Sopenharmony_ci#endif
840141cc406Sopenharmony_ci
841141cc406Sopenharmony_ci	      if (argv[optind][1] == '-')
842141cc406Sopenharmony_ci		{
843141cc406Sopenharmony_ci		  /* --option */
844141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
845141cc406Sopenharmony_ci		  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
846141cc406Sopenharmony_ci				  argv[0], nextchar);
847141cc406Sopenharmony_ci#else
848141cc406Sopenharmony_ci		  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
849141cc406Sopenharmony_ci			   argv[0], nextchar);
850141cc406Sopenharmony_ci#endif
851141cc406Sopenharmony_ci		}
852141cc406Sopenharmony_ci	      else
853141cc406Sopenharmony_ci		{
854141cc406Sopenharmony_ci		  /* +option or -option */
855141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
856141cc406Sopenharmony_ci		  n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
857141cc406Sopenharmony_ci				  argv[0], argv[optind][0], nextchar);
858141cc406Sopenharmony_ci#else
859141cc406Sopenharmony_ci		  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
860141cc406Sopenharmony_ci			   argv[0], argv[optind][0], nextchar);
861141cc406Sopenharmony_ci#endif
862141cc406Sopenharmony_ci		}
863141cc406Sopenharmony_ci
864141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
865141cc406Sopenharmony_ci	      if (n >= 0)
866141cc406Sopenharmony_ci		{
867141cc406Sopenharmony_ci		  if (_IO_fwide (stderr, 0) > 0)
868141cc406Sopenharmony_ci		    __fwprintf (stderr, L"%s", buf);
869141cc406Sopenharmony_ci		  else
870141cc406Sopenharmony_ci		    fputs (buf, stderr);
871141cc406Sopenharmony_ci
872141cc406Sopenharmony_ci		  free (buf);
873141cc406Sopenharmony_ci		}
874141cc406Sopenharmony_ci#endif
875141cc406Sopenharmony_ci	    }
876141cc406Sopenharmony_ci	  nextchar = (char *) "";
877141cc406Sopenharmony_ci	  optind++;
878141cc406Sopenharmony_ci	  optopt = 0;
879141cc406Sopenharmony_ci	  return '?';
880141cc406Sopenharmony_ci	}
881141cc406Sopenharmony_ci    }
882141cc406Sopenharmony_ci
883141cc406Sopenharmony_ci  /* Look at and handle the next short option-character.  */
884141cc406Sopenharmony_ci
885141cc406Sopenharmony_ci  {
886141cc406Sopenharmony_ci    char c = *nextchar++;
887141cc406Sopenharmony_ci    char *temp = my_index (optstring, c);
888141cc406Sopenharmony_ci
889141cc406Sopenharmony_ci    /* Increment `optind' when we start to process its last character.  */
890141cc406Sopenharmony_ci    if (*nextchar == '\0')
891141cc406Sopenharmony_ci      ++optind;
892141cc406Sopenharmony_ci
893141cc406Sopenharmony_ci    if (temp == NULL || c == ':')
894141cc406Sopenharmony_ci      {
895141cc406Sopenharmony_ci	if (print_errors)
896141cc406Sopenharmony_ci	  {
897141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
898141cc406Sopenharmony_ci	      char *buf;
899141cc406Sopenharmony_ci	      int n;
900141cc406Sopenharmony_ci#endif
901141cc406Sopenharmony_ci
902141cc406Sopenharmony_ci	    if (posixly_correct)
903141cc406Sopenharmony_ci	      {
904141cc406Sopenharmony_ci		/* 1003.2 specifies the format of this message.  */
905141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
906141cc406Sopenharmony_ci		n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
907141cc406Sopenharmony_ci				argv[0], c);
908141cc406Sopenharmony_ci#else
909141cc406Sopenharmony_ci		fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
910141cc406Sopenharmony_ci#endif
911141cc406Sopenharmony_ci	      }
912141cc406Sopenharmony_ci	    else
913141cc406Sopenharmony_ci	      {
914141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
915141cc406Sopenharmony_ci		n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
916141cc406Sopenharmony_ci				argv[0], c);
917141cc406Sopenharmony_ci#else
918141cc406Sopenharmony_ci		fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
919141cc406Sopenharmony_ci#endif
920141cc406Sopenharmony_ci	      }
921141cc406Sopenharmony_ci
922141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
923141cc406Sopenharmony_ci	    if (n >= 0)
924141cc406Sopenharmony_ci	      {
925141cc406Sopenharmony_ci		if (_IO_fwide (stderr, 0) > 0)
926141cc406Sopenharmony_ci		  __fwprintf (stderr, L"%s", buf);
927141cc406Sopenharmony_ci		else
928141cc406Sopenharmony_ci		  fputs (buf, stderr);
929141cc406Sopenharmony_ci
930141cc406Sopenharmony_ci		free (buf);
931141cc406Sopenharmony_ci	      }
932141cc406Sopenharmony_ci#endif
933141cc406Sopenharmony_ci	  }
934141cc406Sopenharmony_ci	optopt = c;
935141cc406Sopenharmony_ci	return '?';
936141cc406Sopenharmony_ci      }
937141cc406Sopenharmony_ci    /* Convenience. Treat POSIX -W foo same as long option --foo */
938141cc406Sopenharmony_ci    if (temp[0] == 'W' && temp[1] == ';')
939141cc406Sopenharmony_ci      {
940141cc406Sopenharmony_ci	char *nameend;
941141cc406Sopenharmony_ci	const struct option *p;
942141cc406Sopenharmony_ci	const struct option *pfound = NULL;
943141cc406Sopenharmony_ci	int exact = 0;
944141cc406Sopenharmony_ci	int ambig = 0;
945141cc406Sopenharmony_ci	int indfound = 0;
946141cc406Sopenharmony_ci	int option_index;
947141cc406Sopenharmony_ci
948141cc406Sopenharmony_ci	/* This is an option that requires an argument.  */
949141cc406Sopenharmony_ci	if (*nextchar != '\0')
950141cc406Sopenharmony_ci	  {
951141cc406Sopenharmony_ci	    optarg = nextchar;
952141cc406Sopenharmony_ci	    /* If we end this ARGV-element by taking the rest as an arg,
953141cc406Sopenharmony_ci	       we must advance to the next element now.  */
954141cc406Sopenharmony_ci	    optind++;
955141cc406Sopenharmony_ci	  }
956141cc406Sopenharmony_ci	else if (optind == argc)
957141cc406Sopenharmony_ci	  {
958141cc406Sopenharmony_ci	    if (print_errors)
959141cc406Sopenharmony_ci	      {
960141cc406Sopenharmony_ci		/* 1003.2 specifies the format of this message.  */
961141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
962141cc406Sopenharmony_ci		char *buf;
963141cc406Sopenharmony_ci
964141cc406Sopenharmony_ci		if (__asprintf (&buf,
965141cc406Sopenharmony_ci				_("%s: option requires an argument -- %c\n"),
966141cc406Sopenharmony_ci				argv[0], c) >= 0)
967141cc406Sopenharmony_ci		  {
968141cc406Sopenharmony_ci		    if (_IO_fwide (stderr, 0) > 0)
969141cc406Sopenharmony_ci		      __fwprintf (stderr, L"%s", buf);
970141cc406Sopenharmony_ci		    else
971141cc406Sopenharmony_ci		      fputs (buf, stderr);
972141cc406Sopenharmony_ci
973141cc406Sopenharmony_ci		    free (buf);
974141cc406Sopenharmony_ci		  }
975141cc406Sopenharmony_ci#else
976141cc406Sopenharmony_ci		fprintf (stderr, _("%s: option requires an argument -- %c\n"),
977141cc406Sopenharmony_ci			 argv[0], c);
978141cc406Sopenharmony_ci#endif
979141cc406Sopenharmony_ci	      }
980141cc406Sopenharmony_ci	    optopt = c;
981141cc406Sopenharmony_ci	    if (optstring[0] == ':')
982141cc406Sopenharmony_ci	      c = ':';
983141cc406Sopenharmony_ci	    else
984141cc406Sopenharmony_ci	      c = '?';
985141cc406Sopenharmony_ci	    return c;
986141cc406Sopenharmony_ci	  }
987141cc406Sopenharmony_ci	else
988141cc406Sopenharmony_ci	  /* We already incremented `optind' once;
989141cc406Sopenharmony_ci	     increment it again when taking next ARGV-elt as argument.  */
990141cc406Sopenharmony_ci	  optarg = argv[optind++];
991141cc406Sopenharmony_ci
992141cc406Sopenharmony_ci	/* optarg is now the argument, see if it's in the
993141cc406Sopenharmony_ci	   table of longopts.  */
994141cc406Sopenharmony_ci
995141cc406Sopenharmony_ci	for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
996141cc406Sopenharmony_ci	  /* Do nothing.  */ ;
997141cc406Sopenharmony_ci
998141cc406Sopenharmony_ci	/* Test all long options for either exact match
999141cc406Sopenharmony_ci	   or abbreviated matches.  */
1000141cc406Sopenharmony_ci	for (p = longopts, option_index = 0; p->name; p++, option_index++)
1001141cc406Sopenharmony_ci	  if (!strncmp (p->name, nextchar, nameend - nextchar))
1002141cc406Sopenharmony_ci	    {
1003141cc406Sopenharmony_ci	      if ((unsigned int) (nameend - nextchar) == strlen (p->name))
1004141cc406Sopenharmony_ci		{
1005141cc406Sopenharmony_ci		  /* Exact match found.  */
1006141cc406Sopenharmony_ci		  pfound = p;
1007141cc406Sopenharmony_ci		  indfound = option_index;
1008141cc406Sopenharmony_ci		  exact = 1;
1009141cc406Sopenharmony_ci		  break;
1010141cc406Sopenharmony_ci		}
1011141cc406Sopenharmony_ci	      else if (pfound == NULL)
1012141cc406Sopenharmony_ci		{
1013141cc406Sopenharmony_ci		  /* First nonexact match found.  */
1014141cc406Sopenharmony_ci		  pfound = p;
1015141cc406Sopenharmony_ci		  indfound = option_index;
1016141cc406Sopenharmony_ci		}
1017141cc406Sopenharmony_ci	      else
1018141cc406Sopenharmony_ci		/* Second or later nonexact match found.  */
1019141cc406Sopenharmony_ci		ambig = 1;
1020141cc406Sopenharmony_ci	    }
1021141cc406Sopenharmony_ci	if (ambig && !exact)
1022141cc406Sopenharmony_ci	  {
1023141cc406Sopenharmony_ci	    if (print_errors)
1024141cc406Sopenharmony_ci	      {
1025141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
1026141cc406Sopenharmony_ci		char *buf;
1027141cc406Sopenharmony_ci
1028141cc406Sopenharmony_ci		if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
1029141cc406Sopenharmony_ci				argv[0], argv[optind]) >= 0)
1030141cc406Sopenharmony_ci		  {
1031141cc406Sopenharmony_ci		    if (_IO_fwide (stderr, 0) > 0)
1032141cc406Sopenharmony_ci		      __fwprintf (stderr, L"%s", buf);
1033141cc406Sopenharmony_ci		    else
1034141cc406Sopenharmony_ci		      fputs (buf, stderr);
1035141cc406Sopenharmony_ci
1036141cc406Sopenharmony_ci		    free (buf);
1037141cc406Sopenharmony_ci		  }
1038141cc406Sopenharmony_ci#else
1039141cc406Sopenharmony_ci		fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
1040141cc406Sopenharmony_ci			 argv[0], argv[optind]);
1041141cc406Sopenharmony_ci#endif
1042141cc406Sopenharmony_ci	      }
1043141cc406Sopenharmony_ci	    nextchar += strlen (nextchar);
1044141cc406Sopenharmony_ci	    optind++;
1045141cc406Sopenharmony_ci	    return '?';
1046141cc406Sopenharmony_ci	  }
1047141cc406Sopenharmony_ci	if (pfound != NULL)
1048141cc406Sopenharmony_ci	  {
1049141cc406Sopenharmony_ci	    option_index = indfound;
1050141cc406Sopenharmony_ci	    if (*nameend)
1051141cc406Sopenharmony_ci	      {
1052141cc406Sopenharmony_ci		/* Don't test has_arg with >, because some C compilers don't
1053141cc406Sopenharmony_ci		   allow it to be used on enums.  */
1054141cc406Sopenharmony_ci		if (pfound->has_arg)
1055141cc406Sopenharmony_ci		  optarg = nameend + 1;
1056141cc406Sopenharmony_ci		else
1057141cc406Sopenharmony_ci		  {
1058141cc406Sopenharmony_ci		    if (print_errors)
1059141cc406Sopenharmony_ci		      {
1060141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
1061141cc406Sopenharmony_ci			char *buf;
1062141cc406Sopenharmony_ci
1063141cc406Sopenharmony_ci			if (__asprintf (&buf, _("\
1064141cc406Sopenharmony_ci%s: option `-W %s' doesn't allow an argument\n"),
1065141cc406Sopenharmony_ci					argv[0], pfound->name) >= 0)
1066141cc406Sopenharmony_ci			  {
1067141cc406Sopenharmony_ci			    if (_IO_fwide (stderr, 0) > 0)
1068141cc406Sopenharmony_ci			      __fwprintf (stderr, L"%s", buf);
1069141cc406Sopenharmony_ci			    else
1070141cc406Sopenharmony_ci			      fputs (buf, stderr);
1071141cc406Sopenharmony_ci
1072141cc406Sopenharmony_ci			    free (buf);
1073141cc406Sopenharmony_ci			  }
1074141cc406Sopenharmony_ci#else
1075141cc406Sopenharmony_ci			fprintf (stderr, _("\
1076141cc406Sopenharmony_ci%s: option `-W %s' doesn't allow an argument\n"),
1077141cc406Sopenharmony_ci				 argv[0], pfound->name);
1078141cc406Sopenharmony_ci#endif
1079141cc406Sopenharmony_ci		      }
1080141cc406Sopenharmony_ci
1081141cc406Sopenharmony_ci		    nextchar += strlen (nextchar);
1082141cc406Sopenharmony_ci		    return '?';
1083141cc406Sopenharmony_ci		  }
1084141cc406Sopenharmony_ci	      }
1085141cc406Sopenharmony_ci	    else if (pfound->has_arg == 1)
1086141cc406Sopenharmony_ci	      {
1087141cc406Sopenharmony_ci		if (optind < argc)
1088141cc406Sopenharmony_ci		  optarg = argv[optind++];
1089141cc406Sopenharmony_ci		else
1090141cc406Sopenharmony_ci		  {
1091141cc406Sopenharmony_ci		    if (print_errors)
1092141cc406Sopenharmony_ci		      {
1093141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
1094141cc406Sopenharmony_ci			char *buf;
1095141cc406Sopenharmony_ci
1096141cc406Sopenharmony_ci			if (__asprintf (&buf, _("\
1097141cc406Sopenharmony_ci%s: option `%s' requires an argument\n"),
1098141cc406Sopenharmony_ci					argv[0], argv[optind - 1]) >= 0)
1099141cc406Sopenharmony_ci			  {
1100141cc406Sopenharmony_ci			    if (_IO_fwide (stderr, 0) > 0)
1101141cc406Sopenharmony_ci			      __fwprintf (stderr, L"%s", buf);
1102141cc406Sopenharmony_ci			    else
1103141cc406Sopenharmony_ci			      fputs (buf, stderr);
1104141cc406Sopenharmony_ci
1105141cc406Sopenharmony_ci			    free (buf);
1106141cc406Sopenharmony_ci			  }
1107141cc406Sopenharmony_ci#else
1108141cc406Sopenharmony_ci			fprintf (stderr,
1109141cc406Sopenharmony_ci				 _("%s: option `%s' requires an argument\n"),
1110141cc406Sopenharmony_ci				 argv[0], argv[optind - 1]);
1111141cc406Sopenharmony_ci#endif
1112141cc406Sopenharmony_ci		      }
1113141cc406Sopenharmony_ci		    nextchar += strlen (nextchar);
1114141cc406Sopenharmony_ci		    return optstring[0] == ':' ? ':' : '?';
1115141cc406Sopenharmony_ci		  }
1116141cc406Sopenharmony_ci	      }
1117141cc406Sopenharmony_ci	    nextchar += strlen (nextchar);
1118141cc406Sopenharmony_ci	    if (longind != NULL)
1119141cc406Sopenharmony_ci	      *longind = option_index;
1120141cc406Sopenharmony_ci	    if (pfound->flag)
1121141cc406Sopenharmony_ci	      {
1122141cc406Sopenharmony_ci		*(pfound->flag) = pfound->val;
1123141cc406Sopenharmony_ci		return 0;
1124141cc406Sopenharmony_ci	      }
1125141cc406Sopenharmony_ci	    return pfound->val;
1126141cc406Sopenharmony_ci	  }
1127141cc406Sopenharmony_ci	  nextchar = NULL;
1128141cc406Sopenharmony_ci	  return 'W';	/* Let the application handle it.   */
1129141cc406Sopenharmony_ci      }
1130141cc406Sopenharmony_ci    if (temp[1] == ':')
1131141cc406Sopenharmony_ci      {
1132141cc406Sopenharmony_ci	if (temp[2] == ':')
1133141cc406Sopenharmony_ci	  {
1134141cc406Sopenharmony_ci	    /* This is an option that accepts an argument optionally.  */
1135141cc406Sopenharmony_ci	    if (*nextchar != '\0')
1136141cc406Sopenharmony_ci	      {
1137141cc406Sopenharmony_ci		optarg = nextchar;
1138141cc406Sopenharmony_ci		optind++;
1139141cc406Sopenharmony_ci	      }
1140141cc406Sopenharmony_ci	    else
1141141cc406Sopenharmony_ci	      optarg = NULL;
1142141cc406Sopenharmony_ci	    nextchar = NULL;
1143141cc406Sopenharmony_ci	  }
1144141cc406Sopenharmony_ci	else
1145141cc406Sopenharmony_ci	  {
1146141cc406Sopenharmony_ci	    /* This is an option that requires an argument.  */
1147141cc406Sopenharmony_ci	    if (*nextchar != '\0')
1148141cc406Sopenharmony_ci	      {
1149141cc406Sopenharmony_ci		optarg = nextchar;
1150141cc406Sopenharmony_ci		/* If we end this ARGV-element by taking the rest as an arg,
1151141cc406Sopenharmony_ci		   we must advance to the next element now.  */
1152141cc406Sopenharmony_ci		optind++;
1153141cc406Sopenharmony_ci	      }
1154141cc406Sopenharmony_ci	    else if (optind == argc)
1155141cc406Sopenharmony_ci	      {
1156141cc406Sopenharmony_ci		if (print_errors)
1157141cc406Sopenharmony_ci		  {
1158141cc406Sopenharmony_ci		    /* 1003.2 specifies the format of this message.  */
1159141cc406Sopenharmony_ci#if defined _LIBC && defined USE_IN_LIBIO
1160141cc406Sopenharmony_ci		    char *buf;
1161141cc406Sopenharmony_ci
1162141cc406Sopenharmony_ci		    if (__asprintf (&buf, _("\
1163141cc406Sopenharmony_ci%s: option requires an argument -- %c\n"),
1164141cc406Sopenharmony_ci				    argv[0], c) >= 0)
1165141cc406Sopenharmony_ci		      {
1166141cc406Sopenharmony_ci			if (_IO_fwide (stderr, 0) > 0)
1167141cc406Sopenharmony_ci			  __fwprintf (stderr, L"%s", buf);
1168141cc406Sopenharmony_ci			else
1169141cc406Sopenharmony_ci			  fputs (buf, stderr);
1170141cc406Sopenharmony_ci
1171141cc406Sopenharmony_ci			free (buf);
1172141cc406Sopenharmony_ci		      }
1173141cc406Sopenharmony_ci#else
1174141cc406Sopenharmony_ci		    fprintf (stderr,
1175141cc406Sopenharmony_ci			     _("%s: option requires an argument -- %c\n"),
1176141cc406Sopenharmony_ci			     argv[0], c);
1177141cc406Sopenharmony_ci#endif
1178141cc406Sopenharmony_ci		  }
1179141cc406Sopenharmony_ci		optopt = c;
1180141cc406Sopenharmony_ci		if (optstring[0] == ':')
1181141cc406Sopenharmony_ci		  c = ':';
1182141cc406Sopenharmony_ci		else
1183141cc406Sopenharmony_ci		  c = '?';
1184141cc406Sopenharmony_ci	      }
1185141cc406Sopenharmony_ci	    else
1186141cc406Sopenharmony_ci	      /* We already incremented `optind' once;
1187141cc406Sopenharmony_ci		 increment it again when taking next ARGV-elt as argument.  */
1188141cc406Sopenharmony_ci	      optarg = argv[optind++];
1189141cc406Sopenharmony_ci	    nextchar = NULL;
1190141cc406Sopenharmony_ci	  }
1191141cc406Sopenharmony_ci      }
1192141cc406Sopenharmony_ci    return c;
1193141cc406Sopenharmony_ci  }
1194141cc406Sopenharmony_ci}
1195141cc406Sopenharmony_ci
1196141cc406Sopenharmony_ciint
1197141cc406Sopenharmony_cigetopt (argc, argv, optstring)
1198141cc406Sopenharmony_ci     int argc;
1199141cc406Sopenharmony_ci     char *const *argv;
1200141cc406Sopenharmony_ci     const char *optstring;
1201141cc406Sopenharmony_ci{
1202141cc406Sopenharmony_ci  return _getopt_internal (argc, argv, optstring,
1203141cc406Sopenharmony_ci			   (const struct option *) 0,
1204141cc406Sopenharmony_ci			   (int *) 0,
1205141cc406Sopenharmony_ci			   0);
1206141cc406Sopenharmony_ci}
1207141cc406Sopenharmony_ci
1208141cc406Sopenharmony_ci#endif	/* Not ELIDE_CODE.  */
1209141cc406Sopenharmony_ci
1210141cc406Sopenharmony_ci#ifdef TEST
1211141cc406Sopenharmony_ci
1212141cc406Sopenharmony_ci/* Compile with -DTEST to make an executable for use in testing
1213141cc406Sopenharmony_ci   the above definition of `getopt'.  */
1214141cc406Sopenharmony_ci
1215141cc406Sopenharmony_ciint
1216141cc406Sopenharmony_cimain (argc, argv)
1217141cc406Sopenharmony_ci     int argc;
1218141cc406Sopenharmony_ci     char **argv;
1219141cc406Sopenharmony_ci{
1220141cc406Sopenharmony_ci  int c;
1221141cc406Sopenharmony_ci  int digit_optind = 0;
1222141cc406Sopenharmony_ci
1223141cc406Sopenharmony_ci  while (1)
1224141cc406Sopenharmony_ci    {
1225141cc406Sopenharmony_ci      int this_option_optind = optind ? optind : 1;
1226141cc406Sopenharmony_ci
1227141cc406Sopenharmony_ci      c = getopt (argc, argv, "abc:d:0123456789");
1228141cc406Sopenharmony_ci      if (c == -1)
1229141cc406Sopenharmony_ci	break;
1230141cc406Sopenharmony_ci
1231141cc406Sopenharmony_ci      switch (c)
1232141cc406Sopenharmony_ci	{
1233141cc406Sopenharmony_ci	case '0':
1234141cc406Sopenharmony_ci	case '1':
1235141cc406Sopenharmony_ci	case '2':
1236141cc406Sopenharmony_ci	case '3':
1237141cc406Sopenharmony_ci	case '4':
1238141cc406Sopenharmony_ci	case '5':
1239141cc406Sopenharmony_ci	case '6':
1240141cc406Sopenharmony_ci	case '7':
1241141cc406Sopenharmony_ci	case '8':
1242141cc406Sopenharmony_ci	case '9':
1243141cc406Sopenharmony_ci	  if (digit_optind != 0 && digit_optind != this_option_optind)
1244141cc406Sopenharmony_ci	    printf ("digits occur in two different argv-elements.\n");
1245141cc406Sopenharmony_ci	  digit_optind = this_option_optind;
1246141cc406Sopenharmony_ci	  printf ("option %c\n", c);
1247141cc406Sopenharmony_ci	  break;
1248141cc406Sopenharmony_ci
1249141cc406Sopenharmony_ci	case 'a':
1250141cc406Sopenharmony_ci	  printf ("option a\n");
1251141cc406Sopenharmony_ci	  break;
1252141cc406Sopenharmony_ci
1253141cc406Sopenharmony_ci	case 'b':
1254141cc406Sopenharmony_ci	  printf ("option b\n");
1255141cc406Sopenharmony_ci	  break;
1256141cc406Sopenharmony_ci
1257141cc406Sopenharmony_ci	case 'c':
1258141cc406Sopenharmony_ci	  printf ("option c with value `%s'\n", optarg);
1259141cc406Sopenharmony_ci	  break;
1260141cc406Sopenharmony_ci
1261141cc406Sopenharmony_ci	case '?':
1262141cc406Sopenharmony_ci	  break;
1263141cc406Sopenharmony_ci
1264141cc406Sopenharmony_ci	default:
1265141cc406Sopenharmony_ci	  printf ("?? getopt returned character code 0%o ??\n", c);
1266141cc406Sopenharmony_ci	}
1267141cc406Sopenharmony_ci    }
1268141cc406Sopenharmony_ci
1269141cc406Sopenharmony_ci  if (optind < argc)
1270141cc406Sopenharmony_ci    {
1271141cc406Sopenharmony_ci      printf ("non-option ARGV-elements: ");
1272141cc406Sopenharmony_ci      while (optind < argc)
1273141cc406Sopenharmony_ci	printf ("%s ", argv[optind++]);
1274141cc406Sopenharmony_ci      printf ("\n");
1275141cc406Sopenharmony_ci    }
1276141cc406Sopenharmony_ci
1277141cc406Sopenharmony_ci  exit (0);
1278141cc406Sopenharmony_ci}
1279141cc406Sopenharmony_ci
1280141cc406Sopenharmony_ci#endif /* TEST */
1281141cc406Sopenharmony_ci#endif /* !HAVE_GETOPT_LONG */
1282