xref: /netbsd-src/lib/libc/gen/stringlist.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: stringlist.c,v 1.12 2007/05/09 17:10:29 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
41 __RCSID("$NetBSD: stringlist.c,v 1.12 2007/05/09 17:10:29 christos Exp $");
42 #endif /* LIBC_SCCS and not lint */
43 
44 #include "namespace.h"
45 
46 #include <assert.h>
47 #include <err.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <stringlist.h>
52 
53 #ifdef __weak_alias
54 __weak_alias(sl_add,_sl_add)
55 __weak_alias(sl_find,_sl_find)
56 __weak_alias(sl_free,_sl_free)
57 __weak_alias(sl_init,_sl_init)
58 __weak_alias(sl_delete,_sl_delete)
59 #endif
60 
61 #define _SL_CHUNKSIZE	20
62 
63 /*
64  * sl_init(): Initialize a string list
65  */
66 StringList *
67 sl_init(void)
68 {
69 	StringList *sl;
70 
71 	sl = malloc(sizeof(StringList));
72 	if (sl == NULL)
73 		return NULL;
74 
75 	sl->sl_cur = 0;
76 	sl->sl_max = _SL_CHUNKSIZE;
77 	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
78 	if (sl->sl_str == NULL) {
79 		free(sl);
80 		sl = NULL;
81 	}
82 	return sl;
83 }
84 
85 
86 /*
87  * sl_add(): Add an item to the string list
88  */
89 int
90 sl_add(StringList *sl, char *name)
91 {
92 
93 	_DIAGASSERT(sl != NULL);
94 
95 	if (sl->sl_cur == sl->sl_max - 1) {
96 		char	**new;
97 
98 		new = realloc(sl->sl_str,
99 		    (sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *));
100 		if (new == NULL)
101 			return -1;
102 		sl->sl_max += _SL_CHUNKSIZE;
103 		sl->sl_str = new;
104 	}
105 	sl->sl_str[sl->sl_cur++] = name;
106 	return 0;
107 }
108 
109 
110 /*
111  * sl_free(): Free a stringlist
112  */
113 void
114 sl_free(StringList *sl, int all)
115 {
116 	size_t i;
117 
118 	if (sl == NULL)
119 		return;
120 	if (sl->sl_str) {
121 		if (all)
122 			for (i = 0; i < sl->sl_cur; i++)
123 				free(sl->sl_str[i]);
124 		free(sl->sl_str);
125 	}
126 	free(sl);
127 }
128 
129 
130 /*
131  * sl_find(): Find a name in the string list
132  */
133 char *
134 sl_find(StringList *sl, const char *name)
135 {
136 	size_t i;
137 
138 	_DIAGASSERT(sl != NULL);
139 
140 	for (i = 0; i < sl->sl_cur; i++)
141 		if (strcmp(sl->sl_str[i], name) == 0)
142 			return sl->sl_str[i];
143 
144 	return NULL;
145 }
146 
147 int
148 sl_delete(StringList *sl, const char *name, int all)
149 {
150 	size_t i, j;
151 
152 	for (i = 0; i < sl->sl_cur; i++)
153 		if (strcmp(sl->sl_str[i], name) == 0) {
154 			if (all)
155 				free(sl->sl_str[i]);
156 			for (j = i + 1; j < sl->sl_cur; j++)
157 				sl->sl_str[j - 1] = sl->sl_str[j];
158 			sl->sl_str[--sl->sl_cur] = NULL;
159 			return 0;
160 		}
161 	return -1;
162 }
163 
164