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