xref: /csrg-svn/lib/libc/stdlib/getopt.c (revision 34017)
121397Smckusick /*
232677Sbostic  * Copyright (c) 1987 Regents of the University of California.
332677Sbostic  * All rights reserved.
432677Sbostic  *
532677Sbostic  * Redistribution and use in source and binary forms are permitted
632677Sbostic  * provided that this notice is preserved and that due credit is given
732677Sbostic  * to the University of California at Berkeley. The name of the University
832677Sbostic  * may not be used to endorse or promote products derived from this
932677Sbostic  * software without specific written prior permission. This software
1032677Sbostic  * is provided ``as is'' without express or implied warranty.
1121397Smckusick  */
1221397Smckusick 
1326524Sdonn #if defined(LIBC_SCCS) && !defined(lint)
14*34017Sbostic static char sccsid[] = "@(#)getopt.c	4.6 (Berkeley) 04/19/88";
15*34017Sbostic #endif /* LIBC_SCCS and not lint */
1621397Smckusick 
1721397Smckusick #include <stdio.h>
1821397Smckusick 
1921397Smckusick /*
2021397Smckusick  * get option letter from argument vector
2121397Smckusick  */
2225264Smckusick int	opterr = 1,		/* if error message should be printed */
2321397Smckusick 	optind = 1,		/* index into parent argv vector */
2421397Smckusick 	optopt;			/* character checked for validity */
2521397Smckusick char	*optarg;		/* argument associated with option */
2621397Smckusick 
27*34017Sbostic #define	BADCH	(int)'?'
28*34017Sbostic #define	EMSG	""
29*34017Sbostic #define	tell(s)	{ \
3032111Sbostic 	if (opterr) { \
3132111Sbostic 		fputs(*nargv, stderr); \
3232111Sbostic 		fputs(s, stderr); \
3332111Sbostic 		fputc(optopt, stderr); \
3432111Sbostic 		fputc((int)'\n', stderr); \
3532111Sbostic 	} \
3632111Sbostic 	return(BADCH); \
3732111Sbostic }
3821397Smckusick 
3932111Sbostic getopt(nargc, nargv, ostr)
40*34017Sbostic 	int nargc;
41*34017Sbostic 	char **nargv, *ostr;
4221397Smckusick {
43*34017Sbostic 	static char *place = EMSG;		/* option letter processing */
44*34017Sbostic 	register char *oli;			/* option letter list index */
45*34017Sbostic 	char *index();
4621397Smckusick 
4732111Sbostic 	if (!*place) {				/* update scanning pointer */
48*34017Sbostic 		if (optind >= nargc || *(place = nargv[optind]) != '-')
4932111Sbostic 			return(EOF);
50*34017Sbostic 		if (place[1] && *++place == '-') {	/* found "--" */
5121397Smckusick 			++optind;
5221397Smckusick 			return(EOF);
5321397Smckusick 		}
5432111Sbostic 	}					/* option letter okay? */
5532111Sbostic 	if ((optopt = (int)*place++) == (int)':' ||
5632111Sbostic 	    !(oli = index(ostr, optopt))) {
5732111Sbostic 		if (!*place)
5832111Sbostic 			++optind;
5921397Smckusick 		tell(": illegal option -- ");
6021397Smckusick 	}
6132111Sbostic 	if (*++oli != ':') {			/* don't need argument */
6221397Smckusick 		optarg = NULL;
6332111Sbostic 		if (!*place)
6432111Sbostic 			++optind;
6521397Smckusick 	}
6632111Sbostic 	else {					/* need an argument */
6732111Sbostic 		if (*place)			/* no white space */
6832111Sbostic 			optarg = place;
6921397Smckusick 		else if (nargc <= ++optind) {	/* no arg */
7021397Smckusick 			place = EMSG;
7121397Smckusick 			tell(": option requires an argument -- ");
7221397Smckusick 		}
7332111Sbostic 	 	else				/* white space */
7432111Sbostic 			optarg = nargv[optind];
7521397Smckusick 		place = EMSG;
7621397Smckusick 		++optind;
7721397Smckusick 	}
7832111Sbostic 	return(optopt);				/* dump back option letter */
7921397Smckusick }
80