xref: /original-bsd/lib/libc/stdlib/getopt.c (revision 23cd6db2)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char sccsid[] = "@(#)getopt.c	4.11 (Berkeley) 02/20/90";
20 #endif /* LIBC_SCCS and not lint */
21 
22 #include <stdio.h>
23 
24 /*
25  * get option letter from argument vector
26  */
27 int	opterr = 1,		/* if error message should be printed */
28 	optind = 1,		/* index into parent argv vector */
29 	optopt;			/* character checked for validity */
30 char	*optarg;		/* argument associated with option */
31 
32 #define	BADCH	(int)'?'
33 #define	EMSG	""
34 
35 getopt(nargc, nargv, ostr)
36 	int nargc;
37 	char **nargv, *ostr;
38 {
39 	static char *place = EMSG;		/* option letter processing */
40 	register char *oli;			/* option letter list index */
41 	char *p, *index(), *rindex();
42 
43 	if (!*place) {				/* update scanning pointer */
44 		if (optind >= nargc || *(place = nargv[optind]) != '-') {
45 			place = EMSG;
46 			return(EOF);
47 		}
48 		if (place[1] && *++place == '-') {	/* found "--" */
49 			++optind;
50 			place = EMSG;
51 			return(EOF);
52 		}
53 	}					/* option letter okay? */
54 	if ((optopt = (int)*place++) == (int)':' ||
55 	    !(oli = index(ostr, optopt))) {
56 		/*
57 		 * if the user didn't specify '-' as an option,
58 		 * assume it means EOF.
59 		 */
60 		if (optopt == (int)'-')
61 			return(EOF);
62 		if (!*place)
63 			++optind;
64 		if (opterr) {
65 			if (!(p = rindex(*nargv, '/')))
66 				p = *nargv;
67 			else
68 				++p;
69 			(void)fprintf(stderr, "%s: illegal option -- %c\n",
70 			    p, optopt);
71 		}
72 		return(BADCH);
73 	}
74 	if (*++oli != ':') {			/* don't need argument */
75 		optarg = NULL;
76 		if (!*place)
77 			++optind;
78 	}
79 	else {					/* need an argument */
80 		if (*place)			/* no white space */
81 			optarg = place;
82 		else if (nargc <= ++optind) {	/* no arg */
83 			place = EMSG;
84 			if (!(p = rindex(*nargv, '/')))
85 				p = *nargv;
86 			else
87 				++p;
88 			if (opterr)
89 				(void)fprintf(stderr,
90 				    "%s: option requires an argument -- %c\n",
91 				    p, optopt);
92 			return(BADCH);
93 		}
94 	 	else				/* white space */
95 			optarg = nargv[optind];
96 		place = EMSG;
97 		++optind;
98 	}
99 	return(optopt);				/* dump back option letter */
100 }
101