/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <stdlib.h>32#include <string.h>3334/*35* The SVID interface to getsubopt provides no way of figuring out which36* part of the suboptions list wasn't matched. This makes error messages37* tricky... The extern variable suboptarg is a pointer to the token38* which didn't match.39*/40char *suboptarg;4142int43getsubopt(char **optionp, char * const *tokens, char **valuep)44{45int cnt;46char *p;4748suboptarg = *valuep = NULL;4950if (!optionp || !*optionp)51return(-1);5253/* skip leading white-space, commas */54for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);5556if (!*p) {57*optionp = p;58return(-1);59}6061/* save the start of the token, and skip the rest of the token. */62for (suboptarg = p;63*++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);6465if (*p) {66/*67* If there's an equals sign, set the value pointer, and68* skip over the value part of the token. Terminate the69* token.70*/71if (*p == '=') {72*p = '\0';73for (*valuep = ++p;74*p && *p != ',' && *p != ' ' && *p != '\t'; ++p);75if (*p)76*p++ = '\0';77} else78*p++ = '\0';79/* Skip any whitespace or commas after this token. */80for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);81}8283/* set optionp for next round. */84*optionp = p;8586for (cnt = 0; *tokens; ++tokens, ++cnt)87if (!strcmp(suboptarg, *tokens))88return(cnt);89return(-1);90}919293