xref: /netbsd-src/lib/libc/gen/stringlist.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: stringlist.c,v 1.11 2006/07/27 15:36: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.11 2006/07/27 15:36: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 #endif
59 
60 #define _SL_CHUNKSIZE	20
61 
62 /*
63  * sl_init(): Initialize a string list
64  */
65 StringList *
66 sl_init(void)
67 {
68 	StringList *sl;
69 
70 	sl = malloc(sizeof(StringList));
71 	if (sl == NULL)
72 		return NULL;
73 
74 	sl->sl_cur = 0;
75 	sl->sl_max = _SL_CHUNKSIZE;
76 	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
77 	if (sl->sl_str == NULL) {
78 		free(sl);
79 		sl = NULL;
80 	}
81 	return sl;
82 }
83 
84 
85 /*
86  * sl_add(): Add an item to the string list
87  */
88 int
89 sl_add(StringList *sl, char *name)
90 {
91 
92 	_DIAGASSERT(sl != NULL);
93 
94 	if (sl->sl_cur == sl->sl_max - 1) {
95 		char	**new;
96 
97 		new = realloc(sl->sl_str,
98 		    (sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *));
99 		if (new == NULL)
100 			return -1;
101 		sl->sl_max += _SL_CHUNKSIZE;
102 		sl->sl_str = new;
103 	}
104 	sl->sl_str[sl->sl_cur++] = name;
105 	return 0;
106 }
107 
108 
109 /*
110  * sl_free(): Free a stringlist
111  */
112 void
113 sl_free(StringList *sl, int all)
114 {
115 	size_t i;
116 
117 	if (sl == NULL)
118 		return;
119 	if (sl->sl_str) {
120 		if (all)
121 			for (i = 0; i < sl->sl_cur; i++)
122 				free(sl->sl_str[i]);
123 		free(sl->sl_str);
124 	}
125 	free(sl);
126 }
127 
128 
129 /*
130  * sl_find(): Find a name in the string list
131  */
132 char *
133 sl_find(StringList *sl, const char *name)
134 {
135 	size_t i;
136 
137 	_DIAGASSERT(sl != NULL);
138 
139 	for (i = 0; i < sl->sl_cur; i++)
140 		if (strcmp(sl->sl_str[i], name) == 0)
141 			return sl->sl_str[i];
142 
143 	return NULL;
144 }
145 
146 int
147 sl_delete(StringList *sl, const char *name, int all)
148 {
149 	size_t i, j;
150 
151 	for (i = 0; i < sl->sl_cur; i++)
152 		if (strcmp(sl->sl_str[i], name) == 0) {
153 			if (all)
154 				free(sl->sl_str[i]);
155 			for (j = i + 1; j < sl->sl_cur; j++)
156 				sl->sl_str[j - 1] = sl->sl_str[j];
157 			sl->sl_str[--sl->sl_cur] = NULL;
158 			return 0;
159 		}
160 	return -1;
161 }
162 
163