145447Sbostic /*-
2*61180Sbostic * Copyright (c) 1990, 1993
3*61180Sbostic * The Regents of the University of California. All rights reserved.
445447Sbostic *
545447Sbostic * %sccs.include.redist.c%
645447Sbostic */
745447Sbostic
845447Sbostic #ifndef lint
9*61180Sbostic static char sccsid[] = "@(#)getsubopt.c 8.1 (Berkeley) 06/04/93";
1045447Sbostic #endif /* not lint */
1145447Sbostic
1245447Sbostic #include <unistd.h>
1346627Sbostic #include <stdlib.h>
1445447Sbostic
1545447Sbostic /*
1645447Sbostic * The SVID interface to getsubopt provides no way of figuring out which
1745447Sbostic * part of the suboptions list wasn't matched. This makes error messages
1846627Sbostic * tricky... The extern variable suboptarg is a pointer to the token
1946627Sbostic * which didn't match.
2045447Sbostic */
2145447Sbostic char *suboptarg;
2245447Sbostic
getsubopt(optionp,tokens,valuep)2345447Sbostic getsubopt(optionp, tokens, valuep)
2446627Sbostic register char **optionp, **valuep;
2546627Sbostic register char * const *tokens;
2645447Sbostic {
2745447Sbostic register int cnt;
2845447Sbostic register char *p;
2945447Sbostic
3045447Sbostic suboptarg = *valuep = NULL;
3145447Sbostic
3245447Sbostic if (!optionp || !*optionp)
3345447Sbostic return(-1);
3445447Sbostic
3545447Sbostic /* skip leading white-space, commas */
3645447Sbostic for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
3745447Sbostic
3845447Sbostic if (!*p) {
3945447Sbostic *optionp = p;
4045447Sbostic return(-1);
4145447Sbostic }
4245447Sbostic
4345447Sbostic /* save the start of the token, and skip the rest of the token. */
4445447Sbostic for (suboptarg = p;
4545447Sbostic *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
4645447Sbostic
4745447Sbostic if (*p) {
4845447Sbostic /*
4945447Sbostic * If there's an equals sign, set the value pointer, and
5045447Sbostic * skip over the value part of the token. Terminate the
5145447Sbostic * token.
5245447Sbostic */
5345447Sbostic if (*p == '=') {
5445447Sbostic *p = '\0';
5545447Sbostic for (*valuep = ++p;
5645447Sbostic *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
5745447Sbostic if (*p)
5845447Sbostic *p++ = '\0';
5945447Sbostic } else
6045447Sbostic *p++ = '\0';
6145447Sbostic /* Skip any whitespace or commas after this token. */
6245447Sbostic for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
6345447Sbostic }
6445447Sbostic
6545447Sbostic /* set optionp for next round. */
6645447Sbostic *optionp = p;
6745447Sbostic
6845447Sbostic for (cnt = 0; *tokens; ++tokens, ++cnt)
6945447Sbostic if (!strcmp(suboptarg, *tokens))
7045447Sbostic return(cnt);
7145447Sbostic return(-1);
7245447Sbostic }
73