1241675Suqs#ifdef HAVE_CONFIG_H
2241675Suqs#include "config.h"
3241675Suqs#endif
4241675Suqs
5241675Suqs#ifdef HAVE_GETSUBOPT
6241675Suqs
7241675Suqsint dummy;
8241675Suqs
9241675Suqs#else
10241675Suqs
11241675Suqs/*	$OpenBSD: getsubopt.c,v 1.4 2005/08/08 08:05:36 espie Exp $	*/
12241675Suqs
13241675Suqs/*-
14241675Suqs * Copyright (c) 1990, 1993
15241675Suqs *	The Regents of the University of California.  All rights reserved.
16241675Suqs *
17241675Suqs * Redistribution and use in source and binary forms, with or without
18241675Suqs * modification, are permitted provided that the following conditions
19241675Suqs * are met:
20241675Suqs * 1. Redistributions of source code must retain the above copyright
21241675Suqs *    notice, this list of conditions and the following disclaimer.
22241675Suqs * 2. Redistributions in binary form must reproduce the above copyright
23241675Suqs *    notice, this list of conditions and the following disclaimer in the
24241675Suqs *    documentation and/or other materials provided with the distribution.
25241675Suqs * 3. Neither the name of the University nor the names of its contributors
26241675Suqs *    may be used to endorse or promote products derived from this software
27241675Suqs *    without specific prior written permission.
28241675Suqs *
29241675Suqs * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30241675Suqs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31241675Suqs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32241675Suqs * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33241675Suqs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34241675Suqs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35241675Suqs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36241675Suqs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37241675Suqs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38241675Suqs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39241675Suqs * SUCH DAMAGE.
40241675Suqs */
41241675Suqs
42241675Suqs#include <unistd.h>
43241675Suqs#include <stdlib.h>
44241675Suqs#include <string.h>
45241675Suqs
46241675Suqs/*
47241675Suqs * The SVID interface to getsubopt provides no way of figuring out which
48241675Suqs * part of the suboptions list wasn't matched.  This makes error messages
49241675Suqs * tricky...  The extern variable suboptarg is a pointer to the token
50241675Suqs * which didn't match.
51241675Suqs */
52241675Suqschar *suboptarg;
53241675Suqs
54241675Suqsint
55241675Suqsgetsubopt(char **optionp, char * const *tokens, char **valuep)
56241675Suqs{
57241675Suqs	int cnt;
58241675Suqs	char *p;
59241675Suqs
60241675Suqs	suboptarg = *valuep = NULL;
61241675Suqs
62241675Suqs	if (!optionp || !*optionp)
63241675Suqs		return(-1);
64241675Suqs
65241675Suqs	/* skip leading white-space, commas */
66241675Suqs	for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
67241675Suqs
68241675Suqs	if (!*p) {
69241675Suqs		*optionp = p;
70241675Suqs		return(-1);
71241675Suqs	}
72241675Suqs
73241675Suqs	/* save the start of the token, and skip the rest of the token. */
74241675Suqs	for (suboptarg = p;
75241675Suqs	    *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
76241675Suqs
77241675Suqs	if (*p) {
78241675Suqs		/*
79241675Suqs		 * If there's an equals sign, set the value pointer, and
80241675Suqs		 * skip over the value part of the token.  Terminate the
81241675Suqs		 * token.
82241675Suqs		 */
83241675Suqs		if (*p == '=') {
84241675Suqs			*p = '\0';
85241675Suqs			for (*valuep = ++p;
86241675Suqs			    *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
87241675Suqs			if (*p)
88241675Suqs				*p++ = '\0';
89241675Suqs		} else
90241675Suqs			*p++ = '\0';
91241675Suqs		/* Skip any whitespace or commas after this token. */
92241675Suqs		for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
93241675Suqs	}
94241675Suqs
95241675Suqs	/* set optionp for next round. */
96241675Suqs	*optionp = p;
97241675Suqs
98241675Suqs	for (cnt = 0; *tokens; ++tokens, ++cnt)
99241675Suqs		if (!strcmp(suboptarg, *tokens))
100241675Suqs			return(cnt);
101241675Suqs	return(-1);
102241675Suqs}
103241675Suqs
104241675Suqs#endif
105