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