1 /* $NetBSD: strlist.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
5 ** @(#) strlist.c (c) Mar 2005 Holger Zuleger
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
11 ** Copyright (c) Mar 2005, Holger Zuleger HZnet. All rights reserved.
13 ** This software is open source.
15 ** Redistribution and use in source and binary forms, with or without
16 ** modification, are permitted provided that the following conditions
19 ** Redistributions of source code must retain the above copyright notice,
20 ** this list of conditions and the following disclaimer.
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.
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.
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.
42 *****************************************************************/
52 /*****************************************************************
53 ** prepstrlist (str, delim)
54 ** prepare a string with delimiters to a so called strlist.
55 ** 'str' is a list of substrings delimited 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 *****************************************************************/
60 char *prepstrlist (const char *str
, const char *delim
)
71 if ( (new = malloc (len
+ 2)) == NULL
)
76 for ( *p
++ = '\0'; *str
; str
++ )
78 if ( strchr (delim
, *str
) == NULL
)
80 else if ( p
[-1] != '\0' )
86 *p
= '\0'; /*terminate string */
94 /*****************************************************************
95 ** isinlist (str, list)
96 ** check if 'list' contains 'str'
97 *****************************************************************/
98 int isinlist (const char *str
, const char *list
)
102 if ( list
== NULL
|| *list
== '\0' )
104 if ( str
== NULL
|| *str
== '\0' )
111 if ( strcmp (str
, list
) == 0 )
113 list
+= strlen (list
);
119 /*****************************************************************
120 ** unprepstrlist (list, delimc)
121 *****************************************************************/
122 char *unprepstrlist (char *list
, char delimc
)
129 for ( *p
++ = delimc
; cnt
> 1; p
++ )
140 main (int argc
, char *argv
[])
144 char *searchlist
= NULL
;
148 searchlist
= prepstrlist (argv
[1], LISTDELIM
);
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"));
154 while ( fscanf (fp
, "%[^:]:%*[^\n]\n", group
) != EOF
)
155 if ( isinlist (group
, searchlist
) )
156 printf ("%s\n", group
);
160 printf ("searchlist: \"%s\"\n", unprepstrlist (searchlist
, *LISTDELIM
));
161 for ( p
= searchlist
; *p
; p
++ )