xref: /haiku/src/system/libroot/posix/musl/misc/getsubopt.c (revision 97f11716bfaa0f385eb0e28a52bf56a5023b9e99)
1*22b1d791SAugustin Cavalier #include <stdlib.h>
2*22b1d791SAugustin Cavalier #include <string.h>
3*22b1d791SAugustin Cavalier 
getsubopt(char ** opt,char * const * keys,char ** val)4*22b1d791SAugustin Cavalier int getsubopt(char **opt, char *const *keys, char **val)
5*22b1d791SAugustin Cavalier {
6*22b1d791SAugustin Cavalier 	char *s = *opt;
7*22b1d791SAugustin Cavalier 	int i;
8*22b1d791SAugustin Cavalier 
9*22b1d791SAugustin Cavalier 	*val = NULL;
10*22b1d791SAugustin Cavalier 	*opt = strchr(s, ',');
11*22b1d791SAugustin Cavalier 	if (*opt) *(*opt)++ = 0;
12*22b1d791SAugustin Cavalier 	else *opt = s + strlen(s);
13*22b1d791SAugustin Cavalier 
14*22b1d791SAugustin Cavalier 	for (i=0; keys[i]; i++) {
15*22b1d791SAugustin Cavalier 		size_t l = strlen(keys[i]);
16*22b1d791SAugustin Cavalier 		if (strncmp(keys[i], s, l)) continue;
17*22b1d791SAugustin Cavalier 		if (s[l] == '=')
18*22b1d791SAugustin Cavalier 			*val = s + l + 1;
19*22b1d791SAugustin Cavalier 		else if (s[l]) continue;
20*22b1d791SAugustin Cavalier 		return i;
21*22b1d791SAugustin Cavalier 	}
22*22b1d791SAugustin Cavalier 	return -1;
23*22b1d791SAugustin Cavalier }
24