xref: /csrg-svn/lib/libc/stdlib/getsubopt.c (revision 45447)
1*45447Sbostic /*-
2*45447Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*45447Sbostic  * All rights reserved.
4*45447Sbostic  *
5*45447Sbostic  * %sccs.include.redist.c%
6*45447Sbostic  */
7*45447Sbostic 
8*45447Sbostic #ifndef lint
9*45447Sbostic static char sccsid[] = "@(#)getsubopt.c	5.1 (Berkeley) 10/30/90";
10*45447Sbostic #endif /* not lint */
11*45447Sbostic 
12*45447Sbostic #include <unistd.h>
13*45447Sbostic 
14*45447Sbostic /*
15*45447Sbostic  * The SVID interface to getsubopt provides no way of figuring out which
16*45447Sbostic  * part of the suboptions list wasn't matched.  This makes error messages
17*45447Sbostic  * tricky...  The extern variable suboptarg is a pointer to the token which
18*45447Sbostic  * didn't match.
19*45447Sbostic  */
20*45447Sbostic char *suboptarg;
21*45447Sbostic 
22*45447Sbostic getsubopt(optionp, tokens, valuep)
23*45447Sbostic 	register char **optionp, **tokens, **valuep;
24*45447Sbostic {
25*45447Sbostic 	register int cnt;
26*45447Sbostic 	register char *p;
27*45447Sbostic 
28*45447Sbostic 	suboptarg = *valuep = NULL;
29*45447Sbostic 
30*45447Sbostic 	if (!optionp || !*optionp)
31*45447Sbostic 		return(-1);
32*45447Sbostic 
33*45447Sbostic 	/* skip leading white-space, commas */
34*45447Sbostic 	for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
35*45447Sbostic 
36*45447Sbostic 	if (!*p) {
37*45447Sbostic 		*optionp = p;
38*45447Sbostic 		return(-1);
39*45447Sbostic 	}
40*45447Sbostic 
41*45447Sbostic 	/* save the start of the token, and skip the rest of the token. */
42*45447Sbostic 	for (suboptarg = p;
43*45447Sbostic 	    *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
44*45447Sbostic 
45*45447Sbostic 	if (*p) {
46*45447Sbostic 		/*
47*45447Sbostic 		 * If there's an equals sign, set the value pointer, and
48*45447Sbostic 		 * skip over the value part of the token.  Terminate the
49*45447Sbostic 		 * token.
50*45447Sbostic 		 */
51*45447Sbostic 		if (*p == '=') {
52*45447Sbostic 			*p = '\0';
53*45447Sbostic 			for (*valuep = ++p;
54*45447Sbostic 			    *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
55*45447Sbostic 			if (*p)
56*45447Sbostic 				*p++ = '\0';
57*45447Sbostic 		} else
58*45447Sbostic 			*p++ = '\0';
59*45447Sbostic 		/* Skip any whitespace or commas after this token. */
60*45447Sbostic 		for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
61*45447Sbostic 	}
62*45447Sbostic 
63*45447Sbostic 	/* set optionp for next round. */
64*45447Sbostic 	*optionp = p;
65*45447Sbostic 
66*45447Sbostic 	for (cnt = 0; *tokens; ++tokens, ++cnt)
67*45447Sbostic 		if (!strcmp(suboptarg, *tokens))
68*45447Sbostic 			return(cnt);
69*45447Sbostic 	return(-1);
70*45447Sbostic }
71