xref: /csrg-svn/lib/libc/stdlib/getopt.c (revision 34831)
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
6*34831Sbostic  * provided that the above copyright notice and this paragraph are
7*34831Sbostic  * duplicated in all such forms and that any documentation,
8*34831Sbostic  * advertising materials, and other materials related to such
9*34831Sbostic  * distribution and use acknowledge that the software was developed
10*34831Sbostic  * by the University of California, Berkeley.  The name of the
11*34831Sbostic  * University may not be used to endorse or promote products derived
12*34831Sbostic  * from this software without specific prior written permission.
13*34831Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34831Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34831Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621397Smckusick  */
1721397Smckusick 
1826524Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34831Sbostic static char sccsid[] = "@(#)getopt.c	4.7 (Berkeley) 06/27/88";
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	""
3434017Sbostic #define	tell(s)	{ \
3532111Sbostic 	if (opterr) { \
3632111Sbostic 		fputs(*nargv, stderr); \
3732111Sbostic 		fputs(s, stderr); \
3832111Sbostic 		fputc(optopt, stderr); \
3932111Sbostic 		fputc((int)'\n', stderr); \
4032111Sbostic 	} \
4132111Sbostic 	return(BADCH); \
4232111Sbostic }
4321397Smckusick 
4432111Sbostic getopt(nargc, nargv, ostr)
4534017Sbostic 	int nargc;
4634017Sbostic 	char **nargv, *ostr;
4721397Smckusick {
4834017Sbostic 	static char *place = EMSG;		/* option letter processing */
4934017Sbostic 	register char *oli;			/* option letter list index */
5034017Sbostic 	char *index();
5121397Smckusick 
5232111Sbostic 	if (!*place) {				/* update scanning pointer */
5334017Sbostic 		if (optind >= nargc || *(place = nargv[optind]) != '-')
5432111Sbostic 			return(EOF);
5534017Sbostic 		if (place[1] && *++place == '-') {	/* found "--" */
5621397Smckusick 			++optind;
5721397Smckusick 			return(EOF);
5821397Smckusick 		}
5932111Sbostic 	}					/* option letter okay? */
6032111Sbostic 	if ((optopt = (int)*place++) == (int)':' ||
6132111Sbostic 	    !(oli = index(ostr, optopt))) {
6232111Sbostic 		if (!*place)
6332111Sbostic 			++optind;
6421397Smckusick 		tell(": illegal option -- ");
6521397Smckusick 	}
6632111Sbostic 	if (*++oli != ':') {			/* don't need argument */
6721397Smckusick 		optarg = NULL;
6832111Sbostic 		if (!*place)
6932111Sbostic 			++optind;
7021397Smckusick 	}
7132111Sbostic 	else {					/* need an argument */
7232111Sbostic 		if (*place)			/* no white space */
7332111Sbostic 			optarg = place;
7421397Smckusick 		else if (nargc <= ++optind) {	/* no arg */
7521397Smckusick 			place = EMSG;
7621397Smckusick 			tell(": option requires an argument -- ");
7721397Smckusick 		}
7832111Sbostic 	 	else				/* white space */
7932111Sbostic 			optarg = nargv[optind];
8021397Smckusick 		place = EMSG;
8121397Smckusick 		++optind;
8221397Smckusick 	}
8332111Sbostic 	return(optopt);				/* dump back option letter */
8421397Smckusick }
85