1255670Sdes/*	$OpenBSD: getopt_long.c,v 1.25 2011/03/05 22:10:11 guenther Exp $	*/
2255670Sdes/*	$NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $	*/
3255670Sdes
4255670Sdes/*
5255670Sdes * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
6255670Sdes *
7255670Sdes * Permission to use, copy, modify, and distribute this software for any
8255670Sdes * purpose with or without fee is hereby granted, provided that the above
9255670Sdes * copyright notice and this permission notice appear in all copies.
10255670Sdes *
11255670Sdes * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12255670Sdes * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13255670Sdes * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14255670Sdes * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15255670Sdes * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16255670Sdes * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17255670Sdes * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18255670Sdes *
19255670Sdes * Sponsored in part by the Defense Advanced Research Projects
20255670Sdes * Agency (DARPA) and Air Force Research Laboratory, Air Force
21255670Sdes * Materiel Command, USAF, under agreement number F39502-99-1-0512.
22255670Sdes */
23255670Sdes/*-
24255670Sdes * Copyright (c) 2000 The NetBSD Foundation, Inc.
25255670Sdes * All rights reserved.
26255670Sdes *
27255670Sdes * This code is derived from software contributed to The NetBSD Foundation
28255670Sdes * by Dieter Baron and Thomas Klausner.
29255670Sdes *
30255670Sdes * Redistribution and use in source and binary forms, with or without
31255670Sdes * modification, are permitted provided that the following conditions
32255670Sdes * are met:
33255670Sdes * 1. Redistributions of source code must retain the above copyright
34255670Sdes *    notice, this list of conditions and the following disclaimer.
35255670Sdes * 2. Redistributions in binary form must reproduce the above copyright
36255670Sdes *    notice, this list of conditions and the following disclaimer in the
37255670Sdes *    documentation and/or other materials provided with the distribution.
38255670Sdes *
39255670Sdes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
40255670Sdes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41255670Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42255670Sdes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
43255670Sdes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44255670Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45255670Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46255670Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47255670Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48255670Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49255670Sdes * POSSIBILITY OF SUCH DAMAGE.
50255670Sdes */
51255670Sdes
52255670Sdes/* OPENBSD ORIGINAL: lib/libc/stdlib/getopt_long.c */
53255670Sdes#include "includes.h"
54255670Sdes
55255670Sdes#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
56255670Sdes
57255670Sdes/*
58255670Sdes * Some defines to make it easier to keep the code in sync with upstream.
59255670Sdes * getopt opterr optind optopt optreset optarg are all in defines.h which is
60255670Sdes * pulled in by includes.h.
61255670Sdes */
62255670Sdes#define warnx		logit
63255670Sdes
64255670Sdes#if 0
65255670Sdes#include <err.h>
66255670Sdes#include <getopt.h>
67255670Sdes#endif
68255670Sdes#include <errno.h>
69255670Sdes#include <stdlib.h>
70255670Sdes#include <string.h>
71255670Sdes#include <stdarg.h>
72255670Sdes
73255670Sdes#include "log.h"
74255670Sdes
75255670Sdesint	opterr = 1;		/* if error message should be printed */
76255670Sdesint	optind = 1;		/* index into parent argv vector */
77255670Sdesint	optopt = '?';		/* character checked for validity */
78255670Sdesint	optreset;		/* reset getopt */
79255670Sdeschar    *optarg;		/* argument associated with option */
80255670Sdes
81255670Sdes#define PRINT_ERROR	((opterr) && (*options != ':'))
82255670Sdes
83255670Sdes#define FLAG_PERMUTE	0x01	/* permute non-options to the end of argv */
84255670Sdes#define FLAG_ALLARGS	0x02	/* treat non-options as args to option "-1" */
85255670Sdes#define FLAG_LONGONLY	0x04	/* operate as getopt_long_only */
86255670Sdes
87255670Sdes/* return values */
88255670Sdes#define	BADCH		(int)'?'
89255670Sdes#define	BADARG		((*options == ':') ? (int)':' : (int)'?')
90255670Sdes#define	INORDER 	(int)1
91255670Sdes
92255670Sdes#define	EMSG		""
93255670Sdes
94255670Sdesstatic int getopt_internal(int, char * const *, const char *,
95255670Sdes			   const struct option *, int *, int);
96255670Sdesstatic int parse_long_options(char * const *, const char *,
97255670Sdes			      const struct option *, int *, int);
98255670Sdesstatic int gcd(int, int);
99255670Sdesstatic void permute_args(int, int, int, char * const *);
100255670Sdes
101255670Sdesstatic char *place = EMSG; /* option letter processing */
102255670Sdes
103255670Sdes/* XXX: set optreset to 1 rather than these two */
104255670Sdesstatic int nonopt_start = -1; /* first non option argument (for permute) */
105255670Sdesstatic int nonopt_end = -1;   /* first option after non options (for permute) */
106255670Sdes
107255670Sdes/* Error messages */
108255670Sdesstatic const char recargchar[] = "option requires an argument -- %c";
109255670Sdesstatic const char recargstring[] = "option requires an argument -- %s";
110255670Sdesstatic const char ambig[] = "ambiguous option -- %.*s";
111255670Sdesstatic const char noarg[] = "option doesn't take an argument -- %.*s";
112255670Sdesstatic const char illoptchar[] = "unknown option -- %c";
113255670Sdesstatic const char illoptstring[] = "unknown option -- %s";
114255670Sdes
115255670Sdes/*
116255670Sdes * Compute the greatest common divisor of a and b.
117255670Sdes */
118255670Sdesstatic int
119255670Sdesgcd(int a, int b)
120255670Sdes{
121255670Sdes	int c;
122255670Sdes
123255670Sdes	c = a % b;
124255670Sdes	while (c != 0) {
125255670Sdes		a = b;
126255670Sdes		b = c;
127255670Sdes		c = a % b;
128255670Sdes	}
129255670Sdes
130255670Sdes	return (b);
131255670Sdes}
132255670Sdes
133255670Sdes/*
134255670Sdes * Exchange the block from nonopt_start to nonopt_end with the block
135255670Sdes * from nonopt_end to opt_end (keeping the same order of arguments
136255670Sdes * in each block).
137255670Sdes */
138255670Sdesstatic void
139255670Sdespermute_args(int panonopt_start, int panonopt_end, int opt_end,
140255670Sdes	char * const *nargv)
141255670Sdes{
142255670Sdes	int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
143255670Sdes	char *swap;
144255670Sdes
145255670Sdes	/*
146255670Sdes	 * compute lengths of blocks and number and size of cycles
147255670Sdes	 */
148255670Sdes	nnonopts = panonopt_end - panonopt_start;
149255670Sdes	nopts = opt_end - panonopt_end;
150255670Sdes	ncycle = gcd(nnonopts, nopts);
151255670Sdes	cyclelen = (opt_end - panonopt_start) / ncycle;
152255670Sdes
153255670Sdes	for (i = 0; i < ncycle; i++) {
154255670Sdes		cstart = panonopt_end+i;
155255670Sdes		pos = cstart;
156255670Sdes		for (j = 0; j < cyclelen; j++) {
157255670Sdes			if (pos >= panonopt_end)
158255670Sdes				pos -= nnonopts;
159255670Sdes			else
160255670Sdes				pos += nopts;
161255670Sdes			swap = nargv[pos];
162255670Sdes			/* LINTED const cast */
163255670Sdes			((char **) nargv)[pos] = nargv[cstart];
164255670Sdes			/* LINTED const cast */
165255670Sdes			((char **)nargv)[cstart] = swap;
166255670Sdes		}
167255670Sdes	}
168255670Sdes}
169255670Sdes
170255670Sdes/*
171255670Sdes * parse_long_options --
172255670Sdes *	Parse long options in argc/argv argument vector.
173255670Sdes * Returns -1 if short_too is set and the option does not match long_options.
174255670Sdes */
175255670Sdesstatic int
176255670Sdesparse_long_options(char * const *nargv, const char *options,
177255670Sdes	const struct option *long_options, int *idx, int short_too)
178255670Sdes{
179255670Sdes	char *current_argv, *has_equal;
180255670Sdes	size_t current_argv_len;
181255670Sdes	int i, match;
182255670Sdes
183255670Sdes	current_argv = place;
184255670Sdes	match = -1;
185255670Sdes
186255670Sdes	optind++;
187255670Sdes
188255670Sdes	if ((has_equal = strchr(current_argv, '=')) != NULL) {
189255670Sdes		/* argument found (--option=arg) */
190255670Sdes		current_argv_len = has_equal - current_argv;
191255670Sdes		has_equal++;
192255670Sdes	} else
193255670Sdes		current_argv_len = strlen(current_argv);
194255670Sdes
195255670Sdes	for (i = 0; long_options[i].name; i++) {
196255670Sdes		/* find matching long option */
197255670Sdes		if (strncmp(current_argv, long_options[i].name,
198255670Sdes		    current_argv_len))
199255670Sdes			continue;
200255670Sdes
201255670Sdes		if (strlen(long_options[i].name) == current_argv_len) {
202255670Sdes			/* exact match */
203255670Sdes			match = i;
204255670Sdes			break;
205255670Sdes		}
206255670Sdes		/*
207255670Sdes		 * If this is a known short option, don't allow
208255670Sdes		 * a partial match of a single character.
209255670Sdes		 */
210255670Sdes		if (short_too && current_argv_len == 1)
211255670Sdes			continue;
212255670Sdes
213255670Sdes		if (match == -1)	/* partial match */
214255670Sdes			match = i;
215255670Sdes		else {
216255670Sdes			/* ambiguous abbreviation */
217255670Sdes			if (PRINT_ERROR)
218255670Sdes				warnx(ambig, (int)current_argv_len,
219255670Sdes				     current_argv);
220255670Sdes			optopt = 0;
221255670Sdes			return (BADCH);
222255670Sdes		}
223255670Sdes	}
224255670Sdes	if (match != -1) {		/* option found */
225255670Sdes		if (long_options[match].has_arg == no_argument
226255670Sdes		    && has_equal) {
227255670Sdes			if (PRINT_ERROR)
228255670Sdes				warnx(noarg, (int)current_argv_len,
229255670Sdes				     current_argv);
230255670Sdes			/*
231255670Sdes			 * XXX: GNU sets optopt to val regardless of flag
232255670Sdes			 */
233255670Sdes			if (long_options[match].flag == NULL)
234255670Sdes				optopt = long_options[match].val;
235255670Sdes			else
236255670Sdes				optopt = 0;
237255670Sdes			return (BADARG);
238255670Sdes		}
239255670Sdes		if (long_options[match].has_arg == required_argument ||
240255670Sdes		    long_options[match].has_arg == optional_argument) {
241255670Sdes			if (has_equal)
242255670Sdes				optarg = has_equal;
243255670Sdes			else if (long_options[match].has_arg ==
244255670Sdes			    required_argument) {
245255670Sdes				/*
246255670Sdes				 * optional argument doesn't use next nargv
247255670Sdes				 */
248255670Sdes				optarg = nargv[optind++];
249255670Sdes			}
250255670Sdes		}
251255670Sdes		if ((long_options[match].has_arg == required_argument)
252255670Sdes		    && (optarg == NULL)) {
253255670Sdes			/*
254255670Sdes			 * Missing argument; leading ':' indicates no error
255255670Sdes			 * should be generated.
256255670Sdes			 */
257255670Sdes			if (PRINT_ERROR)
258255670Sdes				warnx(recargstring,
259255670Sdes				    current_argv);
260255670Sdes			/*
261255670Sdes			 * XXX: GNU sets optopt to val regardless of flag
262255670Sdes			 */
263255670Sdes			if (long_options[match].flag == NULL)
264255670Sdes				optopt = long_options[match].val;
265255670Sdes			else
266255670Sdes				optopt = 0;
267255670Sdes			--optind;
268255670Sdes			return (BADARG);
269255670Sdes		}
270255670Sdes	} else {			/* unknown option */
271255670Sdes		if (short_too) {
272255670Sdes			--optind;
273255670Sdes			return (-1);
274255670Sdes		}
275255670Sdes		if (PRINT_ERROR)
276255670Sdes			warnx(illoptstring, current_argv);
277255670Sdes		optopt = 0;
278255670Sdes		return (BADCH);
279255670Sdes	}
280255670Sdes	if (idx)
281255670Sdes		*idx = match;
282255670Sdes	if (long_options[match].flag) {
283255670Sdes		*long_options[match].flag = long_options[match].val;
284255670Sdes		return (0);
285255670Sdes	} else
286255670Sdes		return (long_options[match].val);
287255670Sdes}
288255670Sdes
289255670Sdes/*
290255670Sdes * getopt_internal --
291255670Sdes *	Parse argc/argv argument vector.  Called by user level routines.
292255670Sdes */
293255670Sdesstatic int
294255670Sdesgetopt_internal(int nargc, char * const *nargv, const char *options,
295255670Sdes	const struct option *long_options, int *idx, int flags)
296255670Sdes{
297255670Sdes	char *oli;				/* option letter list index */
298255670Sdes	int optchar, short_too;
299255670Sdes	static int posixly_correct = -1;
300255670Sdes
301255670Sdes	if (options == NULL)
302255670Sdes		return (-1);
303255670Sdes
304255670Sdes	/*
305255670Sdes	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
306255670Sdes	 * XXX using optreset.  Work around this braindamage.
307255670Sdes	 */
308255670Sdes	if (optind == 0)
309255670Sdes		optind = optreset = 1;
310255670Sdes
311255670Sdes	/*
312255670Sdes	 * Disable GNU extensions if POSIXLY_CORRECT is set or options
313255670Sdes	 * string begins with a '+'.
314255670Sdes	 */
315255670Sdes	if (posixly_correct == -1 || optreset)
316255670Sdes		posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
317255670Sdes	if (*options == '-')
318255670Sdes		flags |= FLAG_ALLARGS;
319255670Sdes	else if (posixly_correct || *options == '+')
320255670Sdes		flags &= ~FLAG_PERMUTE;
321255670Sdes	if (*options == '+' || *options == '-')
322255670Sdes		options++;
323255670Sdes
324255670Sdes	optarg = NULL;
325255670Sdes	if (optreset)
326255670Sdes		nonopt_start = nonopt_end = -1;
327255670Sdesstart:
328255670Sdes	if (optreset || !*place) {		/* update scanning pointer */
329255670Sdes		optreset = 0;
330255670Sdes		if (optind >= nargc) {          /* end of argument vector */
331255670Sdes			place = EMSG;
332255670Sdes			if (nonopt_end != -1) {
333255670Sdes				/* do permutation, if we have to */
334255670Sdes				permute_args(nonopt_start, nonopt_end,
335255670Sdes				    optind, nargv);
336255670Sdes				optind -= nonopt_end - nonopt_start;
337255670Sdes			}
338255670Sdes			else if (nonopt_start != -1) {
339255670Sdes				/*
340255670Sdes				 * If we skipped non-options, set optind
341255670Sdes				 * to the first of them.
342255670Sdes				 */
343255670Sdes				optind = nonopt_start;
344255670Sdes			}
345255670Sdes			nonopt_start = nonopt_end = -1;
346255670Sdes			return (-1);
347255670Sdes		}
348255670Sdes		if (*(place = nargv[optind]) != '-' ||
349255670Sdes		    (place[1] == '\0' && strchr(options, '-') == NULL)) {
350255670Sdes			place = EMSG;		/* found non-option */
351255670Sdes			if (flags & FLAG_ALLARGS) {
352255670Sdes				/*
353255670Sdes				 * GNU extension:
354255670Sdes				 * return non-option as argument to option 1
355255670Sdes				 */
356255670Sdes				optarg = nargv[optind++];
357255670Sdes				return (INORDER);
358255670Sdes			}
359255670Sdes			if (!(flags & FLAG_PERMUTE)) {
360255670Sdes				/*
361255670Sdes				 * If no permutation wanted, stop parsing
362255670Sdes				 * at first non-option.
363255670Sdes				 */
364255670Sdes				return (-1);
365255670Sdes			}
366255670Sdes			/* do permutation */
367255670Sdes			if (nonopt_start == -1)
368255670Sdes				nonopt_start = optind;
369255670Sdes			else if (nonopt_end != -1) {
370255670Sdes				permute_args(nonopt_start, nonopt_end,
371255670Sdes				    optind, nargv);
372255670Sdes				nonopt_start = optind -
373255670Sdes				    (nonopt_end - nonopt_start);
374255670Sdes				nonopt_end = -1;
375255670Sdes			}
376255670Sdes			optind++;
377255670Sdes			/* process next argument */
378255670Sdes			goto start;
379255670Sdes		}
380255670Sdes		if (nonopt_start != -1 && nonopt_end == -1)
381255670Sdes			nonopt_end = optind;
382255670Sdes
383255670Sdes		/*
384255670Sdes		 * If we have "-" do nothing, if "--" we are done.
385255670Sdes		 */
386255670Sdes		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
387255670Sdes			optind++;
388255670Sdes			place = EMSG;
389255670Sdes			/*
390255670Sdes			 * We found an option (--), so if we skipped
391255670Sdes			 * non-options, we have to permute.
392255670Sdes			 */
393255670Sdes			if (nonopt_end != -1) {
394255670Sdes				permute_args(nonopt_start, nonopt_end,
395255670Sdes				    optind, nargv);
396255670Sdes				optind -= nonopt_end - nonopt_start;
397255670Sdes			}
398255670Sdes			nonopt_start = nonopt_end = -1;
399255670Sdes			return (-1);
400255670Sdes		}
401255670Sdes	}
402255670Sdes
403255670Sdes	/*
404255670Sdes	 * Check long options if:
405255670Sdes	 *  1) we were passed some
406255670Sdes	 *  2) the arg is not just "-"
407255670Sdes	 *  3) either the arg starts with -- we are getopt_long_only()
408255670Sdes	 */
409255670Sdes	if (long_options != NULL && place != nargv[optind] &&
410255670Sdes	    (*place == '-' || (flags & FLAG_LONGONLY))) {
411255670Sdes		short_too = 0;
412255670Sdes		if (*place == '-')
413255670Sdes			place++;		/* --foo long option */
414255670Sdes		else if (*place != ':' && strchr(options, *place) != NULL)
415255670Sdes			short_too = 1;		/* could be short option too */
416255670Sdes
417255670Sdes		optchar = parse_long_options(nargv, options, long_options,
418255670Sdes		    idx, short_too);
419255670Sdes		if (optchar != -1) {
420255670Sdes			place = EMSG;
421255670Sdes			return (optchar);
422255670Sdes		}
423255670Sdes	}
424255670Sdes
425255670Sdes	if ((optchar = (int)*place++) == (int)':' ||
426255670Sdes	    (optchar == (int)'-' && *place != '\0') ||
427255670Sdes	    (oli = strchr(options, optchar)) == NULL) {
428255670Sdes		/*
429255670Sdes		 * If the user specified "-" and  '-' isn't listed in
430255670Sdes		 * options, return -1 (non-option) as per POSIX.
431255670Sdes		 * Otherwise, it is an unknown option character (or ':').
432255670Sdes		 */
433255670Sdes		if (optchar == (int)'-' && *place == '\0')
434255670Sdes			return (-1);
435255670Sdes		if (!*place)
436255670Sdes			++optind;
437255670Sdes		if (PRINT_ERROR)
438255670Sdes			warnx(illoptchar, optchar);
439255670Sdes		optopt = optchar;
440255670Sdes		return (BADCH);
441255670Sdes	}
442255670Sdes	if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
443255670Sdes		/* -W long-option */
444255670Sdes		if (*place)			/* no space */
445255670Sdes			/* NOTHING */;
446255670Sdes		else if (++optind >= nargc) {	/* no arg */
447255670Sdes			place = EMSG;
448255670Sdes			if (PRINT_ERROR)
449255670Sdes				warnx(recargchar, optchar);
450255670Sdes			optopt = optchar;
451255670Sdes			return (BADARG);
452255670Sdes		} else				/* white space */
453255670Sdes			place = nargv[optind];
454255670Sdes		optchar = parse_long_options(nargv, options, long_options,
455255670Sdes		    idx, 0);
456255670Sdes		place = EMSG;
457255670Sdes		return (optchar);
458255670Sdes	}
459255670Sdes	if (*++oli != ':') {			/* doesn't take argument */
460255670Sdes		if (!*place)
461255670Sdes			++optind;
462255670Sdes	} else {				/* takes (optional) argument */
463255670Sdes		optarg = NULL;
464255670Sdes		if (*place)			/* no white space */
465255670Sdes			optarg = place;
466255670Sdes		else if (oli[1] != ':') {	/* arg not optional */
467255670Sdes			if (++optind >= nargc) {	/* no arg */
468255670Sdes				place = EMSG;
469255670Sdes				if (PRINT_ERROR)
470255670Sdes					warnx(recargchar, optchar);
471255670Sdes				optopt = optchar;
472255670Sdes				return (BADARG);
473255670Sdes			} else
474255670Sdes				optarg = nargv[optind];
475255670Sdes		}
476255670Sdes		place = EMSG;
477255670Sdes		++optind;
478255670Sdes	}
479255670Sdes	/* dump back option letter */
480255670Sdes	return (optchar);
481255670Sdes}
482255670Sdes
483255670Sdes/*
484255670Sdes * getopt --
485255670Sdes *	Parse argc/argv argument vector.
486255670Sdes *
487255670Sdes * [eventually this will replace the BSD getopt]
488255670Sdes */
489255670Sdesint
490255670Sdesgetopt(int nargc, char * const *nargv, const char *options)
491255670Sdes{
492255670Sdes
493255670Sdes	/*
494255670Sdes	 * We don't pass FLAG_PERMUTE to getopt_internal() since
495255670Sdes	 * the BSD getopt(3) (unlike GNU) has never done this.
496255670Sdes	 *
497255670Sdes	 * Furthermore, since many privileged programs call getopt()
498255670Sdes	 * before dropping privileges it makes sense to keep things
499255670Sdes	 * as simple (and bug-free) as possible.
500255670Sdes	 */
501255670Sdes	return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
502255670Sdes}
503255670Sdes
504255670Sdes#if 0
505255670Sdes/*
506255670Sdes * getopt_long --
507255670Sdes *	Parse argc/argv argument vector.
508255670Sdes */
509255670Sdesint
510255670Sdesgetopt_long(int nargc, char * const *nargv, const char *options,
511255670Sdes    const struct option *long_options, int *idx)
512255670Sdes{
513255670Sdes
514255670Sdes	return (getopt_internal(nargc, nargv, options, long_options, idx,
515255670Sdes	    FLAG_PERMUTE));
516255670Sdes}
517255670Sdes
518255670Sdes/*
519255670Sdes * getopt_long_only --
520255670Sdes *	Parse argc/argv argument vector.
521255670Sdes */
522255670Sdesint
523255670Sdesgetopt_long_only(int nargc, char * const *nargv, const char *options,
524255670Sdes    const struct option *long_options, int *idx)
525255670Sdes{
526255670Sdes
527255670Sdes	return (getopt_internal(nargc, nargv, options, long_options, idx,
528255670Sdes	    FLAG_PERMUTE|FLAG_LONGONLY));
529255670Sdes}
530255670Sdes#endif
531255670Sdes
532255670Sdes#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */
533