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