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