xref: /titanic_52/usr/src/lib/libsecdb/common/secdb.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate #include <secdb.h>
33*7c478bd9Sstevel@tonic-gate #include <ctype.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate /* From libnsl */
36*7c478bd9Sstevel@tonic-gate extern char *_strdup_null(char *);
37*7c478bd9Sstevel@tonic-gate extern char *_strtok_escape(char *, char *, char **);
38*7c478bd9Sstevel@tonic-gate extern char *_strpbrk_escape(char *, char *);
39*7c478bd9Sstevel@tonic-gate extern char *_unescape(char *, char *);
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate char *_do_unescape(char *);
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /*
45*7c478bd9Sstevel@tonic-gate  * kva_match(): Given a key-value array and a key, return a pointer to the
46*7c478bd9Sstevel@tonic-gate  * value that matches the key.
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate char *
49*7c478bd9Sstevel@tonic-gate kva_match(kva_t *kva, char *key)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	int	i;
52*7c478bd9Sstevel@tonic-gate 	kv_t	*data;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if (kva == NULL || key == NULL) {
55*7c478bd9Sstevel@tonic-gate 		return ((char *)NULL);
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 	data = kva->data;
58*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
59*7c478bd9Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
60*7c478bd9Sstevel@tonic-gate 			return (data[i].value);
61*7c478bd9Sstevel@tonic-gate 		}
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	return ((char *)NULL);
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * _kva_free(): Free up memory.
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate void
71*7c478bd9Sstevel@tonic-gate _kva_free(kva_t *kva)
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	int	i;
74*7c478bd9Sstevel@tonic-gate 	kv_t	*data;
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
77*7c478bd9Sstevel@tonic-gate 		return;
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 	data = kva->data;
80*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
81*7c478bd9Sstevel@tonic-gate 		if (data[i].key != NULL) {
82*7c478bd9Sstevel@tonic-gate 			free(data[i].key);
83*7c478bd9Sstevel@tonic-gate 			data[i].key = NULL;
84*7c478bd9Sstevel@tonic-gate 		}
85*7c478bd9Sstevel@tonic-gate 		if (data[i].value != NULL) {
86*7c478bd9Sstevel@tonic-gate 			free(data[i].value);
87*7c478bd9Sstevel@tonic-gate 			data[i].value = NULL;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 	}
90*7c478bd9Sstevel@tonic-gate 	free(kva->data);
91*7c478bd9Sstevel@tonic-gate 	free(kva);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate  * new_kva(): Allocate a key-value array.
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate kva_t  *
98*7c478bd9Sstevel@tonic-gate _new_kva(int size)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	kva_t	*new_kva;
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	if ((new_kva = (kva_t *)calloc(1, sizeof (kva_t))) == NULL) {
103*7c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 	if ((new_kva->data = (kv_t *)calloc(1, (size*sizeof (kv_t)))) == NULL) {
106*7c478bd9Sstevel@tonic-gate 		free(new_kva);
107*7c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	return (new_kva);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate /*
114*7c478bd9Sstevel@tonic-gate  * _str2kva(): Given a string (s) of key-value pairs, separated by delimeter
115*7c478bd9Sstevel@tonic-gate  * (del), place the values into the key value array (nkva).
116*7c478bd9Sstevel@tonic-gate  */
117*7c478bd9Sstevel@tonic-gate kva_t  *
118*7c478bd9Sstevel@tonic-gate _str2kva(char *s, char *ass, char *del)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	int	n = 0;
121*7c478bd9Sstevel@tonic-gate 	int	m;
122*7c478bd9Sstevel@tonic-gate 	int	size = KV_ADD_KEYS;
123*7c478bd9Sstevel@tonic-gate 	char	*buf;
124*7c478bd9Sstevel@tonic-gate 	char	*p;
125*7c478bd9Sstevel@tonic-gate 	char	*pair;
126*7c478bd9Sstevel@tonic-gate 	char	*key;
127*7c478bd9Sstevel@tonic-gate 	char	*last_pair;
128*7c478bd9Sstevel@tonic-gate 	char	*last_key;
129*7c478bd9Sstevel@tonic-gate 	kv_t	*data;
130*7c478bd9Sstevel@tonic-gate 	kva_t	*nkva;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if (s == NULL ||
133*7c478bd9Sstevel@tonic-gate 	    ass == NULL ||
134*7c478bd9Sstevel@tonic-gate 	    del == NULL ||
135*7c478bd9Sstevel@tonic-gate 	    *s == '\0' ||
136*7c478bd9Sstevel@tonic-gate 	    *s == '\n' ||
137*7c478bd9Sstevel@tonic-gate 	    (strlen(s) <= 1)) {
138*7c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
139*7c478bd9Sstevel@tonic-gate 	}
140*7c478bd9Sstevel@tonic-gate 	p = s;
141*7c478bd9Sstevel@tonic-gate 	while ((p = _strpbrk_escape(p, ass)) != NULL) {
142*7c478bd9Sstevel@tonic-gate 		n++;
143*7c478bd9Sstevel@tonic-gate 		p++;
144*7c478bd9Sstevel@tonic-gate 	}
145*7c478bd9Sstevel@tonic-gate 	if (n > size) {
146*7c478bd9Sstevel@tonic-gate 		m = n/size;
147*7c478bd9Sstevel@tonic-gate 		if (n%size) {
148*7c478bd9Sstevel@tonic-gate 			++m;
149*7c478bd9Sstevel@tonic-gate 		}
150*7c478bd9Sstevel@tonic-gate 		size = m * KV_ADD_KEYS;
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
153*7c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 	data = nkva->data;
156*7c478bd9Sstevel@tonic-gate 	nkva->length = 0;
157*7c478bd9Sstevel@tonic-gate 	if ((buf = strdup(s)) == NULL) {
158*7c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 	pair = _strtok_escape(buf, del, &last_pair);
161*7c478bd9Sstevel@tonic-gate 	do {
162*7c478bd9Sstevel@tonic-gate 		key = _strtok_escape(pair, ass, &last_key);
163*7c478bd9Sstevel@tonic-gate 		if (key != NULL) {
164*7c478bd9Sstevel@tonic-gate 			data[nkva->length].key = _do_unescape(key);
165*7c478bd9Sstevel@tonic-gate 			data[nkva->length].value = _do_unescape(last_key);
166*7c478bd9Sstevel@tonic-gate 			nkva->length++;
167*7c478bd9Sstevel@tonic-gate 		}
168*7c478bd9Sstevel@tonic-gate 	} while ((pair = _strtok_escape(NULL, del, &last_pair)) != NULL);
169*7c478bd9Sstevel@tonic-gate 	free(buf);
170*7c478bd9Sstevel@tonic-gate 	return (nkva);
171*7c478bd9Sstevel@tonic-gate }
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate /*
174*7c478bd9Sstevel@tonic-gate  * _kva2str(): Given an array of key-value pairs, place them into a string
175*7c478bd9Sstevel@tonic-gate  * (buf). Use delimeter (del) to separate pairs.  Use assignment character
176*7c478bd9Sstevel@tonic-gate  * (ass) to separate keys and values.
177*7c478bd9Sstevel@tonic-gate  *
178*7c478bd9Sstevel@tonic-gate  * Return Values: 0  Success 1  Buffer too small 2  Out of memory
179*7c478bd9Sstevel@tonic-gate  */
180*7c478bd9Sstevel@tonic-gate int
181*7c478bd9Sstevel@tonic-gate _kva2str(kva_t *kva, char *buf, int buflen, char *ass, char *del)
182*7c478bd9Sstevel@tonic-gate {
183*7c478bd9Sstevel@tonic-gate 	int	i;
184*7c478bd9Sstevel@tonic-gate 	int	length = 0;
185*7c478bd9Sstevel@tonic-gate 	char	*tmp;
186*7c478bd9Sstevel@tonic-gate 	kv_t	*data;
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
189*7c478bd9Sstevel@tonic-gate 		return (0);
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	data = kva->data;
192*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
193*7c478bd9Sstevel@tonic-gate 		if (data[i].value != NULL) {
194*7c478bd9Sstevel@tonic-gate 			length += 2 + strlen(data[i].value);
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 	if (length > buflen) {
198*7c478bd9Sstevel@tonic-gate 		return (1);
199*7c478bd9Sstevel@tonic-gate 	}
200*7c478bd9Sstevel@tonic-gate 	(void) memset(buf, 0, buflen);
201*7c478bd9Sstevel@tonic-gate 	if ((tmp = (char *)malloc(buflen)) == NULL) {
202*7c478bd9Sstevel@tonic-gate 		return (2);
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
205*7c478bd9Sstevel@tonic-gate 		if (data[i].value != NULL) {
206*7c478bd9Sstevel@tonic-gate 			if (snprintf(tmp, buflen, "%s%s%s%s",
207*7c478bd9Sstevel@tonic-gate 			    data[i].key, ass, data[i].value, del) >= buflen) {
208*7c478bd9Sstevel@tonic-gate 				return (0);
209*7c478bd9Sstevel@tonic-gate 			}
210*7c478bd9Sstevel@tonic-gate 			(void) strcat(buf, tmp);
211*7c478bd9Sstevel@tonic-gate 		}
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 	return (0);
214*7c478bd9Sstevel@tonic-gate }
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate int
217*7c478bd9Sstevel@tonic-gate _insert2kva(kva_t *kva, char *key, char *value)
218*7c478bd9Sstevel@tonic-gate {
219*7c478bd9Sstevel@tonic-gate 	int	i;
220*7c478bd9Sstevel@tonic-gate 	kv_t	*data;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
223*7c478bd9Sstevel@tonic-gate 		return (0);
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 	data = kva->data;
226*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
227*7c478bd9Sstevel@tonic-gate 		if (strcmp(data[i].key, key) == 0) {
228*7c478bd9Sstevel@tonic-gate 			if (data[i].value != NULL)
229*7c478bd9Sstevel@tonic-gate 				free(data[i].value);
230*7c478bd9Sstevel@tonic-gate 			data[i].value = _strdup_null(value);
231*7c478bd9Sstevel@tonic-gate 			return (0);
232*7c478bd9Sstevel@tonic-gate 		}
233*7c478bd9Sstevel@tonic-gate 	}
234*7c478bd9Sstevel@tonic-gate 	return (1);
235*7c478bd9Sstevel@tonic-gate }
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate kva_t  *
238*7c478bd9Sstevel@tonic-gate _kva_dup(kva_t *old_kva)
239*7c478bd9Sstevel@tonic-gate {
240*7c478bd9Sstevel@tonic-gate 	int	i;
241*7c478bd9Sstevel@tonic-gate 	int	size;
242*7c478bd9Sstevel@tonic-gate 	kv_t	*old_data;
243*7c478bd9Sstevel@tonic-gate 	kv_t	*new_data;
244*7c478bd9Sstevel@tonic-gate 	kva_t 	*nkva = (kva_t *)NULL;
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (old_kva == NULL) {
247*7c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 	old_data = old_kva->data;
250*7c478bd9Sstevel@tonic-gate 	size = old_kva->length;
251*7c478bd9Sstevel@tonic-gate 	if ((nkva = _new_kva(size)) == NULL) {
252*7c478bd9Sstevel@tonic-gate 		return ((kva_t *)NULL);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 	new_data = nkva->data;
255*7c478bd9Sstevel@tonic-gate 	nkva->length = old_kva->length;
256*7c478bd9Sstevel@tonic-gate 	for (i = 0; i <= nkva->length; i++) {
257*7c478bd9Sstevel@tonic-gate 		new_data[i].key = _strdup_null(old_data[i].key);
258*7c478bd9Sstevel@tonic-gate 		new_data[i].value = _strdup_null(old_data[i].value);
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 	return (nkva);
262*7c478bd9Sstevel@tonic-gate }
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate static void
265*7c478bd9Sstevel@tonic-gate strip_spaces(char **valuep)
266*7c478bd9Sstevel@tonic-gate {
267*7c478bd9Sstevel@tonic-gate 	char *p, *start;
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	/* Find first non-white space character and return pointer to it */
270*7c478bd9Sstevel@tonic-gate 	for (p = *valuep; *p != '\0' && isspace((unsigned char)*p); p++)
271*7c478bd9Sstevel@tonic-gate 		;
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate 	*valuep = start = p;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	if (*p == '\0')
276*7c478bd9Sstevel@tonic-gate 		return;
277*7c478bd9Sstevel@tonic-gate 
278*7c478bd9Sstevel@tonic-gate 	p = p + strlen(p) - 1;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	/* Remove trailing spaces */
281*7c478bd9Sstevel@tonic-gate 	while (p > start && isspace((unsigned char)*p))
282*7c478bd9Sstevel@tonic-gate 		p--;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	p[1] = '\0';
285*7c478bd9Sstevel@tonic-gate }
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate char *
288*7c478bd9Sstevel@tonic-gate _do_unescape(char *src)
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
291*7c478bd9Sstevel@tonic-gate 	char *dst = NULL;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	if (src == NULL) {
294*7c478bd9Sstevel@tonic-gate 		dst = _strdup_null(src);
295*7c478bd9Sstevel@tonic-gate 	} else {
296*7c478bd9Sstevel@tonic-gate 		strip_spaces(&src);
297*7c478bd9Sstevel@tonic-gate 		tmp = _unescape(src, "=;:,\\");
298*7c478bd9Sstevel@tonic-gate 		dst = (tmp == NULL) ? _strdup_null(src) : tmp;
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	return (dst);
302*7c478bd9Sstevel@tonic-gate }
303*7c478bd9Sstevel@tonic-gate 
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate /*
306*7c478bd9Sstevel@tonic-gate  * Some utilities for handling comma-separated lists.
307*7c478bd9Sstevel@tonic-gate  */
308*7c478bd9Sstevel@tonic-gate char *
309*7c478bd9Sstevel@tonic-gate _argv_to_csl(char **strings)
310*7c478bd9Sstevel@tonic-gate {
311*7c478bd9Sstevel@tonic-gate 	int len = 0;
312*7c478bd9Sstevel@tonic-gate 	int i = 0;
313*7c478bd9Sstevel@tonic-gate 	char *newstr = (char *)NULL;
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	if (strings == NULL)
316*7c478bd9Sstevel@tonic-gate 		return ((char *)NULL);
317*7c478bd9Sstevel@tonic-gate 	for (i = 0; strings[i] != NULL; i++) {
318*7c478bd9Sstevel@tonic-gate 		len += strlen(strings[i]) + 1;
319*7c478bd9Sstevel@tonic-gate 	}
320*7c478bd9Sstevel@tonic-gate 	if ((newstr = (char *)malloc(len + 1)) == NULL) {
321*7c478bd9Sstevel@tonic-gate 		return ((char *)NULL);
322*7c478bd9Sstevel@tonic-gate 	}
323*7c478bd9Sstevel@tonic-gate 	(void) memset(newstr, 0, len);
324*7c478bd9Sstevel@tonic-gate 	for (i = 0; strings[i] != NULL; i++) {
325*7c478bd9Sstevel@tonic-gate 		(void) strcat(newstr, strings[i]);
326*7c478bd9Sstevel@tonic-gate 		(void) strcat(newstr, ",");
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 	newstr[len-1] = NULL;
329*7c478bd9Sstevel@tonic-gate 	return (newstr);
330*7c478bd9Sstevel@tonic-gate }
331*7c478bd9Sstevel@tonic-gate 
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate char **
334*7c478bd9Sstevel@tonic-gate _csl_to_argv(char *csl)
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate 	int len = 0;
337*7c478bd9Sstevel@tonic-gate 	int ncommas = 0;
338*7c478bd9Sstevel@tonic-gate 	int i = 0;
339*7c478bd9Sstevel@tonic-gate 	char **spc = (char **)NULL;
340*7c478bd9Sstevel@tonic-gate 	char *copy = (char *)NULL;
341*7c478bd9Sstevel@tonic-gate 	char *pc;
342*7c478bd9Sstevel@tonic-gate 	char *lasts = (char *)NULL;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	len = strlen(csl);
345*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
346*7c478bd9Sstevel@tonic-gate 		if (csl[i] == ',')
347*7c478bd9Sstevel@tonic-gate 			ncommas++;
348*7c478bd9Sstevel@tonic-gate 	}
349*7c478bd9Sstevel@tonic-gate 	if ((spc = (char **)malloc((ncommas + 2) * sizeof (char *))) == NULL) {
350*7c478bd9Sstevel@tonic-gate 		return ((char **)NULL);
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate 	copy = strdup(csl);
353*7c478bd9Sstevel@tonic-gate 	for (pc = strtok_r(copy, ",", &lasts), i = 0; pc != NULL;
354*7c478bd9Sstevel@tonic-gate 	    pc = strtok_r(NULL, ",", &lasts), i++) {
355*7c478bd9Sstevel@tonic-gate 		spc[i] = strdup(pc);
356*7c478bd9Sstevel@tonic-gate 	}
357*7c478bd9Sstevel@tonic-gate 	spc[i] = NULL;
358*7c478bd9Sstevel@tonic-gate 	free(copy);
359*7c478bd9Sstevel@tonic-gate 	return (spc);
360*7c478bd9Sstevel@tonic-gate }
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate void
364*7c478bd9Sstevel@tonic-gate _free_argv(char **p_argv)
365*7c478bd9Sstevel@tonic-gate {
366*7c478bd9Sstevel@tonic-gate 	char **p_a;
367*7c478bd9Sstevel@tonic-gate 
368*7c478bd9Sstevel@tonic-gate 	for (p_a = p_argv; *p_a != NULL; p_a++)
369*7c478bd9Sstevel@tonic-gate 		free(*p_a);
370*7c478bd9Sstevel@tonic-gate 	free(p_argv);
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
375*7c478bd9Sstevel@tonic-gate void
376*7c478bd9Sstevel@tonic-gate print_kva(kva_t *kva)
377*7c478bd9Sstevel@tonic-gate {
378*7c478bd9Sstevel@tonic-gate 	int	i;
379*7c478bd9Sstevel@tonic-gate 	kv_t	*data;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	if (kva == NULL) {
382*7c478bd9Sstevel@tonic-gate 		printf("  (empty)\n");
383*7c478bd9Sstevel@tonic-gate 		return;
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 	data = kva->data;
386*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < kva->length; i++) {
387*7c478bd9Sstevel@tonic-gate 		printf("  %s = %s\n", data[i].key, data[i].value);
388*7c478bd9Sstevel@tonic-gate 	}
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
391