xref: /csrg-svn/lib/libc/stdlib/getopt.c (revision 66624)
121397Smckusick /*
2*66624Sbostic  * 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*66624Sbostic static char sccsid[] = "@(#)getopt.c	8.2 (Berkeley) 04/02/94";
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 
26*66624Sbostic /*
27*66624Sbostic  * getopt --
28*66624Sbostic  *	Parse argc/argv argument vector.
29*66624Sbostic  */
3046574Sdonn int
3132111Sbostic getopt(nargc, nargv, ostr)
3234017Sbostic 	int nargc;
3346574Sdonn 	char * const *nargv;
3446574Sdonn 	const char *ostr;
3521397Smckusick {
36*66624Sbostic 	extern char *__progname;
3734017Sbostic 	static char *place = EMSG;		/* option letter processing */
38*66624Sbostic 	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*66624Sbostic 			return (EOF);
4540166Sbostic 		}
4634017Sbostic 		if (place[1] && *++place == '-') {	/* found "--" */
4721397Smckusick 			++optind;
4840166Sbostic 			place = EMSG;
49*66624Sbostic 			return (EOF);
5021397Smckusick 		}
5132111Sbostic 	}					/* option letter okay? */
5232111Sbostic 	if ((optopt = (int)*place++) == (int)':' ||
53*66624Sbostic 	    !(oli = strchr(ostr, optopt))) {
5439712Sbostic 		/*
5539712Sbostic 		 * if the user didn't specify '-' as an option,
5639712Sbostic 		 * assume it means EOF.
5739712Sbostic 		 */
5839712Sbostic 		if (optopt == (int)'-')
59*66624Sbostic 			return (EOF);
6032111Sbostic 		if (!*place)
6132111Sbostic 			++optind;
62*66624Sbostic 		if (opterr && *ostr != ':')
63*66624Sbostic 			(void)fprintf(stderr,
64*66624Sbostic 			    "%s: illegal option -- %c\n", __progname, optopt);
65*66624Sbostic 		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 == ':')
78*66624Sbostic 				return (BADARG);
7936960Sbostic 			if (opterr)
8036960Sbostic 				(void)fprintf(stderr,
8136960Sbostic 				    "%s: option requires an argument -- %c\n",
82*66624Sbostic 				    __progname, optopt);
83*66624Sbostic 			return (BADCH);
8421397Smckusick 		}
8532111Sbostic 	 	else				/* white space */
8632111Sbostic 			optarg = nargv[optind];
8721397Smckusick 		place = EMSG;
8821397Smckusick 		++optind;
8921397Smckusick 	}
90*66624Sbostic 	return (optopt);			/* dump back option letter */
9121397Smckusick }
92