xref: /titanic_41/usr/src/lib/libsecdb/common/secdb.c (revision a126dc1ad668882a9be92c984e6849b9f7852115)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a126dc1aSTony Nguyen  * Common Development and Distribution License (the "License").
6*a126dc1aSTony Nguyen  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*a126dc1aSTony Nguyen  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <strings.h>
307c478bd9Sstevel@tonic-gate #include <secdb.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate /* From libnsl */
347c478bd9Sstevel@tonic-gate extern char *_strdup_null(char *);
357c478bd9Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
367c478bd9Sstevel@tonic-gate extern char *_strpbrk_escape(char *, char *);
377c478bd9Sstevel@tonic-gate extern char *_unescape(char *, char *);
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate char *_do_unescape(char *);
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  * kva_match(): Given a key-value array and a key, return a pointer to the
447c478bd9Sstevel@tonic-gate  * value that matches the key.
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate char *
477c478bd9Sstevel@tonic-gate kva_match(kva_t *kva, char *key)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	int	i;
507c478bd9Sstevel@tonic-gate 	kv_t	*data;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if (kva == NULL || key == NULL) {
537c478bd9Sstevel@tonic-gate 		return ((char *)NULL);
547c478bd9Sstevel@tonic-gate 	}
557c478bd9Sstevel@tonic-gate 	data = kva->data;
567c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
577c478bd9Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
587c478bd9Sstevel@tonic-gate 			return (data[i].value);
597c478bd9Sstevel@tonic-gate 		}
607c478bd9Sstevel@tonic-gate 	}
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	return ((char *)NULL);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * _kva_free(): Free up memory.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate void
697c478bd9Sstevel@tonic-gate _kva_free(kva_t *kva)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	int	i;
727c478bd9Sstevel@tonic-gate 	kv_t	*data;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
757c478bd9Sstevel@tonic-gate 		return;
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	data = kva->data;
787c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
797c478bd9Sstevel@tonic-gate 		if (data[i].key != NULL) {
807c478bd9Sstevel@tonic-gate 			free(data[i].key);
817c478bd9Sstevel@tonic-gate 			data[i].key = NULL;
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 		if (data[i].value != NULL) {
847c478bd9Sstevel@tonic-gate 			free(data[i].value);
857c478bd9Sstevel@tonic-gate 			data[i].value = NULL;
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	free(kva->data);
897c478bd9Sstevel@tonic-gate 	free(kva);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * new_kva(): Allocate a key-value array.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate kva_t  *
967c478bd9Sstevel@tonic-gate _new_kva(int size)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	kva_t	*new_kva;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) {
1017c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) {
1047c478bd9Sstevel@tonic-gate 		free(new_kva);
1057c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (new_kva);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter
1137c478bd9Sstevel@tonic-gate  * (del), place the values into the key value array (nkva).
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate kva_t  *
1167c478bd9Sstevel@tonic-gate _str2kva(char *s, char *ass, char *del)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	int	n = 0;
1197c478bd9Sstevel@tonic-gate 	int	m;
1207c478bd9Sstevel@tonic-gate 	int	size = KV_ADD_KEYS;
1217c478bd9Sstevel@tonic-gate 	char	*buf;
1227c478bd9Sstevel@tonic-gate 	char	*p;
1237c478bd9Sstevel@tonic-gate 	char	*pair;
1247c478bd9Sstevel@tonic-gate 	char	*key;
1257c478bd9Sstevel@tonic-gate 	char	*last_pair;
1267c478bd9Sstevel@tonic-gate 	char	*last_key;
1277c478bd9Sstevel@tonic-gate 	kv_t	*data;
1287c478bd9Sstevel@tonic-gate 	kva_t	*nkva;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (s == NULL ||
1317c478bd9Sstevel@tonic-gate 	    ass == NULL ||
1327c478bd9Sstevel@tonic-gate 	    del == NULL ||
1337c478bd9Sstevel@tonic-gate 	    *s == '\0' ||
1347c478bd9Sstevel@tonic-gate 	    *s == '\n' ||
1357c478bd9Sstevel@tonic-gate 	    (strlen(s) <= 1)) {
1367c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
1377c478bd9Sstevel@tonic-gate 	}
1387c478bd9Sstevel@tonic-gate 	p = s;
1397c478bd9Sstevel@tonic-gate 	while ((p = _strpbrk_escape(p, ass)) != NULL) {
1407c478bd9Sstevel@tonic-gate 		n++;
1417c478bd9Sstevel@tonic-gate 		p++;
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	if (n > size) {
1447c478bd9Sstevel@tonic-gate 		m = n/size;
1457c478bd9Sstevel@tonic-gate 		if (n%size) {
1467c478bd9Sstevel@tonic-gate 			++m;
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 		size = m * KV_ADD_KEYS;
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
1517c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	data = nkva->data;
1547c478bd9Sstevel@tonic-gate 	nkva->length = 0;
1557c478bd9Sstevel@tonic-gate 	if ((buf = strdup(s)) == NULL) {
1567c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 	pair = _strtok_escape(buf, del, &last_pair);
1597c478bd9Sstevel@tonic-gate 	do {
1607c478bd9Sstevel@tonic-gate 		key = _strtok_escape(pair, ass, &last_key);
1617c478bd9Sstevel@tonic-gate 		if (key != NULL) {
1627c478bd9Sstevel@tonic-gate 			data[nkva->length].key = _do_unescape(key);
1637c478bd9Sstevel@tonic-gate 			data[nkva->length].value = _do_unescape(last_key);
1647c478bd9Sstevel@tonic-gate 			nkva->length++;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 	} while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL);
1677c478bd9Sstevel@tonic-gate 	free(buf);
1687c478bd9Sstevel@tonic-gate 	return (nkva);
1697c478bd9Sstevel@tonic-gate }
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * _kva2str(): Given an array of key-value pairs, place them into a string
1737c478bd9Sstevel@tonic-gate  * (buf). Use delimeter (del) to separate pairs.  Use assignment character
1747c478bd9Sstevel@tonic-gate  * (ass) to separate keys and values.
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  * Return Values: 0  Success 1  Buffer too small 2  Out of memory
1777c478bd9Sstevel@tonic-gate  */
1787c478bd9Sstevel@tonic-gate int
1797c478bd9Sstevel@tonic-gate _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	int	i;
1827c478bd9Sstevel@tonic-gate 	int	length = 0;
1837c478bd9Sstevel@tonic-gate 	char	*tmp;
1847c478bd9Sstevel@tonic-gate 	kv_t	*data;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
1877c478bd9Sstevel@tonic-gate 		return (0);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	data = kva->data;
1907c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
1917c478bd9Sstevel@tonic-gate 		if (data[i].value != NULL) {
1927c478bd9Sstevel@tonic-gate 			length += 2 + strlen(data[i].value);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	if (length > buflen) {
1967c478bd9Sstevel@tonic-gate 		return (1);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	(void) memset(buf, 0, buflen);
1997c478bd9Sstevel@tonic-gate 	if ((tmp = (char *)malloc(buflen)) == NULL) {
2007c478bd9Sstevel@tonic-gate 		return (2);
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
2037c478bd9Sstevel@tonic-gate 		if (data[i].value != NULL) {
2047c478bd9Sstevel@tonic-gate 			if (snprintf(tmp, buflen, "%s%s%s%s",
2057c478bd9Sstevel@tonic-gate 			    data[i].key, ass, data[i].value, del) >= buflen) {
2067c478bd9Sstevel@tonic-gate 				return (0);
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 			(void) strcat(buf, tmp);
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 	return (0);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate int
2157c478bd9Sstevel@tonic-gate _insert2kva(kva_t *kva, char *key, char *value)
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	int	i;
2187c478bd9Sstevel@tonic-gate 	kv_t	*data;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
2217c478bd9Sstevel@tonic-gate 		return (0);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	data = kva->data;
2247c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
2257c478bd9Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
2267c478bd9Sstevel@tonic-gate 			if (data[i].value != NULL)
2277c478bd9Sstevel@tonic-gate 				free(data[i].value);
2287c478bd9Sstevel@tonic-gate 			data[i].value = _strdup_null(value);
2297c478bd9Sstevel@tonic-gate 			return (0);
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	return (1);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate kva_t  *
2367c478bd9Sstevel@tonic-gate _kva_dup(kva_t *old_kva)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	int	i;
2397c478bd9Sstevel@tonic-gate 	int	size;
2407c478bd9Sstevel@tonic-gate 	kv_t	*old_data;
2417c478bd9Sstevel@tonic-gate 	kv_t	*new_data;
2427c478bd9Sstevel@tonic-gate 	kva_t 	*nkva = (kva_t *)NULL;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (old_kva == NULL) {
2457c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 	old_data = old_kva->data;
2487c478bd9Sstevel@tonic-gate 	size = old_kva->length;
2497c478bd9Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
2507c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	new_data = nkva->data;
2537c478bd9Sstevel@tonic-gate 	nkva->length = old_kva->length;
2547c478bd9Sstevel@tonic-gate 	for (i = 0; i <= nkva->length; i++) {
2557c478bd9Sstevel@tonic-gate 		new_data[i].key = _strdup_null(old_data[i].key);
2567c478bd9Sstevel@tonic-gate 		new_data[i].value = _strdup_null(old_data[i].value);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	return (nkva);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static void
2637c478bd9Sstevel@tonic-gate strip_spaces(char **valuep)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	char *p, *start;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* Find first non-white space character and return pointer to it */
2687c478bd9Sstevel@tonic-gate 	for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++)
2697c478bd9Sstevel@tonic-gate 		;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	*valuep = start = p;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if (*p == '\0')
2747c478bd9Sstevel@tonic-gate 		return;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	p = p + strlen(p) - 1;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/* Remove trailing spaces */
2797c478bd9Sstevel@tonic-gate 	while (p > start && isspace((unsigned char)*p))
2807c478bd9Sstevel@tonic-gate 		p--;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	p[1] = '\0';
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate char *
2867c478bd9Sstevel@tonic-gate _do_unescape(char *src)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
2897c478bd9Sstevel@tonic-gate 	char *dst = NULL;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (src == NULL) {
2927c478bd9Sstevel@tonic-gate 		dst = _strdup_null(src);
2937c478bd9Sstevel@tonic-gate 	} else {
2947c478bd9Sstevel@tonic-gate 		strip_spaces(&src);
2957c478bd9Sstevel@tonic-gate 		tmp = _unescape(src, "=;:,\\");
2967c478bd9Sstevel@tonic-gate 		dst = (tmp == NULL) ? _strdup_null(src) : tmp;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	return (dst);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate /*
3047c478bd9Sstevel@tonic-gate  * Some utilities for handling comma-separated lists.
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate char *
3077c478bd9Sstevel@tonic-gate _argv_to_csl(char **strings)
3087c478bd9Sstevel@tonic-gate {
3097c478bd9Sstevel@tonic-gate 	int len = 0;
3107c478bd9Sstevel@tonic-gate 	int i = 0;
3117c478bd9Sstevel@tonic-gate 	char *newstr = (char *)NULL;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (strings == NULL)
3147c478bd9Sstevel@tonic-gate 		return ((char *)NULL);
3157c478bd9Sstevel@tonic-gate 	for (i = 0; strings[i] != NULL; i++) {
3167c478bd9Sstevel@tonic-gate 		len += strlen(strings[i]) + 1;
3177c478bd9Sstevel@tonic-gate 	}
318*a126dc1aSTony Nguyen 	if ((len > 0) && ((newstr = (char *)malloc(len + 1)) != NULL)) {
3197c478bd9Sstevel@tonic-gate 		(void) memset(newstr, 0, len);
3207c478bd9Sstevel@tonic-gate 		for (i = 0; strings[i] != NULL; i++) {
3217c478bd9Sstevel@tonic-gate 			(void) strcat(newstr, strings[i]);
3227c478bd9Sstevel@tonic-gate 			(void) strcat(newstr, ",");
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 		newstr[len-1] = NULL;
3257c478bd9Sstevel@tonic-gate 		return (newstr);
326*a126dc1aSTony Nguyen 	} else
327*a126dc1aSTony Nguyen 		return ((char *)NULL);
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate char **
3327c478bd9Sstevel@tonic-gate _csl_to_argv(char *csl)
3337c478bd9Sstevel@tonic-gate {
3347c478bd9Sstevel@tonic-gate 	int len = 0;
3357c478bd9Sstevel@tonic-gate 	int ncommas = 0;
3367c478bd9Sstevel@tonic-gate 	int i = 0;
3377c478bd9Sstevel@tonic-gate 	char **spc = (char **)NULL;
3387c478bd9Sstevel@tonic-gate 	char *copy = (char *)NULL;
3397c478bd9Sstevel@tonic-gate 	char *pc;
3407c478bd9Sstevel@tonic-gate 	char *lasts = (char *)NULL;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	len = strlen(csl);
3437c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
3447c478bd9Sstevel@tonic-gate 		if (csl[i] == ',')
3457c478bd9Sstevel@tonic-gate 			ncommas++;
3467c478bd9Sstevel@tonic-gate 	}
3477c478bd9Sstevel@tonic-gate 	if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) {
3487c478bd9Sstevel@tonic-gate 		return ((char **)NULL);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	copy = strdup(csl);
3517c478bd9Sstevel@tonic-gate 	for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL;
3527c478bd9Sstevel@tonic-gate 	    pc = strtok_r(NULL, ",", &lasts), i++) {
3537c478bd9Sstevel@tonic-gate 		spc[i] = strdup(pc);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	spc[i] = NULL;
3567c478bd9Sstevel@tonic-gate 	free(copy);
3577c478bd9Sstevel@tonic-gate 	return (spc);
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate void
3627c478bd9Sstevel@tonic-gate _free_argv(char **p_argv)
3637c478bd9Sstevel@tonic-gate {
3647c478bd9Sstevel@tonic-gate 	char **p_a;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	for (p_a = p_argv; *p_a != NULL; p_a++)
3677c478bd9Sstevel@tonic-gate 		free(*p_a);
3687c478bd9Sstevel@tonic-gate 	free(p_argv);
3697c478bd9Sstevel@tonic-gate }
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate #ifdef DEBUG
3737c478bd9Sstevel@tonic-gate void
3747c478bd9Sstevel@tonic-gate print_kva(kva_t *kva)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	int	i;
3777c478bd9Sstevel@tonic-gate 	kv_t	*data;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
3807c478bd9Sstevel@tonic-gate 		printf("  (empty)\n");
3817c478bd9Sstevel@tonic-gate 		return;
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 	data = kva->data;
3847c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
3857c478bd9Sstevel@tonic-gate 		printf("  %s = %s\n", data[i].key, data[i].value);
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
389