stringlist.c revision 1.3 1 1.3 christos /* $NetBSD: stringlist.c,v 1.3 1997/07/13 19:31:49 christos Exp $ */
2 1.2 lukem
3 1.2 lukem /*
4 1.2 lukem * Copyright (c) 1994 Christos Zoulas
5 1.2 lukem * All rights reserved.
6 1.2 lukem *
7 1.2 lukem * Redistribution and use in source and binary forms, with or without
8 1.2 lukem * modification, are permitted provided that the following conditions
9 1.2 lukem * are met:
10 1.2 lukem * 1. Redistributions of source code must retain the above copyright
11 1.2 lukem * notice, this list of conditions and the following disclaimer.
12 1.2 lukem * 2. Redistributions in binary form must reproduce the above copyright
13 1.2 lukem * notice, this list of conditions and the following disclaimer in the
14 1.2 lukem * documentation and/or other materials provided with the distribution.
15 1.2 lukem * 3. All advertising materials mentioning features or use of this software
16 1.2 lukem * must display the following acknowledgement:
17 1.2 lukem * This product includes software developed by Christos Zoulas.
18 1.2 lukem * 4. The name of the author may not be used to endorse or promote products
19 1.2 lukem * derived from this software without specific prior written permission.
20 1.2 lukem *
21 1.2 lukem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 1.2 lukem * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 1.2 lukem * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.2 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 1.2 lukem * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.2 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.2 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.2 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.2 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.2 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.2 lukem * SUCH DAMAGE.
32 1.2 lukem */
33 1.2 lukem
34 1.3 christos #include <sys/cdefs.h>
35 1.2 lukem #if defined(LIBC_SCCS) && !defined(lint)
36 1.3 christos __RCSID("$NetBSD: stringlist.c,v 1.3 1997/07/13 19:31:49 christos Exp $");
37 1.2 lukem #endif /* LIBC_SCCS and not lint */
38 1.2 lukem
39 1.3 christos #include "namespace.h"
40 1.2 lukem #include <stdio.h>
41 1.2 lukem #include <string.h>
42 1.2 lukem #include <err.h>
43 1.2 lukem #include <stdlib.h>
44 1.2 lukem #include <stringlist.h>
45 1.2 lukem
46 1.2 lukem #define _SL_CHUNKSIZE 20
47 1.2 lukem
48 1.2 lukem /*
49 1.2 lukem * sl_init(): Initialize a string list
50 1.2 lukem */
51 1.2 lukem StringList *
52 1.2 lukem sl_init()
53 1.2 lukem {
54 1.2 lukem StringList *sl = malloc(sizeof(StringList));
55 1.2 lukem if (sl == NULL)
56 1.3 christos err(1, "stringlist: %m");
57 1.2 lukem
58 1.2 lukem sl->sl_cur = 0;
59 1.2 lukem sl->sl_max = _SL_CHUNKSIZE;
60 1.2 lukem sl->sl_str = malloc(sl->sl_max * sizeof(char *));
61 1.2 lukem if (sl->sl_str == NULL)
62 1.3 christos err(1, "stringlist: %m");
63 1.2 lukem return sl;
64 1.2 lukem }
65 1.2 lukem
66 1.2 lukem
67 1.2 lukem /*
68 1.2 lukem * sl_add(): Add an item to the string list
69 1.2 lukem */
70 1.2 lukem void
71 1.2 lukem sl_add(sl, name)
72 1.2 lukem StringList *sl;
73 1.2 lukem char *name;
74 1.2 lukem {
75 1.2 lukem if (sl->sl_cur == sl->sl_max - 1) {
76 1.2 lukem sl->sl_max += _SL_CHUNKSIZE;
77 1.2 lukem sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof(char *));
78 1.2 lukem if (sl->sl_str == NULL)
79 1.3 christos err(1, "stringlist: %m");
80 1.2 lukem }
81 1.2 lukem sl->sl_str[sl->sl_cur++] = name;
82 1.2 lukem }
83 1.2 lukem
84 1.2 lukem
85 1.2 lukem /*
86 1.2 lukem * sl_free(): Free a stringlist
87 1.2 lukem */
88 1.2 lukem void
89 1.2 lukem sl_free(sl, all)
90 1.2 lukem StringList *sl;
91 1.2 lukem int all;
92 1.2 lukem {
93 1.2 lukem size_t i;
94 1.2 lukem
95 1.2 lukem if (sl == NULL)
96 1.2 lukem return;
97 1.2 lukem if (sl->sl_str) {
98 1.2 lukem if (all)
99 1.2 lukem for (i = 0; i < sl->sl_cur; i++)
100 1.2 lukem free(sl->sl_str[i]);
101 1.2 lukem free(sl->sl_str);
102 1.2 lukem }
103 1.2 lukem free(sl);
104 1.2 lukem }
105 1.2 lukem
106 1.2 lukem
107 1.2 lukem /*
108 1.2 lukem * sl_find(): Find a name in the string list
109 1.2 lukem */
110 1.2 lukem char *
111 1.2 lukem sl_find(sl, name)
112 1.2 lukem StringList *sl;
113 1.2 lukem char *name;
114 1.2 lukem {
115 1.2 lukem size_t i;
116 1.2 lukem
117 1.2 lukem for (i = 0; i < sl->sl_cur; i++)
118 1.2 lukem if (strcmp(sl->sl_str[i], name) == 0)
119 1.2 lukem return sl->sl_str[i];
120 1.2 lukem
121 1.2 lukem return NULL;
122 1.2 lukem }
123