1 /* $Id: compat_stringlist.c,v 1.8 2020/06/15 21:48:09 schwarze Exp $ */
2 /* $NetBSD: stringlist.c,v 1.14 2015/05/21 01:29:13 christos Exp $ */
5 * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
36 #include "compat_stringlist.h"
38 #define _SL_CHUNKSIZE 20
41 * sl_init(): Initialize a string list
48 sl = malloc(sizeof(StringList));
53 sl->sl_max = _SL_CHUNKSIZE;
54 sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
55 if (sl->sl_str == NULL) {
64 * sl_add(): Add an item to the string list
67 sl_add(StringList *sl, char *name)
69 if (sl->sl_cur == sl->sl_max - 1) {
72 new = reallocarray(sl->sl_str, (sl->sl_max + _SL_CHUNKSIZE),
76 sl->sl_max += _SL_CHUNKSIZE;
79 sl->sl_str[sl->sl_cur++] = name;
85 * sl_free(): Free a stringlist
88 sl_free(StringList *sl, int all)
96 for (i = 0; i < sl->sl_cur; i++)
105 * sl_find(): Find a name in the string list
108 sl_find(StringList *sl, const char *name)
112 for (i = 0; i < sl->sl_cur; i++)
113 if (strcmp(sl->sl_str[i], name) == 0)
114 return sl->sl_str[i];
120 sl_delete(StringList *sl, const char *name, int all)
124 for (i = 0; i < sl->sl_cur; i++)
125 if (strcmp(sl->sl_str[i], name) == 0) {
128 for (j = i + 1; j < sl->sl_cur; j++)
129 sl->sl_str[j - 1] = sl->sl_str[j];
130 sl->sl_str[--sl->sl_cur] = NULL;