1 /* $OpenBSD: stringlist.c,v 1.14 2019/05/16 12:44:18 florian Exp $ */
2 /* $NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $ */
3
4 /*
5 * Copyright (c) 1994 Christos Zoulas
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifndef SMALL
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <err.h>
35 #include <stdlib.h>
36
37 #include "stringlist.h"
38
39 #define _SL_CHUNKSIZE 20
40
41 /*
42 * sl_init(): Initialize a string list
43 */
44 StringList *
sl_init(void)45 sl_init(void)
46 {
47 StringList *sl = malloc(sizeof(StringList));
48 if (sl == NULL)
49 err(1, "stringlist");
50
51 sl->sl_cur = 0;
52 sl->sl_max = _SL_CHUNKSIZE;
53 sl->sl_str = calloc(sl->sl_max, sizeof(char *));
54 if (sl->sl_str == NULL)
55 err(1, "stringlist");
56 return sl;
57 }
58
59
60 /*
61 * sl_add(): Add an item to the string list
62 */
63 void
sl_add(StringList * sl,char * name)64 sl_add(StringList *sl, char *name)
65 {
66 if (sl->sl_cur == sl->sl_max - 1) {
67 sl->sl_max += _SL_CHUNKSIZE;
68 sl->sl_str = reallocarray(sl->sl_str, sl->sl_max,
69 sizeof(char *));
70 if (sl->sl_str == NULL)
71 err(1, "stringlist");
72 }
73 sl->sl_str[sl->sl_cur++] = name;
74 }
75
76
77 /*
78 * sl_free(): Free a stringlist
79 */
80 void
sl_free(StringList * sl,int all)81 sl_free(StringList *sl, int all)
82 {
83 size_t i;
84
85 if (sl == NULL)
86 return;
87 if (sl->sl_str) {
88 if (all)
89 for (i = 0; i < sl->sl_cur; i++)
90 free(sl->sl_str[i]);
91 free(sl->sl_str);
92 }
93 free(sl);
94 }
95
96 #endif /* !SMALL */
97
98