1*b50261e2SCy Schubert /* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */
2*b50261e2SCy Schubert
3*b50261e2SCy Schubert /*
4*b50261e2SCy Schubert * Copyright (c) 1987, 1993, 1994, 1995
5*b50261e2SCy Schubert * The Regents of the University of California. All rights reserved.
6*b50261e2SCy Schubert *
7*b50261e2SCy Schubert * Redistribution and use in source and binary forms, with or without
8*b50261e2SCy Schubert * modification, are permitted provided that the following conditions
9*b50261e2SCy Schubert * are met:
10*b50261e2SCy Schubert * 1. Redistributions of source code must retain the above copyright
11*b50261e2SCy Schubert * notice, this list of conditions and the following disclaimer.
12*b50261e2SCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
13*b50261e2SCy Schubert * notice, this list of conditions and the following disclaimer in the
14*b50261e2SCy Schubert * documentation and/or other materials provided with the distribution.
15*b50261e2SCy Schubert * 3. Neither the names of the copyright holders nor the names of its
16*b50261e2SCy Schubert * contributors may be used to endorse or promote products derived from
17*b50261e2SCy Schubert * this software without specific prior written permission.
18*b50261e2SCy Schubert *
19*b50261e2SCy Schubert * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20*b50261e2SCy Schubert * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21*b50261e2SCy Schubert * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*b50261e2SCy Schubert * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23*b50261e2SCy Schubert * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*b50261e2SCy Schubert * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*b50261e2SCy Schubert * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*b50261e2SCy Schubert * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*b50261e2SCy Schubert * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*b50261e2SCy Schubert * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*b50261e2SCy Schubert * POSSIBILITY OF SUCH DAMAGE.
30*b50261e2SCy Schubert */
31*b50261e2SCy Schubert
32*b50261e2SCy Schubert #if 0
33*b50261e2SCy Schubert static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
34*b50261e2SCy Schubert #endif
35*b50261e2SCy Schubert
36*b50261e2SCy Schubert #include <assert.h>
37*b50261e2SCy Schubert #include <errno.h>
38*b50261e2SCy Schubert #include <stdio.h>
39*b50261e2SCy Schubert #include <string.h>
40*b50261e2SCy Schubert
41*b50261e2SCy Schubert #define __P(x) x
42*b50261e2SCy Schubert #define _DIAGASSERT(x) assert(x)
43*b50261e2SCy Schubert
44*b50261e2SCy Schubert #ifdef __weak_alias
45*b50261e2SCy Schubert __weak_alias(getopt,_getopt);
46*b50261e2SCy Schubert #endif
47*b50261e2SCy Schubert
48*b50261e2SCy Schubert
49*b50261e2SCy Schubert int opterr = 1, /* if error message should be printed */
50*b50261e2SCy Schubert optind = 1, /* index into parent argv vector */
51*b50261e2SCy Schubert optopt, /* character checked for validity */
52*b50261e2SCy Schubert optreset; /* reset getopt */
53*b50261e2SCy Schubert char *optarg; /* argument associated with option */
54*b50261e2SCy Schubert
55*b50261e2SCy Schubert static char * _progname __P((char *));
56*b50261e2SCy Schubert int getopt_internal __P((int, char * const *, const char *));
57*b50261e2SCy Schubert
58*b50261e2SCy Schubert static char *
_progname(nargv0)59*b50261e2SCy Schubert _progname(nargv0)
60*b50261e2SCy Schubert char * nargv0;
61*b50261e2SCy Schubert {
62*b50261e2SCy Schubert char * tmp;
63*b50261e2SCy Schubert
64*b50261e2SCy Schubert _DIAGASSERT(nargv0 != NULL);
65*b50261e2SCy Schubert
66*b50261e2SCy Schubert tmp = strrchr(nargv0, '/');
67*b50261e2SCy Schubert if (tmp)
68*b50261e2SCy Schubert tmp++;
69*b50261e2SCy Schubert else
70*b50261e2SCy Schubert tmp = nargv0;
71*b50261e2SCy Schubert return(tmp);
72*b50261e2SCy Schubert }
73*b50261e2SCy Schubert
74*b50261e2SCy Schubert #define BADCH (int)'?'
75*b50261e2SCy Schubert #define BADARG (int)':'
76*b50261e2SCy Schubert #define EMSG ""
77*b50261e2SCy Schubert
78*b50261e2SCy Schubert /*
79*b50261e2SCy Schubert * getopt --
80*b50261e2SCy Schubert * Parse argc/argv argument vector.
81*b50261e2SCy Schubert */
82*b50261e2SCy Schubert int
getopt(nargc,nargv,ostr)83*b50261e2SCy Schubert getopt(nargc, nargv, ostr)
84*b50261e2SCy Schubert int nargc;
85*b50261e2SCy Schubert char * const nargv[];
86*b50261e2SCy Schubert const char *ostr;
87*b50261e2SCy Schubert {
88*b50261e2SCy Schubert static char *__progname = 0;
89*b50261e2SCy Schubert static char *place = EMSG; /* option letter processing */
90*b50261e2SCy Schubert char *oli; /* option letter list index */
91*b50261e2SCy Schubert __progname = __progname?__progname:_progname(*nargv);
92*b50261e2SCy Schubert
93*b50261e2SCy Schubert _DIAGASSERT(nargv != NULL);
94*b50261e2SCy Schubert _DIAGASSERT(ostr != NULL);
95*b50261e2SCy Schubert
96*b50261e2SCy Schubert if (optreset || !*place) { /* update scanning pointer */
97*b50261e2SCy Schubert optreset = 0;
98*b50261e2SCy Schubert if (optind >= nargc || *(place = nargv[optind]) != '-') {
99*b50261e2SCy Schubert place = EMSG;
100*b50261e2SCy Schubert return (-1);
101*b50261e2SCy Schubert }
102*b50261e2SCy Schubert if (place[1] && *++place == '-' /* found "--" */
103*b50261e2SCy Schubert && place[1] == '\0') {
104*b50261e2SCy Schubert ++optind;
105*b50261e2SCy Schubert place = EMSG;
106*b50261e2SCy Schubert return (-1);
107*b50261e2SCy Schubert }
108*b50261e2SCy Schubert } /* option letter okay? */
109*b50261e2SCy Schubert if ((optopt = (int)*place++) == (int)':' ||
110*b50261e2SCy Schubert !(oli = strchr(ostr, optopt))) {
111*b50261e2SCy Schubert /*
112*b50261e2SCy Schubert * if the user didn't specify '-' as an option,
113*b50261e2SCy Schubert * assume it means -1.
114*b50261e2SCy Schubert */
115*b50261e2SCy Schubert if (optopt == (int)'-')
116*b50261e2SCy Schubert return (-1);
117*b50261e2SCy Schubert if (!*place)
118*b50261e2SCy Schubert ++optind;
119*b50261e2SCy Schubert if (opterr && *ostr != ':')
120*b50261e2SCy Schubert (void)fprintf(stderr,
121*b50261e2SCy Schubert "%s: illegal option -- %c\n", __progname, optopt);
122*b50261e2SCy Schubert return (BADCH);
123*b50261e2SCy Schubert }
124*b50261e2SCy Schubert if (*++oli != ':') { /* don't need argument */
125*b50261e2SCy Schubert optarg = NULL;
126*b50261e2SCy Schubert if (!*place)
127*b50261e2SCy Schubert ++optind;
128*b50261e2SCy Schubert }
129*b50261e2SCy Schubert else { /* need an argument */
130*b50261e2SCy Schubert if (*place) /* no white space */
131*b50261e2SCy Schubert optarg = place;
132*b50261e2SCy Schubert else if (nargc <= ++optind) { /* no arg */
133*b50261e2SCy Schubert place = EMSG;
134*b50261e2SCy Schubert if (*ostr == ':')
135*b50261e2SCy Schubert return (BADARG);
136*b50261e2SCy Schubert if (opterr)
137*b50261e2SCy Schubert (void)fprintf(stderr,
138*b50261e2SCy Schubert "%s: option requires an argument -- %c\n",
139*b50261e2SCy Schubert __progname, optopt);
140*b50261e2SCy Schubert return (BADCH);
141*b50261e2SCy Schubert }
142*b50261e2SCy Schubert else /* white space */
143*b50261e2SCy Schubert optarg = nargv[optind];
144*b50261e2SCy Schubert place = EMSG;
145*b50261e2SCy Schubert ++optind;
146*b50261e2SCy Schubert }
147*b50261e2SCy Schubert return (optopt); /* dump back option letter */
148*b50261e2SCy Schubert }
149*b50261e2SCy Schubert
150