1*0025f5bbSschwarze /* $OpenBSD: getopt_long.c,v 1.32 2020/05/27 22:25:09 schwarze Exp $ */
247f6c619Smillert /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
347f6c619Smillert
470ad9c96Smillert /*
5bf198cc6Smillert * Copyright (c) 2002 Todd C. Miller <millert@openbsd.org>
670ad9c96Smillert *
706f01696Smillert * Permission to use, copy, modify, and distribute this software for any
806f01696Smillert * purpose with or without fee is hereby granted, provided that the above
906f01696Smillert * copyright notice and this permission notice appear in all copies.
1070ad9c96Smillert *
11328f1f07Smillert * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12328f1f07Smillert * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13328f1f07Smillert * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14328f1f07Smillert * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15328f1f07Smillert * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16328f1f07Smillert * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17328f1f07Smillert * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18328f1f07Smillert *
19328f1f07Smillert * Sponsored in part by the Defense Advanced Research Projects
20328f1f07Smillert * Agency (DARPA) and Air Force Research Laboratory, Air Force
21328f1f07Smillert * Materiel Command, USAF, under agreement number F39502-99-1-0512.
2270ad9c96Smillert */
2347f6c619Smillert /*-
2447f6c619Smillert * Copyright (c) 2000 The NetBSD Foundation, Inc.
2547f6c619Smillert * All rights reserved.
2647f6c619Smillert *
2747f6c619Smillert * This code is derived from software contributed to The NetBSD Foundation
2847f6c619Smillert * by Dieter Baron and Thomas Klausner.
2947f6c619Smillert *
3047f6c619Smillert * Redistribution and use in source and binary forms, with or without
3147f6c619Smillert * modification, are permitted provided that the following conditions
3247f6c619Smillert * are met:
3347f6c619Smillert * 1. Redistributions of source code must retain the above copyright
3447f6c619Smillert * notice, this list of conditions and the following disclaimer.
3547f6c619Smillert * 2. Redistributions in binary form must reproduce the above copyright
3647f6c619Smillert * notice, this list of conditions and the following disclaimer in the
3747f6c619Smillert * documentation and/or other materials provided with the distribution.
3847f6c619Smillert *
3947f6c619Smillert * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
4047f6c619Smillert * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4147f6c619Smillert * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4247f6c619Smillert * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
4347f6c619Smillert * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4447f6c619Smillert * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
4547f6c619Smillert * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
4647f6c619Smillert * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
4747f6c619Smillert * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4847f6c619Smillert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
4947f6c619Smillert * POSSIBILITY OF SUCH DAMAGE.
5047f6c619Smillert */
5147f6c619Smillert
5247f6c619Smillert #include <err.h>
5347f6c619Smillert #include <errno.h>
5447f6c619Smillert #include <getopt.h>
5547f6c619Smillert #include <stdlib.h>
5647f6c619Smillert #include <string.h>
5747f6c619Smillert
5847f6c619Smillert int opterr = 1; /* if error message should be printed */
5947f6c619Smillert int optind = 1; /* index into parent argv vector */
6047f6c619Smillert int optopt = '?'; /* character checked for validity */
6147f6c619Smillert int optreset; /* reset getopt */
6247f6c619Smillert char *optarg; /* argument associated with option */
6347f6c619Smillert
6414437dc7Sguenther #if 0
650efd2defSguenther /* DEF_* only work on initialized (non-COMMON) variables */
660efd2defSguenther DEF_WEAK(opterr);
670efd2defSguenther DEF_WEAK(optind);
680efd2defSguenther DEF_WEAK(optopt);
6914437dc7Sguenther #endif
700efd2defSguenther
7147f6c619Smillert #define PRINT_ERROR ((opterr) && (*options != ':'))
7247f6c619Smillert
73a4cca5d9Smillert #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
74a4cca5d9Smillert #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
75a4cca5d9Smillert #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
7647f6c619Smillert
7747f6c619Smillert /* return values */
7847f6c619Smillert #define BADCH (int)'?'
7947f6c619Smillert #define BADARG ((*options == ':') ? (int)':' : (int)'?')
8047f6c619Smillert #define INORDER (int)1
8147f6c619Smillert
8247f6c619Smillert #define EMSG ""
8347f6c619Smillert
84a4cca5d9Smillert static int getopt_internal(int, char * const *, const char *,
85a4cca5d9Smillert const struct option *, int *, int);
8670ad9c96Smillert static int parse_long_options(char * const *, const char *,
87009fd011Smillert const struct option *, int *, int, int);
8847f6c619Smillert static int gcd(int, int);
8947f6c619Smillert static void permute_args(int, int, int, char * const *);
9047f6c619Smillert
9147f6c619Smillert static char *place = EMSG; /* option letter processing */
9247f6c619Smillert
9347f6c619Smillert /* XXX: set optreset to 1 rather than these two */
9447f6c619Smillert static int nonopt_start = -1; /* first non option argument (for permute) */
9547f6c619Smillert static int nonopt_end = -1; /* first option after non options (for permute) */
9647f6c619Smillert
9747f6c619Smillert /* Error messages */
9847f6c619Smillert static const char recargchar[] = "option requires an argument -- %c";
9947f6c619Smillert static const char recargstring[] = "option requires an argument -- %s";
10047f6c619Smillert static const char ambig[] = "ambiguous option -- %.*s";
10147f6c619Smillert static const char noarg[] = "option doesn't take an argument -- %.*s";
10247f6c619Smillert static const char illoptchar[] = "unknown option -- %c";
10347f6c619Smillert static const char illoptstring[] = "unknown option -- %s";
10447f6c619Smillert
10547f6c619Smillert /*
10647f6c619Smillert * Compute the greatest common divisor of a and b.
10747f6c619Smillert */
10847f6c619Smillert static int
gcd(int a,int b)109a4cca5d9Smillert gcd(int a, int b)
11047f6c619Smillert {
11147f6c619Smillert int c;
11247f6c619Smillert
11347f6c619Smillert c = a % b;
11447f6c619Smillert while (c != 0) {
11547f6c619Smillert a = b;
11647f6c619Smillert b = c;
11747f6c619Smillert c = a % b;
11847f6c619Smillert }
11947f6c619Smillert
120a4cca5d9Smillert return (b);
12147f6c619Smillert }
12247f6c619Smillert
12347f6c619Smillert /*
12447f6c619Smillert * Exchange the block from nonopt_start to nonopt_end with the block
12547f6c619Smillert * from nonopt_end to opt_end (keeping the same order of arguments
12647f6c619Smillert * in each block).
12747f6c619Smillert */
12847f6c619Smillert static void
permute_args(int panonopt_start,int panonopt_end,int opt_end,char * const * nargv)129a4cca5d9Smillert permute_args(int panonopt_start, int panonopt_end, int opt_end,
130a4cca5d9Smillert char * const *nargv)
13147f6c619Smillert {
13247f6c619Smillert int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
13347f6c619Smillert char *swap;
13447f6c619Smillert
13547f6c619Smillert /*
13647f6c619Smillert * compute lengths of blocks and number and size of cycles
13747f6c619Smillert */
13847f6c619Smillert nnonopts = panonopt_end - panonopt_start;
13947f6c619Smillert nopts = opt_end - panonopt_end;
14047f6c619Smillert ncycle = gcd(nnonopts, nopts);
14147f6c619Smillert cyclelen = (opt_end - panonopt_start) / ncycle;
14247f6c619Smillert
14347f6c619Smillert for (i = 0; i < ncycle; i++) {
14447f6c619Smillert cstart = panonopt_end+i;
14547f6c619Smillert pos = cstart;
14647f6c619Smillert for (j = 0; j < cyclelen; j++) {
14747f6c619Smillert if (pos >= panonopt_end)
14847f6c619Smillert pos -= nnonopts;
14947f6c619Smillert else
15047f6c619Smillert pos += nopts;
15147f6c619Smillert swap = nargv[pos];
15247f6c619Smillert ((char **)nargv)[pos] = nargv[cstart];
15347f6c619Smillert ((char **)nargv)[cstart] = swap;
15447f6c619Smillert }
15547f6c619Smillert }
15647f6c619Smillert }
15747f6c619Smillert
15847f6c619Smillert /*
159a4cca5d9Smillert * parse_long_options --
160a4cca5d9Smillert * Parse long options in argc/argv argument vector.
1619f947fd3Smillert * Returns -1 if short_too is set and the option does not match long_options.
16247f6c619Smillert */
16347f6c619Smillert static int
parse_long_options(char * const * nargv,const char * options,const struct option * long_options,int * idx,int short_too,int flags)16470ad9c96Smillert parse_long_options(char * const *nargv, const char *options,
165009fd011Smillert const struct option *long_options, int *idx, int short_too, int flags)
166a4cca5d9Smillert {
167a4cca5d9Smillert char *current_argv, *has_equal;
168a4cca5d9Smillert size_t current_argv_len;
169009fd011Smillert int i, match, exact_match, second_partial_match;
170a4cca5d9Smillert
171a4cca5d9Smillert current_argv = place;
172a4cca5d9Smillert match = -1;
173009fd011Smillert exact_match = 0;
174009fd011Smillert second_partial_match = 0;
175a4cca5d9Smillert
176a4cca5d9Smillert optind++;
177a4cca5d9Smillert
178a4cca5d9Smillert if ((has_equal = strchr(current_argv, '=')) != NULL) {
179a4cca5d9Smillert /* argument found (--option=arg) */
180a4cca5d9Smillert current_argv_len = has_equal - current_argv;
181a4cca5d9Smillert has_equal++;
182a4cca5d9Smillert } else
183a4cca5d9Smillert current_argv_len = strlen(current_argv);
184a4cca5d9Smillert
185a4cca5d9Smillert for (i = 0; long_options[i].name; i++) {
186a4cca5d9Smillert /* find matching long option */
187a4cca5d9Smillert if (strncmp(current_argv, long_options[i].name,
188a4cca5d9Smillert current_argv_len))
189a4cca5d9Smillert continue;
190a4cca5d9Smillert
191a4cca5d9Smillert if (strlen(long_options[i].name) == current_argv_len) {
192a4cca5d9Smillert /* exact match */
193a4cca5d9Smillert match = i;
194009fd011Smillert exact_match = 1;
195a4cca5d9Smillert break;
196a4cca5d9Smillert }
197a4cca5d9Smillert /*
1989f947fd3Smillert * If this is a known short option, don't allow
1999f947fd3Smillert * a partial match of a single character.
200a4cca5d9Smillert */
2019f947fd3Smillert if (short_too && current_argv_len == 1)
202a4cca5d9Smillert continue;
203a4cca5d9Smillert
204009fd011Smillert if (match == -1) /* first partial match */
205a4cca5d9Smillert match = i;
206009fd011Smillert else if ((flags & FLAG_LONGONLY) ||
207009fd011Smillert long_options[i].has_arg != long_options[match].has_arg ||
208009fd011Smillert long_options[i].flag != long_options[match].flag ||
209009fd011Smillert long_options[i].val != long_options[match].val)
210009fd011Smillert second_partial_match = 1;
211009fd011Smillert }
212009fd011Smillert if (!exact_match && second_partial_match) {
213a4cca5d9Smillert /* ambiguous abbreviation */
214a4cca5d9Smillert if (PRINT_ERROR)
215009fd011Smillert warnx(ambig, (int)current_argv_len, current_argv);
216a4cca5d9Smillert optopt = 0;
217a4cca5d9Smillert return (BADCH);
218a4cca5d9Smillert }
219a4cca5d9Smillert if (match != -1) { /* option found */
220a4cca5d9Smillert if (long_options[match].has_arg == no_argument
221a4cca5d9Smillert && has_equal) {
222a4cca5d9Smillert if (PRINT_ERROR)
223a4cca5d9Smillert warnx(noarg, (int)current_argv_len,
224a4cca5d9Smillert current_argv);
225a4cca5d9Smillert /*
226a4cca5d9Smillert * XXX: GNU sets optopt to val regardless of flag
227a4cca5d9Smillert */
228a4cca5d9Smillert if (long_options[match].flag == NULL)
229a4cca5d9Smillert optopt = long_options[match].val;
230a4cca5d9Smillert else
231a4cca5d9Smillert optopt = 0;
232a4cca5d9Smillert return (BADARG);
233a4cca5d9Smillert }
234a4cca5d9Smillert if (long_options[match].has_arg == required_argument ||
235a4cca5d9Smillert long_options[match].has_arg == optional_argument) {
236a4cca5d9Smillert if (has_equal)
237a4cca5d9Smillert optarg = has_equal;
238a4cca5d9Smillert else if (long_options[match].has_arg ==
239a4cca5d9Smillert required_argument) {
240a4cca5d9Smillert /*
241a4cca5d9Smillert * optional argument doesn't use next nargv
242a4cca5d9Smillert */
243a4cca5d9Smillert optarg = nargv[optind++];
244a4cca5d9Smillert }
245a4cca5d9Smillert }
246a4cca5d9Smillert if ((long_options[match].has_arg == required_argument)
247a4cca5d9Smillert && (optarg == NULL)) {
248a4cca5d9Smillert /*
249a4cca5d9Smillert * Missing argument; leading ':' indicates no error
250a4cca5d9Smillert * should be generated.
251a4cca5d9Smillert */
252a4cca5d9Smillert if (PRINT_ERROR)
253a4cca5d9Smillert warnx(recargstring,
254a4cca5d9Smillert current_argv);
255a4cca5d9Smillert /*
256a4cca5d9Smillert * XXX: GNU sets optopt to val regardless of flag
257a4cca5d9Smillert */
258a4cca5d9Smillert if (long_options[match].flag == NULL)
259a4cca5d9Smillert optopt = long_options[match].val;
260a4cca5d9Smillert else
261a4cca5d9Smillert optopt = 0;
262a4cca5d9Smillert --optind;
263a4cca5d9Smillert return (BADARG);
264a4cca5d9Smillert }
265a4cca5d9Smillert } else { /* unknown option */
2669f947fd3Smillert if (short_too) {
267a4cca5d9Smillert --optind;
26870ad9c96Smillert return (-1);
269a4cca5d9Smillert }
270a4cca5d9Smillert if (PRINT_ERROR)
271a4cca5d9Smillert warnx(illoptstring, current_argv);
272a4cca5d9Smillert optopt = 0;
273a4cca5d9Smillert return (BADCH);
274a4cca5d9Smillert }
275a4cca5d9Smillert if (idx)
276a4cca5d9Smillert *idx = match;
277a4cca5d9Smillert if (long_options[match].flag) {
278a4cca5d9Smillert *long_options[match].flag = long_options[match].val;
279a4cca5d9Smillert return (0);
280a4cca5d9Smillert } else
281a4cca5d9Smillert return (long_options[match].val);
282a4cca5d9Smillert }
283a4cca5d9Smillert
284a4cca5d9Smillert /*
285a4cca5d9Smillert * getopt_internal --
286a4cca5d9Smillert * Parse argc/argv argument vector. Called by user level routines.
287a4cca5d9Smillert */
288a4cca5d9Smillert static int
getopt_internal(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx,int flags)289a4cca5d9Smillert getopt_internal(int nargc, char * const *nargv, const char *options,
290a4cca5d9Smillert const struct option *long_options, int *idx, int flags)
29147f6c619Smillert {
29247f6c619Smillert char *oli; /* option letter list index */
2939f947fd3Smillert int optchar, short_too;
294a4cca5d9Smillert static int posixly_correct = -1;
29547f6c619Smillert
2966777ee37Smillert if (options == NULL)
2976777ee37Smillert return (-1);
29847f6c619Smillert
29947f6c619Smillert /*
3009f947fd3Smillert * XXX Some GNU programs (like cvs) set optind to 0 instead of
3019f947fd3Smillert * XXX using optreset. Work around this braindamage.
30247f6c619Smillert */
3036f1bcab3Smillert if (optind == 0)
3046f1bcab3Smillert optind = optreset = 1;
30547f6c619Smillert
306ebb4ed69Sguenther /*
307ebb4ed69Sguenther * Disable GNU extensions if POSIXLY_CORRECT is set or options
308ebb4ed69Sguenther * string begins with a '+'.
309ebb4ed69Sguenther */
310ebb4ed69Sguenther if (posixly_correct == -1 || optreset)
311ebb4ed69Sguenther posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
312ebb4ed69Sguenther if (*options == '-')
313ebb4ed69Sguenther flags |= FLAG_ALLARGS;
314ebb4ed69Sguenther else if (posixly_correct || *options == '+')
315ebb4ed69Sguenther flags &= ~FLAG_PERMUTE;
316ebb4ed69Sguenther if (*options == '+' || *options == '-')
317ebb4ed69Sguenther options++;
318ebb4ed69Sguenther
3196777ee37Smillert optarg = NULL;
32047f6c619Smillert if (optreset)
32147f6c619Smillert nonopt_start = nonopt_end = -1;
32247f6c619Smillert start:
32347f6c619Smillert if (optreset || !*place) { /* update scanning pointer */
32447f6c619Smillert optreset = 0;
32547f6c619Smillert if (optind >= nargc) { /* end of argument vector */
32647f6c619Smillert place = EMSG;
32747f6c619Smillert if (nonopt_end != -1) {
32847f6c619Smillert /* do permutation, if we have to */
32947f6c619Smillert permute_args(nonopt_start, nonopt_end,
33047f6c619Smillert optind, nargv);
33147f6c619Smillert optind -= nonopt_end - nonopt_start;
33247f6c619Smillert }
33347f6c619Smillert else if (nonopt_start != -1) {
33447f6c619Smillert /*
33547f6c619Smillert * If we skipped non-options, set optind
33647f6c619Smillert * to the first of them.
33747f6c619Smillert */
33847f6c619Smillert optind = nonopt_start;
33947f6c619Smillert }
34047f6c619Smillert nonopt_start = nonopt_end = -1;
341a4cca5d9Smillert return (-1);
34247f6c619Smillert }
343ddcd2923Smillert if (*(place = nargv[optind]) != '-' ||
344ddcd2923Smillert (place[1] == '\0' && strchr(options, '-') == NULL)) {
345ddcd2923Smillert place = EMSG; /* found non-option */
34647f6c619Smillert if (flags & FLAG_ALLARGS) {
34747f6c619Smillert /*
34847f6c619Smillert * GNU extension:
34947f6c619Smillert * return non-option as argument to option 1
35047f6c619Smillert */
35147f6c619Smillert optarg = nargv[optind++];
352a4cca5d9Smillert return (INORDER);
35347f6c619Smillert }
35447f6c619Smillert if (!(flags & FLAG_PERMUTE)) {
35547f6c619Smillert /*
35647f6c619Smillert * If no permutation wanted, stop parsing
35747f6c619Smillert * at first non-option.
35847f6c619Smillert */
359a4cca5d9Smillert return (-1);
36047f6c619Smillert }
36147f6c619Smillert /* do permutation */
36247f6c619Smillert if (nonopt_start == -1)
36347f6c619Smillert nonopt_start = optind;
36447f6c619Smillert else if (nonopt_end != -1) {
36547f6c619Smillert permute_args(nonopt_start, nonopt_end,
36647f6c619Smillert optind, nargv);
36747f6c619Smillert nonopt_start = optind -
36847f6c619Smillert (nonopt_end - nonopt_start);
36947f6c619Smillert nonopt_end = -1;
37047f6c619Smillert }
37147f6c619Smillert optind++;
37247f6c619Smillert /* process next argument */
37347f6c619Smillert goto start;
37447f6c619Smillert }
37547f6c619Smillert if (nonopt_start != -1 && nonopt_end == -1)
37647f6c619Smillert nonopt_end = optind;
3779f947fd3Smillert
37846b52239Smillert /*
379359295a3Smillert * If we have "-" do nothing, if "--" we are done.
38046b52239Smillert */
381359295a3Smillert if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
382a4cca5d9Smillert optind++;
383a4cca5d9Smillert place = EMSG;
384a4cca5d9Smillert /*
385a4cca5d9Smillert * We found an option (--), so if we skipped
386a4cca5d9Smillert * non-options, we have to permute.
387a4cca5d9Smillert */
388a4cca5d9Smillert if (nonopt_end != -1) {
389a4cca5d9Smillert permute_args(nonopt_start, nonopt_end,
390a4cca5d9Smillert optind, nargv);
391a4cca5d9Smillert optind -= nonopt_end - nonopt_start;
392a4cca5d9Smillert }
393a4cca5d9Smillert nonopt_start = nonopt_end = -1;
394a4cca5d9Smillert return (-1);
395a4cca5d9Smillert }
39670ad9c96Smillert }
397a4cca5d9Smillert
39846b52239Smillert /*
39946b52239Smillert * Check long options if:
40046b52239Smillert * 1) we were passed some
40146b52239Smillert * 2) the arg is not just "-"
40246b52239Smillert * 3) either the arg starts with -- we are getopt_long_only()
40346b52239Smillert */
40446b52239Smillert if (long_options != NULL && place != nargv[optind] &&
4059f947fd3Smillert (*place == '-' || (flags & FLAG_LONGONLY))) {
4069f947fd3Smillert short_too = 0;
4079f947fd3Smillert if (*place == '-')
4089f947fd3Smillert place++; /* --foo long option */
409334a385aSmillert else if (*place != ':' && strchr(options, *place) != NULL)
4109f947fd3Smillert short_too = 1; /* could be short option too */
4119f947fd3Smillert
4129f947fd3Smillert optchar = parse_long_options(nargv, options, long_options,
413009fd011Smillert idx, short_too, flags);
41470ad9c96Smillert if (optchar != -1) {
415a4cca5d9Smillert place = EMSG;
416a4cca5d9Smillert return (optchar);
417a4cca5d9Smillert }
418a4cca5d9Smillert }
4199f947fd3Smillert
42047f6c619Smillert if ((optchar = (int)*place++) == (int)':' ||
42147f6c619Smillert (oli = strchr(options, optchar)) == NULL) {
4229f947fd3Smillert if (!*place)
42370ad9c96Smillert ++optind;
4249f947fd3Smillert if (PRINT_ERROR)
4259f947fd3Smillert warnx(illoptchar, optchar);
42647f6c619Smillert optopt = optchar;
427a4cca5d9Smillert return (BADCH);
42847f6c619Smillert }
429a4cca5d9Smillert if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
430a4cca5d9Smillert /* -W long-option */
4313106aed5Smillert if (*place) /* no space */
4323106aed5Smillert /* NOTHING */;
4333106aed5Smillert else if (++optind >= nargc) { /* no arg */
43447f6c619Smillert place = EMSG;
43547f6c619Smillert if (PRINT_ERROR)
43647f6c619Smillert warnx(recargchar, optchar);
43747f6c619Smillert optopt = optchar;
438a4cca5d9Smillert return (BADARG);
43947f6c619Smillert } else /* white space */
44047f6c619Smillert place = nargv[optind];
44170ad9c96Smillert optchar = parse_long_options(nargv, options, long_options,
442009fd011Smillert idx, 0, flags);
443a4cca5d9Smillert place = EMSG;
444a4cca5d9Smillert return (optchar);
44547f6c619Smillert }
44647f6c619Smillert if (*++oli != ':') { /* doesn't take argument */
44747f6c619Smillert if (!*place)
44847f6c619Smillert ++optind;
44947f6c619Smillert } else { /* takes (optional) argument */
45047f6c619Smillert optarg = NULL;
45147f6c619Smillert if (*place) /* no white space */
45247f6c619Smillert optarg = place;
45347f6c619Smillert else if (oli[1] != ':') { /* arg not optional */
45447f6c619Smillert if (++optind >= nargc) { /* no arg */
45547f6c619Smillert place = EMSG;
45647f6c619Smillert if (PRINT_ERROR)
45747f6c619Smillert warnx(recargchar, optchar);
45847f6c619Smillert optopt = optchar;
459a4cca5d9Smillert return (BADARG);
46047f6c619Smillert } else
46147f6c619Smillert optarg = nargv[optind];
46247f6c619Smillert }
46347f6c619Smillert place = EMSG;
46447f6c619Smillert ++optind;
46547f6c619Smillert }
46647f6c619Smillert /* dump back option letter */
467a4cca5d9Smillert return (optchar);
46847f6c619Smillert }
46947f6c619Smillert
47047f6c619Smillert /*
47147f6c619Smillert * getopt --
47247f6c619Smillert * Parse argc/argv argument vector.
47347f6c619Smillert */
47447f6c619Smillert int
getopt(int nargc,char * const * nargv,const char * options)475a4cca5d9Smillert getopt(int nargc, char * const *nargv, const char *options)
47647f6c619Smillert {
47747f6c619Smillert
47847f6c619Smillert /*
479d2b86fc4Sjmc * We don't pass FLAG_PERMUTE to getopt_internal() since
480a4cca5d9Smillert * the BSD getopt(3) (unlike GNU) has never done this.
481a4cca5d9Smillert *
482a4cca5d9Smillert * Furthermore, since many privileged programs call getopt()
483a4cca5d9Smillert * before dropping privileges it makes sense to keep things
484a4cca5d9Smillert * as simple (and bug-free) as possible.
48547f6c619Smillert */
486a4cca5d9Smillert return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
48747f6c619Smillert }
48847f6c619Smillert
48947f6c619Smillert /*
49047f6c619Smillert * getopt_long --
49147f6c619Smillert * Parse argc/argv argument vector.
49247f6c619Smillert */
49347f6c619Smillert int
getopt_long(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx)494d8bc04e4Spat getopt_long(int nargc, char * const *nargv, const char *options,
495d8bc04e4Spat const struct option *long_options, int *idx)
49647f6c619Smillert {
49747f6c619Smillert
498a4cca5d9Smillert return (getopt_internal(nargc, nargv, options, long_options, idx,
499a4cca5d9Smillert FLAG_PERMUTE));
50047f6c619Smillert }
50147f6c619Smillert
50247f6c619Smillert /*
50347f6c619Smillert * getopt_long_only --
50447f6c619Smillert * Parse argc/argv argument vector.
50547f6c619Smillert */
50647f6c619Smillert int
getopt_long_only(int nargc,char * const * nargv,const char * options,const struct option * long_options,int * idx)507d8bc04e4Spat getopt_long_only(int nargc, char * const *nargv, const char *options,
508d8bc04e4Spat const struct option *long_options, int *idx)
50947f6c619Smillert {
51047f6c619Smillert
511a4cca5d9Smillert return (getopt_internal(nargc, nargv, options, long_options, idx,
512a4cca5d9Smillert FLAG_PERMUTE|FLAG_LONGONLY));
51347f6c619Smillert }
514