1 /* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */
\r
4 * Copyright (c) 1987, 1993, 1994
\r
5 * The Regents of the University of California. All rights reserved.
\r
7 * Redistribution and use in source and binary forms, with or without
\r
8 * modification, are permitted provided that the following conditions
\r
10 * 1. Redistributions of source code must retain the above copyright
\r
11 * notice, this list of conditions and the following disclaimer.
\r
12 * 2. Redistributions in binary form must reproduce the above copyright
\r
13 * notice, this list of conditions and the following disclaimer in the
\r
14 * documentation and/or other materials provided with the distribution.
\r
15 * 3. All advertising materials mentioning features or use of this software
\r
16 * must display the following acknowledgement:
\r
17 * This product includes software developed by the University of
\r
18 * California, Berkeley and its contributors.
\r
19 * 4. Neither the name of the University nor the names of its contributors
\r
20 * may be used to endorse or promote products derived from this software
\r
21 * without specific prior written permission.
\r
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
\r
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
\r
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
\r
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
\r
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
\r
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
\r
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
\r
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
\r
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
\r
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
\r
37 static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
\r
46 #define _DIAGASSERT(x) assert(x)
\r
49 __weak_alias(getopt,_getopt);
\r
53 int opterr = 1, /* if error message should be printed */
\r
54 optind = 1, /* index into parent argv vector */
\r
55 optopt, /* character checked for validity */
\r
56 optreset; /* reset getopt */
\r
57 char *optarg; /* argument associated with option */
\r
59 static char * _progname __P((char *));
\r
60 int getopt_internal __P((int, char * const *, const char *));
\r
68 _DIAGASSERT(nargv0 != NULL);
\r
70 tmp = strrchr(nargv0, '/');
\r
78 #define BADCH (int)'?'
\r
79 #define BADARG (int)':'
\r
84 * Parse argc/argv argument vector.
\r
87 getopt(nargc, nargv, ostr)
\r
89 char * const nargv[];
\r
92 static char *__progname = 0;
\r
93 static char *place = EMSG; /* option letter processing */
\r
94 char *oli; /* option letter list index */
\r
95 __progname = __progname?__progname:_progname(*nargv);
\r
97 _DIAGASSERT(nargv != NULL);
\r
98 _DIAGASSERT(ostr != NULL);
\r
100 if (optreset || !*place) { /* update scanning pointer */
\r
102 if (optind >= nargc || *(place = nargv[optind]) != '-') {
\r
106 if (place[1] && *++place == '-' /* found "--" */
\r
107 && place[1] == '\0') {
\r
112 } /* option letter okay? */
\r
113 if ((optopt = (int)*place++) == (int)':' ||
\r
114 !(oli = strchr(ostr, optopt))) {
\r
116 * if the user didn't specify '-' as an option,
\r
117 * assume it means -1.
\r
119 if (optopt == (int)'-')
\r
123 if (opterr && *ostr != ':')
\r
124 (void)fprintf(stderr,
\r
125 "%s: illegal option -- %c\n", __progname, optopt);
\r
128 if (*++oli != ':') { /* don't need argument */
\r
133 else { /* need an argument */
\r
134 if (*place) /* no white space */
\r
136 else if (nargc <= ++optind) { /* no arg */
\r
141 (void)fprintf(stderr,
\r
142 "%s: option requires an argument -- %c\n",
\r
143 __progname, optopt);
\r
146 else /* white space */
\r
147 optarg = nargv[optind];
\r
151 return (optopt); /* dump back option letter */
\r