xref: /csrg-svn/lib/libc/stdlib/getopt.c (revision 39712)
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
634831Sbostic  * provided that the above copyright notice and this paragraph are
734831Sbostic  * duplicated in all such forms and that any documentation,
834831Sbostic  * advertising materials, and other materials related to such
934831Sbostic  * distribution and use acknowledge that the software was developed
1034831Sbostic  * by the University of California, Berkeley.  The name of the
1134831Sbostic  * University may not be used to endorse or promote products derived
1234831Sbostic  * from this software without specific prior written permission.
1334831Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434831Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534831Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621397Smckusick  */
1721397Smckusick 
1826524Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*39712Sbostic static char sccsid[] = "@(#)getopt.c	4.10 (Berkeley) 12/14/89";
2034017Sbostic #endif /* LIBC_SCCS and not lint */
2121397Smckusick 
2221397Smckusick #include <stdio.h>
2321397Smckusick 
2421397Smckusick /*
2521397Smckusick  * get option letter from argument vector
2621397Smckusick  */
2725264Smckusick int	opterr = 1,		/* if error message should be printed */
2821397Smckusick 	optind = 1,		/* index into parent argv vector */
2921397Smckusick 	optopt;			/* character checked for validity */
3021397Smckusick char	*optarg;		/* argument associated with option */
3121397Smckusick 
3234017Sbostic #define	BADCH	(int)'?'
3334017Sbostic #define	EMSG	""
3421397Smckusick 
3532111Sbostic getopt(nargc, nargv, ostr)
3634017Sbostic 	int nargc;
3734017Sbostic 	char **nargv, *ostr;
3821397Smckusick {
3934017Sbostic 	static char *place = EMSG;		/* option letter processing */
4034017Sbostic 	register char *oli;			/* option letter list index */
4138284Sbostic 	char *p, *index(), *rindex();
4221397Smckusick 
4332111Sbostic 	if (!*place) {				/* update scanning pointer */
4434017Sbostic 		if (optind >= nargc || *(place = nargv[optind]) != '-')
4532111Sbostic 			return(EOF);
4634017Sbostic 		if (place[1] && *++place == '-') {	/* found "--" */
4721397Smckusick 			++optind;
4821397Smckusick 			return(EOF);
4921397Smckusick 		}
5032111Sbostic 	}					/* option letter okay? */
5132111Sbostic 	if ((optopt = (int)*place++) == (int)':' ||
5232111Sbostic 	    !(oli = index(ostr, optopt))) {
53*39712Sbostic 		/*
54*39712Sbostic 		 * if the user didn't specify '-' as an option,
55*39712Sbostic 		 * assume it means EOF.
56*39712Sbostic 		 */
57*39712Sbostic 		if (optopt == (int)'-')
58*39712Sbostic 			return(EOF);
5932111Sbostic 		if (!*place)
6032111Sbostic 			++optind;
6138284Sbostic 		if (opterr) {
6238284Sbostic 			if (!(p = rindex(*nargv, '/')))
6338284Sbostic 				p = *nargv;
6438284Sbostic 			else
6538284Sbostic 				++p;
6636960Sbostic 			(void)fprintf(stderr, "%s: illegal option -- %c\n",
6738284Sbostic 			    p, optopt);
6838284Sbostic 		}
6936960Sbostic 		return(BADCH);
7021397Smckusick 	}
7132111Sbostic 	if (*++oli != ':') {			/* don't need argument */
7221397Smckusick 		optarg = NULL;
7332111Sbostic 		if (!*place)
7432111Sbostic 			++optind;
7521397Smckusick 	}
7632111Sbostic 	else {					/* need an argument */
7732111Sbostic 		if (*place)			/* no white space */
7832111Sbostic 			optarg = place;
7921397Smckusick 		else if (nargc <= ++optind) {	/* no arg */
8021397Smckusick 			place = EMSG;
8138284Sbostic 			if (!(p = rindex(*nargv, '/')))
8238284Sbostic 				p = *nargv;
8338284Sbostic 			else
8438284Sbostic 				++p;
8536960Sbostic 			if (opterr)
8636960Sbostic 				(void)fprintf(stderr,
8736960Sbostic 				    "%s: option requires an argument -- %c\n",
8838284Sbostic 				    p, optopt);
8936960Sbostic 			return(BADCH);
9021397Smckusick 		}
9132111Sbostic 	 	else				/* white space */
9232111Sbostic 			optarg = nargv[optind];
9321397Smckusick 		place = EMSG;
9421397Smckusick 		++optind;
9521397Smckusick 	}
9632111Sbostic 	return(optopt);				/* dump back option letter */
9721397Smckusick }
98