xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/xlist.c (revision 5562:0f12179b71ab)
1*5562Sjp161948 /*
2*5562Sjp161948  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3*5562Sjp161948  * Use is subject to license terms.
4*5562Sjp161948  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
80Sstevel@tonic-gate #include <stdio.h>
90Sstevel@tonic-gate #include <strings.h>
100Sstevel@tonic-gate #include "xmalloc.h"
110Sstevel@tonic-gate #include "xlist.h"
120Sstevel@tonic-gate 
130Sstevel@tonic-gate char **
xsplit(char * list,char sep)140Sstevel@tonic-gate xsplit(char *list, char sep)
150Sstevel@tonic-gate {
16*5562Sjp161948 	char **a;
17*5562Sjp161948 	char *p, *q;
18*5562Sjp161948 	uint_t n = 0;
190Sstevel@tonic-gate 
20*5562Sjp161948 	for (n = 0, p = list; p && *p; ) {
21*5562Sjp161948 		while (p && *p && *p == sep)
22*5562Sjp161948 			p++;
23*5562Sjp161948 		if (!*p)
24*5562Sjp161948 			break;
25*5562Sjp161948 		n++;
26*5562Sjp161948 		p = strchr(p, sep);
27*5562Sjp161948 	}
28*5562Sjp161948 	a = (char **)xmalloc(sizeof (char *) * (n + 2));
29*5562Sjp161948 	for (n = 0, p = list; p && *p; ) {
30*5562Sjp161948 		while (*p == sep)
31*5562Sjp161948 			p++;
32*5562Sjp161948 		if (!*p)
33*5562Sjp161948 			break;
34*5562Sjp161948 		q = strchr(p, sep);
35*5562Sjp161948 		if (!q)
36*5562Sjp161948 			q = p + strlen(p);
37*5562Sjp161948 		a[n] = (char *)xmalloc((q - p + 2));
38*5562Sjp161948 		(void) strncpy(a[n], p, q - p);
39*5562Sjp161948 		a[n][q - p] = '\0';
40*5562Sjp161948 		n++;
41*5562Sjp161948 		if (!*q)
42*5562Sjp161948 			break;
43*5562Sjp161948 		p = q + 1;
44*5562Sjp161948 	}
45*5562Sjp161948 	a[n] = NULL;
46*5562Sjp161948 	return (a);
470Sstevel@tonic-gate }
480Sstevel@tonic-gate 
490Sstevel@tonic-gate void
xfree_split_list(char ** list)500Sstevel@tonic-gate xfree_split_list(char **list)
510Sstevel@tonic-gate {
52*5562Sjp161948 	char **p;
53*5562Sjp161948 	for (p = list; p && *p; p++) {
54*5562Sjp161948 		xfree(*p);
55*5562Sjp161948 	}
56*5562Sjp161948 	xfree(list);
570Sstevel@tonic-gate }
580Sstevel@tonic-gate 
590Sstevel@tonic-gate char *
xjoin(char ** alist,char sep)600Sstevel@tonic-gate xjoin(char **alist, char sep)
610Sstevel@tonic-gate {
62*5562Sjp161948 	char **p;
63*5562Sjp161948 	char *list;
64*5562Sjp161948 	char sep_str[2];
65*5562Sjp161948 	uint_t n;
660Sstevel@tonic-gate 
67*5562Sjp161948 	for (n = 1, p = alist; p && *p; p++) {
68*5562Sjp161948 		if (!*p || !**p)
69*5562Sjp161948 			continue;
70*5562Sjp161948 		n += strlen(*p) + 1;
71*5562Sjp161948 	}
72*5562Sjp161948 	list = (char *)xmalloc(n);
73*5562Sjp161948 	*list = '\0';
740Sstevel@tonic-gate 
75*5562Sjp161948 	sep_str[0] = sep;
76*5562Sjp161948 	sep_str[1] = '\0';
77*5562Sjp161948 	for (p = alist; p && *p; p++) {
78*5562Sjp161948 		if (!*p || !**p)
79*5562Sjp161948 			continue;
80*5562Sjp161948 		if (*list != '\0')
81*5562Sjp161948 			(void) strlcat(list, sep_str, n);
82*5562Sjp161948 		(void) strlcat(list, *p, n);
83*5562Sjp161948 	}
84*5562Sjp161948 	return (list);
850Sstevel@tonic-gate }
86