9 /* $Id: compat_stringlist.c,v 1.6 2015/11/07 14:22:29 schwarze Exp $ */
11 * Copyright (c) 1994 Christos Zoulas <christos@netbsd.org>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #include "compat_stringlist.h"
43 #define _SL_CHUNKSIZE 20
46 * sl_init(): Initialize a string list
53 sl = malloc(sizeof(StringList));
58 sl->sl_max = _SL_CHUNKSIZE;
59 sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
60 if (sl->sl_str == NULL)
67 * sl_add(): Add an item to the string list
70 sl_add(StringList *sl, char *name)
72 if (sl->sl_cur == sl->sl_max - 1) {
73 sl->sl_max += _SL_CHUNKSIZE;
74 sl->sl_str = reallocarray(sl->sl_str,
75 sl->sl_max, sizeof(char *));
76 if (sl->sl_str == NULL)
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];