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