3 * Copyright (c) 1987, 1993, 1994, 1996
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
19 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
37 extern int opterr; /* if error message should be printed */
38 extern int optind; /* index into parent argv vector */
39 extern int optopt; /* character checked for validity */
40 extern int optreset; /* reset getopt */
41 extern char *optarg; /* argument associated with option */
44 #define _DIAGASSERT(x) assert(x)
46 static char * __progname __P((char *));
47 int getopt_internal __P((int, char * const *, const char *));
55 _DIAGASSERT(nargv0 != NULL);
57 tmp = strrchr(nargv0, '/');
65 #define BADCH (int)'?'
66 #define BADARG (int)':'
71 * Parse argc/argv argument vector.
74 getopt_internal(nargc, nargv, ostr)
79 static char *place = EMSG; /* option letter processing */
80 char *oli; /* option letter list index */
82 _DIAGASSERT(nargv != NULL);
83 _DIAGASSERT(ostr != NULL);
85 if (optreset || !*place) { /* update scanning pointer */
87 if (optind >= nargc || *(place = nargv[optind]) != '-') {
91 if (place[1] && *++place == '-') { /* found "--" */
96 } /* option letter okay? */
97 if ((optopt = (int)*place++) == (int)':' ||
98 !(oli = strchr(ostr, optopt))) {
100 * if the user didn't specify '-' as an option,
101 * assume it means -1.
103 if (optopt == (int)'-')
107 if (opterr && *ostr != ':')
108 (void)fprintf(stderr,
109 "%s: illegal option -- %c\n", __progname(nargv[0]), optopt);
112 if (*++oli != ':') { /* don't need argument */
116 } else { /* need an argument */
117 if (*place) /* no white space */
119 else if (nargc <= ++optind) { /* no arg */
121 if ((opterr) && (*ostr != ':'))
122 (void)fprintf(stderr,
123 "%s: option requires an argument -- %c\n",
124 __progname(nargv[0]), optopt);
126 } else /* white space */
127 optarg = nargv[optind];
131 return (optopt); /* dump back option letter */
137 * Parse argc/argv argument vector.
140 getopt2(nargc, nargv, ostr)
147 if ((retval = getopt_internal(nargc, nargv, ostr)) == -2) {
157 * Parse argc/argv argument vector.
160 getopt_long(nargc, nargv, options, long_options, index)
163 const char * options;
164 const struct option * long_options;
169 _DIAGASSERT(nargv != NULL);
170 _DIAGASSERT(options != NULL);
171 _DIAGASSERT(long_options != NULL);
172 /* index may be NULL */
174 if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
175 char *current_argv = nargv[optind++] + 2, *has_equal;
177 size_t current_argv_len;
179 if (*current_argv == '\0') {
182 if ((has_equal = strchr(current_argv, '=')) != NULL) {
183 current_argv_len = has_equal - current_argv;
186 current_argv_len = strlen(current_argv);
188 for (i = 0; long_options[i].name; i++) {
189 if (strncmp(current_argv, long_options[i].name, current_argv_len))
192 if (strlen(long_options[i].name) == current_argv_len) {
200 if (long_options[match].has_arg == required_argument ||
201 long_options[match].has_arg == optional_argument) {
205 optarg = nargv[optind++];
207 if ((long_options[match].has_arg == required_argument)
208 && (optarg == NULL)) {
210 * Missing argument, leading :
211 * indicates no error should be generated
213 if ((opterr) && (*options != ':'))
214 (void)fprintf(stderr,
215 "%s: option requires an argument -- %s\n",
216 __progname(nargv[0]), current_argv);
219 } else { /* No matching argument */
220 if ((opterr) && (*options != ':'))
221 (void)fprintf(stderr,
222 "%s: illegal option -- %s\n", __progname(nargv[0]), current_argv);
225 if (long_options[match].flag) {
226 *long_options[match].flag = long_options[match].val;
229 retval = long_options[match].val;