etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / zkt-1.1.3 / strlist.c
blob8dab3ee1bcf047f0d321d0ecffd4e1007b940f71
1 /* $NetBSD: strlist.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
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
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
17 ** are met:
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 *****************************************************************/
44 #ifdef TEST
45 # include <stdio.h>
46 #endif
47 #include <string.h>
48 #include <stdlib.h>
49 #include "strlist.h"
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)
62 char *p;
63 char *new;
64 int len;
65 int cnt;
67 if ( str == NULL )
68 return NULL;
70 len = strlen (str);
71 if ( (new = malloc (len + 2)) == NULL )
72 return new;
74 cnt = 0;
75 p = new;
76 for ( *p++ = '\0'; *str; str++ )
78 if ( strchr (delim, *str) == NULL )
79 *p++ = *str;
80 else if ( p[-1] != '\0' )
82 *p++ = '\0';
83 cnt++;
86 *p = '\0'; /*terminate string */
87 if ( p[-1] != '\0' )
88 cnt++;
89 *new = cnt & 0xFF;
91 return new;
94 /*****************************************************************
95 ** isinlist (str, list)
96 ** check if 'list' contains 'str'
97 *****************************************************************/
98 int isinlist (const char *str, const char *list)
100 int cnt;
102 if ( list == NULL || *list == '\0' )
103 return 1;
104 if ( str == NULL || *str == '\0' )
105 return 0;
107 cnt = *list;
108 while ( cnt-- > 0 )
110 list++;
111 if ( strcmp (str, list) == 0 )
112 return 1;
113 list += strlen (list);
116 return 0;
119 /*****************************************************************
120 ** unprepstrlist (list, delimc)
121 *****************************************************************/
122 char *unprepstrlist (char *list, char delimc)
124 char *p;
125 int cnt;
127 cnt = *list & 0xFF;
128 p = list;
129 for ( *p++ = delimc; cnt > 1; p++ )
130 if ( *p == '\0' )
132 *p = delimc;
133 cnt--;
136 return list;
139 #ifdef TEST
140 main (int argc, char *argv[])
142 FILE *fp;
143 char *p;
144 char *searchlist = NULL;
145 char group[255];
147 if ( argc > 1 )
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);
158 fclose (fp);
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");
168 #endif