xref: /csrg-svn/lib/libc/stdlib/getopt.c (revision 46574)
121397Smckusick /*
232677Sbostic  * Copyright (c) 1987 Regents of the University of California.
332677Sbostic  * All rights reserved.
432677Sbostic  *
542635Sbostic  * %sccs.include.redist.c%
621397Smckusick  */
721397Smckusick 
826524Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*46574Sdonn static char sccsid[] = "@(#)getopt.c	4.13 (Berkeley) 02/23/91";
1034017Sbostic #endif /* LIBC_SCCS and not lint */
1121397Smckusick 
1221397Smckusick #include <stdio.h>
13*46574Sdonn #include <stdlib.h>
14*46574Sdonn #include <string.h>
1521397Smckusick 
1621397Smckusick /*
1721397Smckusick  * get option letter from argument vector
1821397Smckusick  */
1925264Smckusick int	opterr = 1,		/* if error message should be printed */
2021397Smckusick 	optind = 1,		/* index into parent argv vector */
2121397Smckusick 	optopt;			/* character checked for validity */
2221397Smckusick char	*optarg;		/* argument associated with option */
2321397Smckusick 
2434017Sbostic #define	BADCH	(int)'?'
2534017Sbostic #define	EMSG	""
2621397Smckusick 
27*46574Sdonn int
2832111Sbostic getopt(nargc, nargv, ostr)
2934017Sbostic 	int nargc;
30*46574Sdonn 	char * const *nargv;
31*46574Sdonn 	const char *ostr;
3221397Smckusick {
3334017Sbostic 	static char *place = EMSG;		/* option letter processing */
3434017Sbostic 	register char *oli;			/* option letter list index */
35*46574Sdonn 	char *p;
3621397Smckusick 
3732111Sbostic 	if (!*place) {				/* update scanning pointer */
3840166Sbostic 		if (optind >= nargc || *(place = nargv[optind]) != '-') {
3940166Sbostic 			place = EMSG;
4032111Sbostic 			return(EOF);
4140166Sbostic 		}
4234017Sbostic 		if (place[1] && *++place == '-') {	/* found "--" */
4321397Smckusick 			++optind;
4440166Sbostic 			place = EMSG;
4521397Smckusick 			return(EOF);
4621397Smckusick 		}
4732111Sbostic 	}					/* option letter okay? */
4832111Sbostic 	if ((optopt = (int)*place++) == (int)':' ||
4932111Sbostic 	    !(oli = index(ostr, optopt))) {
5039712Sbostic 		/*
5139712Sbostic 		 * if the user didn't specify '-' as an option,
5239712Sbostic 		 * assume it means EOF.
5339712Sbostic 		 */
5439712Sbostic 		if (optopt == (int)'-')
5539712Sbostic 			return(EOF);
5632111Sbostic 		if (!*place)
5732111Sbostic 			++optind;
5838284Sbostic 		if (opterr) {
5938284Sbostic 			if (!(p = rindex(*nargv, '/')))
6038284Sbostic 				p = *nargv;
6138284Sbostic 			else
6238284Sbostic 				++p;
6336960Sbostic 			(void)fprintf(stderr, "%s: illegal option -- %c\n",
6438284Sbostic 			    p, optopt);
6538284Sbostic 		}
6636960Sbostic 		return(BADCH);
6721397Smckusick 	}
6832111Sbostic 	if (*++oli != ':') {			/* don't need argument */
6921397Smckusick 		optarg = NULL;
7032111Sbostic 		if (!*place)
7132111Sbostic 			++optind;
7221397Smckusick 	}
7332111Sbostic 	else {					/* need an argument */
7432111Sbostic 		if (*place)			/* no white space */
7532111Sbostic 			optarg = place;
7621397Smckusick 		else if (nargc <= ++optind) {	/* no arg */
7721397Smckusick 			place = EMSG;
7838284Sbostic 			if (!(p = rindex(*nargv, '/')))
7938284Sbostic 				p = *nargv;
8038284Sbostic 			else
8138284Sbostic 				++p;
8236960Sbostic 			if (opterr)
8336960Sbostic 				(void)fprintf(stderr,
8436960Sbostic 				    "%s: option requires an argument -- %c\n",
8538284Sbostic 				    p, optopt);
8636960Sbostic 			return(BADCH);
8721397Smckusick 		}
8832111Sbostic 	 	else				/* white space */
8932111Sbostic 			optarg = nargv[optind];
9021397Smckusick 		place = EMSG;
9121397Smckusick 		++optind;
9221397Smckusick 	}
9332111Sbostic 	return(optopt);				/* dump back option letter */
9421397Smckusick }
95