1/*****************************************************************
2**
3**	@(#) strlist.c (c) Mar 2005  Holger Zuleger
4**
5**	TODO:	Maybe we should use a special type for the list:
6**		typedef struct { char cnt; char list[0+1]; } strlist__t;
7**		This results in better type control of the function parameters
8**
9**	Copyright (c) Mar 2005, Holger Zuleger HZnet. All rights reserved.
10**
11**	This software is open source.
12**
13**	Redistribution and use in source and binary forms, with or without
14**	modification, are permitted provided that the following conditions
15**	are met:
16**
17**	Redistributions of source code must retain the above copyright notice,
18**	this list of conditions and the following disclaimer.
19**
20**	Redistributions in binary form must reproduce the above copyright notice,
21**	this list of conditions and the following disclaimer in the documentation
22**	and/or other materials provided with the distribution.
23**
24**	Neither the name of Holger Zuleger HZnet nor the names of its contributors may
25**	be used to endorse or promote products derived from this software without
26**	specific prior written permission.
27**
28**	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29**	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30**	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31**	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
32**	LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33**	CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34**	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35**	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36**	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37**	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38**	POSSIBILITY OF SUCH DAMAGE.
39**
40*****************************************************************/
41
42#ifdef TEST
43# include <stdio.h>
44#endif
45#include <string.h>
46#include <stdlib.h>
47#include "strlist.h"
48
49
50/*****************************************************************
51**	prepstrlist (str, delim)
52**	prepare a string with delimiters to a so called strlist.
53**	'str' is a list of substrings delimeted by 'delim'
54**	The # of strings is stored at the first byte of the allocated
55**	memory. Every substring is stored as a '\0' terminated C-String.
56**	The function returns a pointer to dynamic allocated memory
57*****************************************************************/
58char	*prepstrlist (const char *str, const char *delim)
59{
60	char	*p;
61	char	*new;
62	int	len;
63	int	cnt;
64
65	if ( str == NULL )
66		return NULL;
67
68	len = strlen (str);
69	if ( (new = malloc (len + 2)) == NULL )
70		return new;
71
72	cnt = 0;
73	p = new;
74	for ( *p++ = '\0'; *str; str++ )
75	{
76		if ( strchr (delim, *str) == NULL )
77			*p++ = *str;
78		else if ( p[-1] != '\0' )
79		{
80			*p++ = '\0';
81			cnt++;
82		}
83	}
84	*p = '\0';	/*terminate string */
85	if ( p[-1] != '\0' )
86		cnt++;
87	*new = cnt & 0xFF;
88
89	return new;
90}
91
92/*****************************************************************
93**	isinlist (str, list)
94**	check if 'list' contains 'str'
95*****************************************************************/
96int	isinlist (const char *str, const char *list)
97{
98	int	cnt;
99
100	if ( list == NULL || *list == '\0' )
101		return 1;
102	if ( str == NULL || *str == '\0' )
103		return 0;
104
105	cnt = *list;
106	while ( cnt-- > 0 )
107	{
108		list++;
109		if ( strcmp (str, list) == 0 )
110			return 1;
111		list += strlen (list);
112	}
113
114	return 0;
115}
116
117/*****************************************************************
118**	unprepstrlist (list, delimc)
119*****************************************************************/
120char	*unprepstrlist (char *list, char delimc)
121{
122	char	*p;
123	int	cnt;
124
125	cnt = *list & 0xFF;
126	p = list;
127	for ( *p++ = delimc; cnt > 1; p++ )
128		if ( *p == '\0' )
129		{
130			*p = delimc;
131			cnt--;
132		}
133
134	return list;
135}
136
137#ifdef TEST
138main (int argc, char *argv[])
139{
140	FILE	*fp;
141	char	*p;
142	char	*searchlist = NULL;
143	char	group[255];
144
145	if ( argc > 1 )
146		searchlist = prepstrlist (argv[1], LISTDELIM);
147
148	printf ("searchlist: %d entrys: \n", searchlist[0]);
149	if ( (fp = fopen ("/etc/group", "r")) == NULL )
150		exit (fprintf (stderr, "can't open file\n"));
151
152	while ( fscanf (fp, "%[^:]:%*[^\n]\n", group) != EOF )
153		if ( isinlist (group, searchlist) )
154			printf ("%s\n", group);
155
156	fclose (fp);
157
158	printf ("searchlist: \"%s\"\n", unprepstrlist  (searchlist, *LISTDELIM));
159	for ( p = searchlist; *p; p++ )
160		if ( *p < 32 )
161			printf ("<%d>", *p);
162		else
163			printf ("%c", *p);
164	printf ("\n");
165}
166#endif
167