Blame


1 dd038bc6 2021-09-21 thomas.ad /*
2 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 1987, 1993, 1994
3 dd038bc6 2021-09-21 thomas.ad * The Regents of the University of California. All rights reserved.
4 dd038bc6 2021-09-21 thomas.ad *
5 dd038bc6 2021-09-21 thomas.ad * Redistribution and use in source and binary forms, with or without
6 dd038bc6 2021-09-21 thomas.ad * modification, are permitted provided that the following conditions
7 dd038bc6 2021-09-21 thomas.ad * are met:
8 dd038bc6 2021-09-21 thomas.ad * 1. Redistributions of source code must retain the above copyright
9 dd038bc6 2021-09-21 thomas.ad * notice, this list of conditions and the following disclaimer.
10 dd038bc6 2021-09-21 thomas.ad * 2. Redistributions in binary form must reproduce the above copyright
11 dd038bc6 2021-09-21 thomas.ad * notice, this list of conditions and the following disclaimer in the
12 dd038bc6 2021-09-21 thomas.ad * documentation and/or other materials provided with the distribution.
13 dd038bc6 2021-09-21 thomas.ad * 3. Neither the name of the University nor the names of its contributors
14 dd038bc6 2021-09-21 thomas.ad * may be used to endorse or promote products derived from this software
15 dd038bc6 2021-09-21 thomas.ad * without specific prior written permission.
16 dd038bc6 2021-09-21 thomas.ad *
17 dd038bc6 2021-09-21 thomas.ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 dd038bc6 2021-09-21 thomas.ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 dd038bc6 2021-09-21 thomas.ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 dd038bc6 2021-09-21 thomas.ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 dd038bc6 2021-09-21 thomas.ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 dd038bc6 2021-09-21 thomas.ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 dd038bc6 2021-09-21 thomas.ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 dd038bc6 2021-09-21 thomas.ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 dd038bc6 2021-09-21 thomas.ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 dd038bc6 2021-09-21 thomas.ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 dd038bc6 2021-09-21 thomas.ad * SUCH DAMAGE.
28 dd038bc6 2021-09-21 thomas.ad */
29 dd038bc6 2021-09-21 thomas.ad
30 dd038bc6 2021-09-21 thomas.ad /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
31 dd038bc6 2021-09-21 thomas.ad
32 8d60d668 2023-03-16 thomas #include "got_compat.h"
33 8d60d668 2023-03-16 thomas
34 dd038bc6 2021-09-21 thomas.ad #include <stdio.h>
35 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
36 dd038bc6 2021-09-21 thomas.ad #include <string.h>
37 dd038bc6 2021-09-21 thomas.ad
38 dd038bc6 2021-09-21 thomas.ad int BSDopterr = 1, /* if error message should be printed */
39 dd038bc6 2021-09-21 thomas.ad BSDoptind = 1, /* index into parent argv vector */
40 dd038bc6 2021-09-21 thomas.ad BSDoptopt, /* character checked for validity */
41 dd038bc6 2021-09-21 thomas.ad BSDoptreset; /* reset getopt */
42 dd038bc6 2021-09-21 thomas.ad char *BSDoptarg; /* argument associated with option */
43 dd038bc6 2021-09-21 thomas.ad
44 dd038bc6 2021-09-21 thomas.ad #define BADCH (int)'?'
45 dd038bc6 2021-09-21 thomas.ad #define BADARG (int)':'
46 dd038bc6 2021-09-21 thomas.ad #define EMSG ""
47 dd038bc6 2021-09-21 thomas.ad
48 dd038bc6 2021-09-21 thomas.ad /*
49 dd038bc6 2021-09-21 thomas.ad * getopt --
50 dd038bc6 2021-09-21 thomas.ad * Parse argc/argv argument vector.
51 dd038bc6 2021-09-21 thomas.ad */
52 dd038bc6 2021-09-21 thomas.ad int
53 dd038bc6 2021-09-21 thomas.ad BSDgetopt(int nargc, char *const *nargv, const char *ostr)
54 dd038bc6 2021-09-21 thomas.ad {
55 dd038bc6 2021-09-21 thomas.ad static const char *place = EMSG; /* option letter processing */
56 dd038bc6 2021-09-21 thomas.ad char *oli; /* option letter list index */
57 dd038bc6 2021-09-21 thomas.ad
58 dd038bc6 2021-09-21 thomas.ad if (ostr == NULL)
59 dd038bc6 2021-09-21 thomas.ad return (-1);
60 dd038bc6 2021-09-21 thomas.ad
61 dd038bc6 2021-09-21 thomas.ad if (BSDoptreset || !*place) { /* update scanning pointer */
62 dd038bc6 2021-09-21 thomas.ad BSDoptreset = 0;
63 dd038bc6 2021-09-21 thomas.ad if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
64 dd038bc6 2021-09-21 thomas.ad place = EMSG;
65 dd038bc6 2021-09-21 thomas.ad return (-1);
66 dd038bc6 2021-09-21 thomas.ad }
67 dd038bc6 2021-09-21 thomas.ad if (place[1] && *++place == '-') { /* found "--" */
68 dd038bc6 2021-09-21 thomas.ad if (place[1])
69 dd038bc6 2021-09-21 thomas.ad return (BADCH);
70 dd038bc6 2021-09-21 thomas.ad ++BSDoptind;
71 dd038bc6 2021-09-21 thomas.ad place = EMSG;
72 dd038bc6 2021-09-21 thomas.ad return (-1);
73 dd038bc6 2021-09-21 thomas.ad }
74 dd038bc6 2021-09-21 thomas.ad } /* option letter okay? */
75 dd038bc6 2021-09-21 thomas.ad if ((BSDoptopt = (int)*place++) == (int)':' ||
76 dd038bc6 2021-09-21 thomas.ad !(oli = strchr(ostr, BSDoptopt))) {
77 dd038bc6 2021-09-21 thomas.ad /*
78 dd038bc6 2021-09-21 thomas.ad * if the user didn't specify '-' as an option,
79 dd038bc6 2021-09-21 thomas.ad * assume it means -1.
80 dd038bc6 2021-09-21 thomas.ad */
81 dd038bc6 2021-09-21 thomas.ad if (BSDoptopt == (int)'-')
82 dd038bc6 2021-09-21 thomas.ad return (-1);
83 dd038bc6 2021-09-21 thomas.ad if (!*place)
84 dd038bc6 2021-09-21 thomas.ad ++BSDoptind;
85 dd038bc6 2021-09-21 thomas.ad if (BSDopterr && *ostr != ':')
86 dd038bc6 2021-09-21 thomas.ad (void)fprintf(stderr,
87 dd038bc6 2021-09-21 thomas.ad "%s: unknown option -- %c\n", getprogname(),
88 dd038bc6 2021-09-21 thomas.ad BSDoptopt);
89 dd038bc6 2021-09-21 thomas.ad return (BADCH);
90 dd038bc6 2021-09-21 thomas.ad }
91 dd038bc6 2021-09-21 thomas.ad if (*++oli != ':') { /* don't need argument */
92 dd038bc6 2021-09-21 thomas.ad BSDoptarg = NULL;
93 dd038bc6 2021-09-21 thomas.ad if (!*place)
94 dd038bc6 2021-09-21 thomas.ad ++BSDoptind;
95 dd038bc6 2021-09-21 thomas.ad }
96 dd038bc6 2021-09-21 thomas.ad else { /* need an argument */
97 dd038bc6 2021-09-21 thomas.ad if (*place) /* no white space */
98 dd038bc6 2021-09-21 thomas.ad BSDoptarg = (char *)place;
99 dd038bc6 2021-09-21 thomas.ad else if (nargc <= ++BSDoptind) { /* no arg */
100 dd038bc6 2021-09-21 thomas.ad place = EMSG;
101 dd038bc6 2021-09-21 thomas.ad if (*ostr == ':')
102 dd038bc6 2021-09-21 thomas.ad return (BADARG);
103 dd038bc6 2021-09-21 thomas.ad if (BSDopterr)
104 dd038bc6 2021-09-21 thomas.ad (void)fprintf(stderr,
105 dd038bc6 2021-09-21 thomas.ad "%s: option requires an argument -- %c\n",
106 dd038bc6 2021-09-21 thomas.ad getprogname(), BSDoptopt);
107 dd038bc6 2021-09-21 thomas.ad return (BADCH);
108 dd038bc6 2021-09-21 thomas.ad }
109 dd038bc6 2021-09-21 thomas.ad else /* white space */
110 dd038bc6 2021-09-21 thomas.ad BSDoptarg = nargv[BSDoptind];
111 dd038bc6 2021-09-21 thomas.ad place = EMSG;
112 dd038bc6 2021-09-21 thomas.ad ++BSDoptind;
113 dd038bc6 2021-09-21 thomas.ad }
114 dd038bc6 2021-09-21 thomas.ad return (BSDoptopt); /* dump back option letter */
115 dd038bc6 2021-09-21 thomas.ad }