xref: /csrg-svn/lib/libc/stdlib/getopt.c (revision 68918)
121397Smckusick /*
266624Sbostic  * Copyright (c) 1987, 1993, 1994
361180Sbostic  *	The Regents of the University of California.  All rights reserved.
432677Sbostic  *
542635Sbostic  * %sccs.include.redist.c%
621397Smckusick  */
721397Smckusick 
826524Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*68918Sbostic static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 04/27/95";
1034017Sbostic #endif /* LIBC_SCCS and not lint */
1121397Smckusick 
1221397Smckusick #include <stdio.h>
1346574Sdonn #include <stdlib.h>
1446574Sdonn #include <string.h>
1521397Smckusick 
1625264Smckusick int	opterr = 1,		/* if error message should be printed */
1721397Smckusick 	optind = 1,		/* index into parent argv vector */
1854707Smarc 	optopt,			/* character checked for validity */
1954707Smarc 	optreset;		/* reset getopt */
2021397Smckusick char	*optarg;		/* argument associated with option */
2121397Smckusick 
2234017Sbostic #define	BADCH	(int)'?'
2354707Smarc #define	BADARG	(int)':'
2434017Sbostic #define	EMSG	""
2521397Smckusick 
2666624Sbostic /*
2766624Sbostic  * getopt --
2866624Sbostic  *	Parse argc/argv argument vector.
2966624Sbostic  */
3046574Sdonn int
getopt(nargc,nargv,ostr)3132111Sbostic getopt(nargc, nargv, ostr)
3234017Sbostic 	int nargc;
3346574Sdonn 	char * const *nargv;
3446574Sdonn 	const char *ostr;
3521397Smckusick {
3666624Sbostic 	extern char *__progname;
3734017Sbostic 	static char *place = EMSG;		/* option letter processing */
3866624Sbostic 	char *oli;				/* option letter list index */
3921397Smckusick 
4054707Smarc 	if (optreset || !*place) {		/* update scanning pointer */
4154707Smarc 		optreset = 0;
4240166Sbostic 		if (optind >= nargc || *(place = nargv[optind]) != '-') {
4340166Sbostic 			place = EMSG;
44*68918Sbostic 			return (-1);
4540166Sbostic 		}
4634017Sbostic 		if (place[1] && *++place == '-') {	/* found "--" */
4721397Smckusick 			++optind;
4840166Sbostic 			place = EMSG;
49*68918Sbostic 			return (-1);
5021397Smckusick 		}
5132111Sbostic 	}					/* option letter okay? */
5232111Sbostic 	if ((optopt = (int)*place++) == (int)':' ||
5366624Sbostic 	    !(oli = strchr(ostr, optopt))) {
5439712Sbostic 		/*
5539712Sbostic 		 * if the user didn't specify '-' as an option,
56*68918Sbostic 		 * assume it means -1.
5739712Sbostic 		 */
5839712Sbostic 		if (optopt == (int)'-')
59*68918Sbostic 			return (-1);
6032111Sbostic 		if (!*place)
6132111Sbostic 			++optind;
6266624Sbostic 		if (opterr && *ostr != ':')
6366624Sbostic 			(void)fprintf(stderr,
6466624Sbostic 			    "%s: illegal option -- %c\n", __progname, optopt);
6566624Sbostic 		return (BADCH);
6621397Smckusick 	}
6732111Sbostic 	if (*++oli != ':') {			/* don't need argument */
6821397Smckusick 		optarg = NULL;
6932111Sbostic 		if (!*place)
7032111Sbostic 			++optind;
7121397Smckusick 	}
7232111Sbostic 	else {					/* need an argument */
7332111Sbostic 		if (*place)			/* no white space */
7432111Sbostic 			optarg = place;
7521397Smckusick 		else if (nargc <= ++optind) {	/* no arg */
7621397Smckusick 			place = EMSG;
7754707Smarc 			if (*ostr == ':')
7866624Sbostic 				return (BADARG);
7936960Sbostic 			if (opterr)
8036960Sbostic 				(void)fprintf(stderr,
8136960Sbostic 				    "%s: option requires an argument -- %c\n",
8266624Sbostic 				    __progname, optopt);
8366624Sbostic 			return (BADCH);
8421397Smckusick 		}
8532111Sbostic 	 	else				/* white space */
8632111Sbostic 			optarg = nargv[optind];
8721397Smckusick 		place = EMSG;
8821397Smckusick 		++optind;
8921397Smckusick 	}
9066624Sbostic 	return (optopt);			/* dump back option letter */
9121397Smckusick }
92