1*92395e9cSLionel Sambuc #ifdef HAVE_CONFIG_H
2*92395e9cSLionel Sambuc #include "config.h"
3*92395e9cSLionel Sambuc #endif
4*92395e9cSLionel Sambuc
5*92395e9cSLionel Sambuc #ifdef HAVE_GETSUBOPT
6*92395e9cSLionel Sambuc
7*92395e9cSLionel Sambuc int dummy;
8*92395e9cSLionel Sambuc
9*92395e9cSLionel Sambuc #else
10*92395e9cSLionel Sambuc
11*92395e9cSLionel Sambuc /* $OpenBSD: getsubopt.c,v 1.4 2005/08/08 08:05:36 espie Exp $ */
12*92395e9cSLionel Sambuc
13*92395e9cSLionel Sambuc /*-
14*92395e9cSLionel Sambuc * Copyright (c) 1990, 1993
15*92395e9cSLionel Sambuc * The Regents of the University of California. All rights reserved.
16*92395e9cSLionel Sambuc *
17*92395e9cSLionel Sambuc * Redistribution and use in source and binary forms, with or without
18*92395e9cSLionel Sambuc * modification, are permitted provided that the following conditions
19*92395e9cSLionel Sambuc * are met:
20*92395e9cSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
21*92395e9cSLionel Sambuc * notice, this list of conditions and the following disclaimer.
22*92395e9cSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
23*92395e9cSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
24*92395e9cSLionel Sambuc * documentation and/or other materials provided with the distribution.
25*92395e9cSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
26*92395e9cSLionel Sambuc * may be used to endorse or promote products derived from this software
27*92395e9cSLionel Sambuc * without specific prior written permission.
28*92395e9cSLionel Sambuc *
29*92395e9cSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30*92395e9cSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31*92395e9cSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32*92395e9cSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33*92395e9cSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34*92395e9cSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35*92395e9cSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36*92395e9cSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37*92395e9cSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38*92395e9cSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39*92395e9cSLionel Sambuc * SUCH DAMAGE.
40*92395e9cSLionel Sambuc */
41*92395e9cSLionel Sambuc
42*92395e9cSLionel Sambuc #include <unistd.h>
43*92395e9cSLionel Sambuc #include <stdlib.h>
44*92395e9cSLionel Sambuc #include <string.h>
45*92395e9cSLionel Sambuc
46*92395e9cSLionel Sambuc /*
47*92395e9cSLionel Sambuc * The SVID interface to getsubopt provides no way of figuring out which
48*92395e9cSLionel Sambuc * part of the suboptions list wasn't matched. This makes error messages
49*92395e9cSLionel Sambuc * tricky... The extern variable suboptarg is a pointer to the token
50*92395e9cSLionel Sambuc * which didn't match.
51*92395e9cSLionel Sambuc */
52*92395e9cSLionel Sambuc char *suboptarg;
53*92395e9cSLionel Sambuc
54*92395e9cSLionel Sambuc int
getsubopt(char ** optionp,char * const * tokens,char ** valuep)55*92395e9cSLionel Sambuc getsubopt(char **optionp, char * const *tokens, char **valuep)
56*92395e9cSLionel Sambuc {
57*92395e9cSLionel Sambuc int cnt;
58*92395e9cSLionel Sambuc char *p;
59*92395e9cSLionel Sambuc
60*92395e9cSLionel Sambuc suboptarg = *valuep = NULL;
61*92395e9cSLionel Sambuc
62*92395e9cSLionel Sambuc if (!optionp || !*optionp)
63*92395e9cSLionel Sambuc return(-1);
64*92395e9cSLionel Sambuc
65*92395e9cSLionel Sambuc /* skip leading white-space, commas */
66*92395e9cSLionel Sambuc for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
67*92395e9cSLionel Sambuc
68*92395e9cSLionel Sambuc if (!*p) {
69*92395e9cSLionel Sambuc *optionp = p;
70*92395e9cSLionel Sambuc return(-1);
71*92395e9cSLionel Sambuc }
72*92395e9cSLionel Sambuc
73*92395e9cSLionel Sambuc /* save the start of the token, and skip the rest of the token. */
74*92395e9cSLionel Sambuc for (suboptarg = p;
75*92395e9cSLionel Sambuc *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
76*92395e9cSLionel Sambuc
77*92395e9cSLionel Sambuc if (*p) {
78*92395e9cSLionel Sambuc /*
79*92395e9cSLionel Sambuc * If there's an equals sign, set the value pointer, and
80*92395e9cSLionel Sambuc * skip over the value part of the token. Terminate the
81*92395e9cSLionel Sambuc * token.
82*92395e9cSLionel Sambuc */
83*92395e9cSLionel Sambuc if (*p == '=') {
84*92395e9cSLionel Sambuc *p = '\0';
85*92395e9cSLionel Sambuc for (*valuep = ++p;
86*92395e9cSLionel Sambuc *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
87*92395e9cSLionel Sambuc if (*p)
88*92395e9cSLionel Sambuc *p++ = '\0';
89*92395e9cSLionel Sambuc } else
90*92395e9cSLionel Sambuc *p++ = '\0';
91*92395e9cSLionel Sambuc /* Skip any whitespace or commas after this token. */
92*92395e9cSLionel Sambuc for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
93*92395e9cSLionel Sambuc }
94*92395e9cSLionel Sambuc
95*92395e9cSLionel Sambuc /* set optionp for next round. */
96*92395e9cSLionel Sambuc *optionp = p;
97*92395e9cSLionel Sambuc
98*92395e9cSLionel Sambuc for (cnt = 0; *tokens; ++tokens, ++cnt)
99*92395e9cSLionel Sambuc if (!strcmp(suboptarg, *tokens))
100*92395e9cSLionel Sambuc return(cnt);
101*92395e9cSLionel Sambuc return(-1);
102*92395e9cSLionel Sambuc }
103*92395e9cSLionel Sambuc
104*92395e9cSLionel Sambuc #endif
105