1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /*
28  * Copyright 2019 J. Schilling
29  *
30  * @(#)getsubopt.c	1.2 19/11/07 J. Schilling
31  */
32 #if defined(sun)
33 #pragma ident "@(#)getsubopt.c 1.2 19/11/07 J. Schilling"
34 #endif
35 
36 /*	Copyright (c) 1988 AT&T	*/
37 /*	  All Rights Reserved  	*/
38 
39 #if defined(sun)
40 #pragma ident	"@(#)getsubopt.c	1.14	08/06/06 SMI"
41 #endif
42 
43 /*
44  * getsubopt - parse suboptions from a flag argument.
45  */
46 /*#pragma weak _getsubopt = getsubopt*/
47 
48 /*#include "lint.h"*/
49 #include <schily/types.h>
50 #include <schily/string.h>
51 #include <schily/stdlib.h>
52 #include <schily/stdio.h>
53 
54 int
getsubopt(char ** optionsp,char * const * tokens,char ** valuep)55 getsubopt(char **optionsp, char * const *tokens, char **valuep)
56 {
57 	char *s = *optionsp, *p;
58 	int i;
59 	size_t optlen;
60 
61 	*valuep = NULL;
62 	if (*s == '\0')
63 		return (-1);
64 	p = strchr(s, ',');		/* find next option */
65 	if (p == NULL) {
66 		p = s + strlen(s);
67 	} else {
68 		*p++ = '\0';		/* mark end and point to next */
69 	}
70 	*optionsp = p;			/* point to next option */
71 	p = strchr(s, '=');		/* find value */
72 	if (p == NULL) {
73 		optlen = strlen(s);
74 		*valuep = NULL;
75 	} else {
76 		optlen = p - s;
77 		*valuep = ++p;
78 	}
79 	for (i = 0; tokens[i] != NULL; i++) {
80 		if ((optlen == strlen(tokens[i])) &&
81 		    (strncmp(s, tokens[i], optlen) == 0))
82 			return (i);
83 	}
84 	/* no match, point value at option and return error */
85 	*valuep = s;
86 	return (-1);
87 }
88