xref: /freebsd-12.1/lib/libc/stdlib/getopt_long.c (revision e6fc380c)
1a35a7e76SEric Melville /*	$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $	*/
2a35a7e76SEric Melville /*	$FreeBSD$ */
3a35a7e76SEric Melville 
4a35a7e76SEric Melville /*-
5a35a7e76SEric Melville  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6a35a7e76SEric Melville  * All rights reserved.
7a35a7e76SEric Melville  *
8a35a7e76SEric Melville  * This code is derived from software contributed to The NetBSD Foundation
9a35a7e76SEric Melville  * by Dieter Baron and Thomas Klausner.
10a35a7e76SEric Melville  *
11a35a7e76SEric Melville  * Redistribution and use in source and binary forms, with or without
12a35a7e76SEric Melville  * modification, are permitted provided that the following conditions
13a35a7e76SEric Melville  * are met:
14a35a7e76SEric Melville  * 1. Redistributions of source code must retain the above copyright
15a35a7e76SEric Melville  *    notice, this list of conditions and the following disclaimer.
16a35a7e76SEric Melville  * 2. Redistributions in binary form must reproduce the above copyright
17a35a7e76SEric Melville  *    notice, this list of conditions and the following disclaimer in the
18a35a7e76SEric Melville  *    documentation and/or other materials provided with the distribution.
19a35a7e76SEric Melville  * 3. All advertising materials mentioning features or use of this software
20a35a7e76SEric Melville  *    must display the following acknowledgement:
21a35a7e76SEric Melville  *        This product includes software developed by the NetBSD
22a35a7e76SEric Melville  *        Foundation, Inc. and its contributors.
23a35a7e76SEric Melville  * 4. Neither the name of The NetBSD Foundation nor the names of its
24a35a7e76SEric Melville  *    contributors may be used to endorse or promote products derived
25a35a7e76SEric Melville  *    from this software without specific prior written permission.
26a35a7e76SEric Melville  *
27a35a7e76SEric Melville  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28a35a7e76SEric Melville  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29a35a7e76SEric Melville  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30a35a7e76SEric Melville  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31a35a7e76SEric Melville  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32a35a7e76SEric Melville  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33a35a7e76SEric Melville  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34a35a7e76SEric Melville  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35a35a7e76SEric Melville  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36a35a7e76SEric Melville  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37a35a7e76SEric Melville  * POSSIBILITY OF SUCH DAMAGE.
38a35a7e76SEric Melville  */
39a35a7e76SEric Melville 
40a35a7e76SEric Melville #include <sys/cdefs.h>
41a35a7e76SEric Melville #if defined(LIBC_SCCS) && !defined(lint)
42a35a7e76SEric Melville __RCSID("$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $");
43a35a7e76SEric Melville #endif /* LIBC_SCCS and not lint */
44a35a7e76SEric Melville 
45a35a7e76SEric Melville #include "namespace.h"
46a35a7e76SEric Melville 
47a35a7e76SEric Melville #include <assert.h>
48a35a7e76SEric Melville #include <err.h>
49a35a7e76SEric Melville #include <errno.h>
50a35a7e76SEric Melville #include <getopt.h>
51a35a7e76SEric Melville #include <stdlib.h>
52a35a7e76SEric Melville #include <string.h>
53a35a7e76SEric Melville 
54a35a7e76SEric Melville /* not part of the original file */
55a35a7e76SEric Melville #ifndef _DIAGASSERT
56a35a7e76SEric Melville #define _DIAGASSERT(X)
57a35a7e76SEric Melville #endif
58a35a7e76SEric Melville 
59a35a7e76SEric Melville #if HAVE_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND
60a35a7e76SEric Melville #define REPLACE_GETOPT
61a35a7e76SEric Melville #endif
62a35a7e76SEric Melville 
63a35a7e76SEric Melville #ifdef REPLACE_GETOPT
64a35a7e76SEric Melville #ifdef __weak_alias
65a35a7e76SEric Melville __weak_alias(getopt,_getopt)
66a35a7e76SEric Melville #endif
67a35a7e76SEric Melville int	opterr = 1;		/* if error message should be printed */
68a35a7e76SEric Melville int	optind = 1;		/* index into parent argv vector */
69a35a7e76SEric Melville int	optopt = '?';		/* character checked for validity */
70a35a7e76SEric Melville int	optreset;		/* reset getopt */
71a35a7e76SEric Melville char    *optarg;		/* argument associated with option */
72a35a7e76SEric Melville #elif HAVE_CONFIG_H && !HAVE_DECL_OPTRESET
73a35a7e76SEric Melville static int optreset;
74a35a7e76SEric Melville #endif
75a35a7e76SEric Melville 
76a35a7e76SEric Melville #ifdef __weak_alias
77a35a7e76SEric Melville __weak_alias(getopt_long,_getopt_long)
78a35a7e76SEric Melville #endif
79a35a7e76SEric Melville 
80a35a7e76SEric Melville #if !HAVE_GETOPT_LONG
81a35a7e76SEric Melville #define IGNORE_FIRST	(*options == '-' || *options == '+')
82a35a7e76SEric Melville #define PRINT_ERROR	((opterr) && ((*options != ':') \
83a35a7e76SEric Melville 				      || (IGNORE_FIRST && options[1] != ':')))
84a35a7e76SEric Melville #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
85a35a7e76SEric Melville #define PERMUTE         (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
86a35a7e76SEric Melville /* XXX: GNU ignores PC if *options == '-' */
87a35a7e76SEric Melville #define IN_ORDER        (!IS_POSIXLY_CORRECT && *options == '-')
88a35a7e76SEric Melville 
89a35a7e76SEric Melville /* return values */
90a35a7e76SEric Melville #define	BADCH	(int)'?'
91a35a7e76SEric Melville #define	BADARG		((IGNORE_FIRST && options[1] == ':') \
92a35a7e76SEric Melville 			 || (*options == ':') ? (int)':' : (int)'?')
93a35a7e76SEric Melville #define INORDER (int)1
94a35a7e76SEric Melville 
95a35a7e76SEric Melville #define	EMSG	""
96a35a7e76SEric Melville 
97*e6fc380cSAlfred Perlstein static int getopt_internal(int, char * const *, const char *);
98*e6fc380cSAlfred Perlstein static int gcd(int, int);
99*e6fc380cSAlfred Perlstein static void permute_args(int, int, int, char * const *);
100a35a7e76SEric Melville 
101a35a7e76SEric Melville static char *place = EMSG; /* option letter processing */
102a35a7e76SEric Melville 
103a35a7e76SEric Melville /* XXX: set optreset to 1 rather than these two */
104a35a7e76SEric Melville static int nonopt_start = -1; /* first non option argument (for permute) */
105a35a7e76SEric Melville static int nonopt_end = -1;   /* first option after non options (for permute) */
106a35a7e76SEric Melville 
107a35a7e76SEric Melville /* Error messages */
108a35a7e76SEric Melville static const char recargchar[] = "option requires an argument -- %c";
109a35a7e76SEric Melville static const char recargstring[] = "option requires an argument -- %s";
110a35a7e76SEric Melville static const char ambig[] = "ambiguous option -- %.*s";
111a35a7e76SEric Melville static const char noarg[] = "option doesn't take an argument -- %.*s";
112a35a7e76SEric Melville static const char illoptchar[] = "unknown option -- %c";
113a35a7e76SEric Melville static const char illoptstring[] = "unknown option -- %s";
114a35a7e76SEric Melville 
115a35a7e76SEric Melville 
116a35a7e76SEric Melville /*
117a35a7e76SEric Melville  * Compute the greatest common divisor of a and b.
118a35a7e76SEric Melville  */
119a35a7e76SEric Melville static int
120a35a7e76SEric Melville gcd(a, b)
121a35a7e76SEric Melville 	int a;
122a35a7e76SEric Melville 	int b;
123a35a7e76SEric Melville {
124a35a7e76SEric Melville 	int c;
125a35a7e76SEric Melville 
126a35a7e76SEric Melville 	c = a % b;
127a35a7e76SEric Melville 	while (c != 0) {
128a35a7e76SEric Melville 		a = b;
129a35a7e76SEric Melville 		b = c;
130a35a7e76SEric Melville 		c = a % b;
131a35a7e76SEric Melville 	}
132a35a7e76SEric Melville 
133a35a7e76SEric Melville 	return b;
134a35a7e76SEric Melville }
135a35a7e76SEric Melville 
136a35a7e76SEric Melville /*
137a35a7e76SEric Melville  * Exchange the block from nonopt_start to nonopt_end with the block
138a35a7e76SEric Melville  * from nonopt_end to opt_end (keeping the same order of arguments
139a35a7e76SEric Melville  * in each block).
140a35a7e76SEric Melville  */
141a35a7e76SEric Melville static void
142a35a7e76SEric Melville permute_args(panonopt_start, panonopt_end, opt_end, nargv)
143a35a7e76SEric Melville 	int panonopt_start;
144a35a7e76SEric Melville 	int panonopt_end;
145a35a7e76SEric Melville 	int opt_end;
146a35a7e76SEric Melville 	char * const *nargv;
147a35a7e76SEric Melville {
148a35a7e76SEric Melville 	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
149a35a7e76SEric Melville 	char *swap;
150a35a7e76SEric Melville 
151a35a7e76SEric Melville 	_DIAGASSERT(nargv != NULL);
152a35a7e76SEric Melville 
153a35a7e76SEric Melville 	/*
154a35a7e76SEric Melville 	 * compute lengths of blocks and number and size of cycles
155a35a7e76SEric Melville 	 */
156a35a7e76SEric Melville 	nnonopts = panonopt_end - panonopt_start;
157a35a7e76SEric Melville 	nopts = opt_end - panonopt_end;
158a35a7e76SEric Melville 	ncycle = gcd(nnonopts, nopts);
159a35a7e76SEric Melville 	cyclelen = (opt_end - panonopt_start) / ncycle;
160a35a7e76SEric Melville 
161a35a7e76SEric Melville 	for (i = 0; i < ncycle; i++) {
162a35a7e76SEric Melville 		cstart = panonopt_end+i;
163a35a7e76SEric Melville 		pos = cstart;
164a35a7e76SEric Melville 		for (j = 0; j < cyclelen; j++) {
165a35a7e76SEric Melville 			if (pos >= panonopt_end)
166a35a7e76SEric Melville 				pos -= nnonopts;
167a35a7e76SEric Melville 			else
168a35a7e76SEric Melville 				pos += nopts;
169a35a7e76SEric Melville 			swap = nargv[pos];
170a35a7e76SEric Melville 			/* LINTED const cast */
171a35a7e76SEric Melville 			((char **) nargv)[pos] = nargv[cstart];
172a35a7e76SEric Melville 			/* LINTED const cast */
173a35a7e76SEric Melville 			((char **)nargv)[cstart] = swap;
174a35a7e76SEric Melville 		}
175a35a7e76SEric Melville 	}
176a35a7e76SEric Melville }
177a35a7e76SEric Melville 
178a35a7e76SEric Melville /*
179a35a7e76SEric Melville  * getopt_internal --
180a35a7e76SEric Melville  *	Parse argc/argv argument vector.  Called by user level routines.
181a35a7e76SEric Melville  *  Returns -2 if -- is found (can be long option or end of options marker).
182a35a7e76SEric Melville  */
183a35a7e76SEric Melville static int
184a35a7e76SEric Melville getopt_internal(nargc, nargv, options)
185a35a7e76SEric Melville 	int nargc;
186a35a7e76SEric Melville 	char * const *nargv;
187a35a7e76SEric Melville 	const char *options;
188a35a7e76SEric Melville {
189a35a7e76SEric Melville 	char *oli;				/* option letter list index */
190a35a7e76SEric Melville 	int optchar;
191a35a7e76SEric Melville 
192a35a7e76SEric Melville 	_DIAGASSERT(nargv != NULL);
193a35a7e76SEric Melville 	_DIAGASSERT(options != NULL);
194a35a7e76SEric Melville 
195a35a7e76SEric Melville 	optarg = NULL;
196a35a7e76SEric Melville 
197a35a7e76SEric Melville 	/*
198a35a7e76SEric Melville 	 * XXX Some programs (like rsyncd) expect to be able to
199a35a7e76SEric Melville 	 * XXX re-initialize optind to 0 and have getopt_long(3)
200a35a7e76SEric Melville 	 * XXX properly function again.  Work around this braindamage.
201a35a7e76SEric Melville 	 */
202a35a7e76SEric Melville 	if (optind == 0)
203a35a7e76SEric Melville 		optind = 1;
204a35a7e76SEric Melville 
205a35a7e76SEric Melville 	if (optreset)
206a35a7e76SEric Melville 		nonopt_start = nonopt_end = -1;
207a35a7e76SEric Melville start:
208a35a7e76SEric Melville 	if (optreset || !*place) {		/* update scanning pointer */
209a35a7e76SEric Melville 		optreset = 0;
210a35a7e76SEric Melville 		if (optind >= nargc) {          /* end of argument vector */
211a35a7e76SEric Melville 			place = EMSG;
212a35a7e76SEric Melville 			if (nonopt_end != -1) {
213a35a7e76SEric Melville 				/* do permutation, if we have to */
214a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
215a35a7e76SEric Melville 				    optind, nargv);
216a35a7e76SEric Melville 				optind -= nonopt_end - nonopt_start;
217a35a7e76SEric Melville 			}
218a35a7e76SEric Melville 			else if (nonopt_start != -1) {
219a35a7e76SEric Melville 				/*
220a35a7e76SEric Melville 				 * If we skipped non-options, set optind
221a35a7e76SEric Melville 				 * to the first of them.
222a35a7e76SEric Melville 				 */
223a35a7e76SEric Melville 				optind = nonopt_start;
224a35a7e76SEric Melville 			}
225a35a7e76SEric Melville 			nonopt_start = nonopt_end = -1;
226a35a7e76SEric Melville 			return -1;
227a35a7e76SEric Melville 		}
228a35a7e76SEric Melville 		if ((*(place = nargv[optind]) != '-')
229a35a7e76SEric Melville 		    || (place[1] == '\0')) {    /* found non-option */
230a35a7e76SEric Melville 			place = EMSG;
231a35a7e76SEric Melville 			if (IN_ORDER) {
232a35a7e76SEric Melville 				/*
233a35a7e76SEric Melville 				 * GNU extension:
234a35a7e76SEric Melville 				 * return non-option as argument to option 1
235a35a7e76SEric Melville 				 */
236a35a7e76SEric Melville 				optarg = nargv[optind++];
237a35a7e76SEric Melville 				return INORDER;
238a35a7e76SEric Melville 			}
239a35a7e76SEric Melville 			if (!PERMUTE) {
240a35a7e76SEric Melville 				/*
241a35a7e76SEric Melville 				 * if no permutation wanted, stop parsing
242a35a7e76SEric Melville 				 * at first non-option
243a35a7e76SEric Melville 				 */
244a35a7e76SEric Melville 				return -1;
245a35a7e76SEric Melville 			}
246a35a7e76SEric Melville 			/* do permutation */
247a35a7e76SEric Melville 			if (nonopt_start == -1)
248a35a7e76SEric Melville 				nonopt_start = optind;
249a35a7e76SEric Melville 			else if (nonopt_end != -1) {
250a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
251a35a7e76SEric Melville 				    optind, nargv);
252a35a7e76SEric Melville 				nonopt_start = optind -
253a35a7e76SEric Melville 				    (nonopt_end - nonopt_start);
254a35a7e76SEric Melville 				nonopt_end = -1;
255a35a7e76SEric Melville 			}
256a35a7e76SEric Melville 			optind++;
257a35a7e76SEric Melville 			/* process next argument */
258a35a7e76SEric Melville 			goto start;
259a35a7e76SEric Melville 		}
260a35a7e76SEric Melville 		if (nonopt_start != -1 && nonopt_end == -1)
261a35a7e76SEric Melville 			nonopt_end = optind;
262a35a7e76SEric Melville 		if (place[1] && *++place == '-') {	/* found "--" */
263a35a7e76SEric Melville 			place++;
264a35a7e76SEric Melville 			return -2;
265a35a7e76SEric Melville 		}
266a35a7e76SEric Melville 	}
267a35a7e76SEric Melville 	if ((optchar = (int)*place++) == (int)':' ||
268a35a7e76SEric Melville 	    (oli = strchr(options + (IGNORE_FIRST ? 1 : 0), optchar)) == NULL) {
269a35a7e76SEric Melville 		/* option letter unknown or ':' */
270a35a7e76SEric Melville 		if (!*place)
271a35a7e76SEric Melville 			++optind;
272a35a7e76SEric Melville 		if (PRINT_ERROR)
273a35a7e76SEric Melville 			warnx(illoptchar, optchar);
274a35a7e76SEric Melville 		optopt = optchar;
275a35a7e76SEric Melville 		return BADCH;
276a35a7e76SEric Melville 	}
277a35a7e76SEric Melville 	if (optchar == 'W' && oli[1] == ';') {		/* -W long-option */
278a35a7e76SEric Melville 		/* XXX: what if no long options provided (called by getopt)? */
279a35a7e76SEric Melville 		if (*place)
280a35a7e76SEric Melville 			return -2;
281a35a7e76SEric Melville 
282a35a7e76SEric Melville 		if (++optind >= nargc) {	/* no arg */
283a35a7e76SEric Melville 			place = EMSG;
284a35a7e76SEric Melville 			if (PRINT_ERROR)
285a35a7e76SEric Melville 				warnx(recargchar, optchar);
286a35a7e76SEric Melville 			optopt = optchar;
287a35a7e76SEric Melville 			return BADARG;
288a35a7e76SEric Melville 		} else				/* white space */
289a35a7e76SEric Melville 			place = nargv[optind];
290a35a7e76SEric Melville 		/*
291a35a7e76SEric Melville 		 * Handle -W arg the same as --arg (which causes getopt to
292a35a7e76SEric Melville 		 * stop parsing).
293a35a7e76SEric Melville 		 */
294a35a7e76SEric Melville 		return -2;
295a35a7e76SEric Melville 	}
296a35a7e76SEric Melville 	if (*++oli != ':') {			/* doesn't take argument */
297a35a7e76SEric Melville 		if (!*place)
298a35a7e76SEric Melville 			++optind;
299a35a7e76SEric Melville 	} else {				/* takes (optional) argument */
300a35a7e76SEric Melville 		optarg = NULL;
301a35a7e76SEric Melville 		if (*place)			/* no white space */
302a35a7e76SEric Melville 			optarg = place;
303a35a7e76SEric Melville 		/* XXX: disable test for :: if PC? (GNU doesn't) */
304a35a7e76SEric Melville 		else if (oli[1] != ':') {	/* arg not optional */
305a35a7e76SEric Melville 			if (++optind >= nargc) {	/* no arg */
306a35a7e76SEric Melville 				place = EMSG;
307a35a7e76SEric Melville 				if (PRINT_ERROR)
308a35a7e76SEric Melville 					warnx(recargchar, optchar);
309a35a7e76SEric Melville 				optopt = optchar;
310a35a7e76SEric Melville 				return BADARG;
311a35a7e76SEric Melville 			} else
312a35a7e76SEric Melville 				optarg = nargv[optind];
313a35a7e76SEric Melville 		}
314a35a7e76SEric Melville 		place = EMSG;
315a35a7e76SEric Melville 		++optind;
316a35a7e76SEric Melville 	}
317a35a7e76SEric Melville 	/* dump back option letter */
318a35a7e76SEric Melville 	return optchar;
319a35a7e76SEric Melville }
320a35a7e76SEric Melville 
321a35a7e76SEric Melville #ifdef REPLACE_GETOPT
322a35a7e76SEric Melville /*
323a35a7e76SEric Melville  * getopt --
324a35a7e76SEric Melville  *	Parse argc/argv argument vector.
325a35a7e76SEric Melville  *
326a35a7e76SEric Melville  * [eventually this will replace the real getopt]
327a35a7e76SEric Melville  */
328a35a7e76SEric Melville int
329a35a7e76SEric Melville getopt(nargc, nargv, options)
330a35a7e76SEric Melville 	int nargc;
331a35a7e76SEric Melville 	char * const *nargv;
332a35a7e76SEric Melville 	const char *options;
333a35a7e76SEric Melville {
334a35a7e76SEric Melville 	int retval;
335a35a7e76SEric Melville 
336a35a7e76SEric Melville 	_DIAGASSERT(nargv != NULL);
337a35a7e76SEric Melville 	_DIAGASSERT(options != NULL);
338a35a7e76SEric Melville 
339a35a7e76SEric Melville 	if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
340a35a7e76SEric Melville 		++optind;
341a35a7e76SEric Melville 		/*
342a35a7e76SEric Melville 		 * We found an option (--), so if we skipped non-options,
343a35a7e76SEric Melville 		 * we have to permute.
344a35a7e76SEric Melville 		 */
345a35a7e76SEric Melville 		if (nonopt_end != -1) {
346a35a7e76SEric Melville 			permute_args(nonopt_start, nonopt_end, optind,
347a35a7e76SEric Melville 				       nargv);
348a35a7e76SEric Melville 			optind -= nonopt_end - nonopt_start;
349a35a7e76SEric Melville 		}
350a35a7e76SEric Melville 		nonopt_start = nonopt_end = -1;
351a35a7e76SEric Melville 		retval = -1;
352a35a7e76SEric Melville 	}
353a35a7e76SEric Melville 	return retval;
354a35a7e76SEric Melville }
355a35a7e76SEric Melville #endif
356a35a7e76SEric Melville 
357a35a7e76SEric Melville /*
358a35a7e76SEric Melville  * getopt_long --
359a35a7e76SEric Melville  *	Parse argc/argv argument vector.
360a35a7e76SEric Melville  */
361a35a7e76SEric Melville int
362a35a7e76SEric Melville getopt_long(nargc, nargv, options, long_options, idx)
363a35a7e76SEric Melville 	int nargc;
364a35a7e76SEric Melville 	char * const *nargv;
365a35a7e76SEric Melville 	const char *options;
366a35a7e76SEric Melville 	const struct option *long_options;
367a35a7e76SEric Melville 	int *idx;
368a35a7e76SEric Melville {
369a35a7e76SEric Melville 	int retval;
370a35a7e76SEric Melville 
371a35a7e76SEric Melville 	_DIAGASSERT(nargv != NULL);
372a35a7e76SEric Melville 	_DIAGASSERT(options != NULL);
373a35a7e76SEric Melville 	_DIAGASSERT(long_options != NULL);
374a35a7e76SEric Melville 	/* idx may be NULL */
375a35a7e76SEric Melville 
376a35a7e76SEric Melville 	if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
377a35a7e76SEric Melville 		char *current_argv, *has_equal;
378a35a7e76SEric Melville 		size_t current_argv_len;
379a35a7e76SEric Melville 		int i, match;
380a35a7e76SEric Melville 
381a35a7e76SEric Melville 		current_argv = place;
382a35a7e76SEric Melville 		match = -1;
383a35a7e76SEric Melville 
384a35a7e76SEric Melville 		optind++;
385a35a7e76SEric Melville 		place = EMSG;
386a35a7e76SEric Melville 
387a35a7e76SEric Melville 		if (*current_argv == '\0') {		/* found "--" */
388a35a7e76SEric Melville 			/*
389a35a7e76SEric Melville 			 * We found an option (--), so if we skipped
390a35a7e76SEric Melville 			 * non-options, we have to permute.
391a35a7e76SEric Melville 			 */
392a35a7e76SEric Melville 			if (nonopt_end != -1) {
393a35a7e76SEric Melville 				permute_args(nonopt_start, nonopt_end,
394a35a7e76SEric Melville 				    optind, nargv);
395a35a7e76SEric Melville 				optind -= nonopt_end - nonopt_start;
396a35a7e76SEric Melville 			}
397a35a7e76SEric Melville 			nonopt_start = nonopt_end = -1;
398a35a7e76SEric Melville 			return -1;
399a35a7e76SEric Melville 		}
400a35a7e76SEric Melville 		if ((has_equal = strchr(current_argv, '=')) != NULL) {
401a35a7e76SEric Melville 			/* argument found (--option=arg) */
402a35a7e76SEric Melville 			current_argv_len = has_equal - current_argv;
403a35a7e76SEric Melville 			has_equal++;
404a35a7e76SEric Melville 		} else
405a35a7e76SEric Melville 			current_argv_len = strlen(current_argv);
406a35a7e76SEric Melville 
407a35a7e76SEric Melville 		for (i = 0; long_options[i].name; i++) {
408a35a7e76SEric Melville 			/* find matching long option */
409a35a7e76SEric Melville 			if (strncmp(current_argv, long_options[i].name,
410a35a7e76SEric Melville 			    current_argv_len))
411a35a7e76SEric Melville 				continue;
412a35a7e76SEric Melville 
413a35a7e76SEric Melville 			if (strlen(long_options[i].name) ==
414a35a7e76SEric Melville 			    (unsigned)current_argv_len) {
415a35a7e76SEric Melville 				/* exact match */
416a35a7e76SEric Melville 				match = i;
417a35a7e76SEric Melville 				break;
418a35a7e76SEric Melville 			}
419a35a7e76SEric Melville 			if (match == -1)		/* partial match */
420a35a7e76SEric Melville 				match = i;
421a35a7e76SEric Melville 			else {
422a35a7e76SEric Melville 				/* ambiguous abbreviation */
423a35a7e76SEric Melville 				if (PRINT_ERROR)
424a35a7e76SEric Melville 					warnx(ambig, (int)current_argv_len,
425a35a7e76SEric Melville 					     current_argv);
426a35a7e76SEric Melville 				optopt = 0;
427a35a7e76SEric Melville 				return BADCH;
428a35a7e76SEric Melville 			}
429a35a7e76SEric Melville 		}
430a35a7e76SEric Melville 		if (match != -1) {			/* option found */
431a35a7e76SEric Melville 		        if (long_options[match].has_arg == no_argument
432a35a7e76SEric Melville 			    && has_equal) {
433a35a7e76SEric Melville 				if (PRINT_ERROR)
434a35a7e76SEric Melville 					warnx(noarg, (int)current_argv_len,
435a35a7e76SEric Melville 					     current_argv);
436a35a7e76SEric Melville 				/*
437a35a7e76SEric Melville 				 * XXX: GNU sets optopt to val regardless of
438a35a7e76SEric Melville 				 * flag
439a35a7e76SEric Melville 				 */
440a35a7e76SEric Melville 				if (long_options[match].flag == NULL)
441a35a7e76SEric Melville 					optopt = long_options[match].val;
442a35a7e76SEric Melville 				else
443a35a7e76SEric Melville 					optopt = 0;
444a35a7e76SEric Melville 				return BADARG;
445a35a7e76SEric Melville 			}
446a35a7e76SEric Melville 			if (long_options[match].has_arg == required_argument ||
447a35a7e76SEric Melville 			    long_options[match].has_arg == optional_argument) {
448a35a7e76SEric Melville 				if (has_equal)
449a35a7e76SEric Melville 					optarg = has_equal;
450a35a7e76SEric Melville 				else if (long_options[match].has_arg ==
451a35a7e76SEric Melville 				    required_argument) {
452a35a7e76SEric Melville 					/*
453a35a7e76SEric Melville 					 * optional argument doesn't use
454a35a7e76SEric Melville 					 * next nargv
455a35a7e76SEric Melville 					 */
456a35a7e76SEric Melville 					optarg = nargv[optind++];
457a35a7e76SEric Melville 				}
458a35a7e76SEric Melville 			}
459a35a7e76SEric Melville 			if ((long_options[match].has_arg == required_argument)
460a35a7e76SEric Melville 			    && (optarg == NULL)) {
461a35a7e76SEric Melville 				/*
462a35a7e76SEric Melville 				 * Missing argument; leading ':'
463a35a7e76SEric Melville 				 * indicates no error should be generated
464a35a7e76SEric Melville 				 */
465a35a7e76SEric Melville 				if (PRINT_ERROR)
466a35a7e76SEric Melville 					warnx(recargstring, current_argv);
467a35a7e76SEric Melville 				/*
468a35a7e76SEric Melville 				 * XXX: GNU sets optopt to val regardless
469a35a7e76SEric Melville 				 * of flag
470a35a7e76SEric Melville 				 */
471a35a7e76SEric Melville 				if (long_options[match].flag == NULL)
472a35a7e76SEric Melville 					optopt = long_options[match].val;
473a35a7e76SEric Melville 				else
474a35a7e76SEric Melville 					optopt = 0;
475a35a7e76SEric Melville 				--optind;
476a35a7e76SEric Melville 				return BADARG;
477a35a7e76SEric Melville 			}
478a35a7e76SEric Melville 		} else {			/* unknown option */
479a35a7e76SEric Melville 			if (PRINT_ERROR)
480a35a7e76SEric Melville 				warnx(illoptstring, current_argv);
481a35a7e76SEric Melville 			optopt = 0;
482a35a7e76SEric Melville 			return BADCH;
483a35a7e76SEric Melville 		}
484a35a7e76SEric Melville 		if (long_options[match].flag) {
485a35a7e76SEric Melville 			*long_options[match].flag = long_options[match].val;
486a35a7e76SEric Melville 			retval = 0;
487a35a7e76SEric Melville 		} else
488a35a7e76SEric Melville 			retval = long_options[match].val;
489a35a7e76SEric Melville 		if (idx)
490a35a7e76SEric Melville 			*idx = match;
491a35a7e76SEric Melville 	}
492a35a7e76SEric Melville 	return retval;
493a35a7e76SEric Melville }
494a35a7e76SEric Melville #endif /* !GETOPT_LONG */
495