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