stringlist.c revision 1.5 1 1.5 enami /* $NetBSD: stringlist.c,v 1.5 1997/09/29 06:52:40 enami 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.5 enami __RCSID("$NetBSD: stringlist.c,v 1.5 1997/09/29 06:52:40 enami 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.4 jtc
46 1.4 jtc #ifdef __weak_alias
47 1.4 jtc __weak_alias(sl_add,_sl_add);
48 1.4 jtc __weak_alias(sl_find,_sl_find);
49 1.4 jtc __weak_alias(sl_free,_sl_free);
50 1.4 jtc __weak_alias(sl_init,_sl_init);
51 1.4 jtc #endif
52 1.2 lukem
53 1.2 lukem #define _SL_CHUNKSIZE 20
54 1.2 lukem
55 1.2 lukem /*
56 1.2 lukem * sl_init(): Initialize a string list
57 1.2 lukem */
58 1.2 lukem StringList *
59 1.2 lukem sl_init()
60 1.2 lukem {
61 1.2 lukem StringList *sl = malloc(sizeof(StringList));
62 1.2 lukem if (sl == NULL)
63 1.5 enami err(1, "stringlist");
64 1.2 lukem
65 1.2 lukem sl->sl_cur = 0;
66 1.2 lukem sl->sl_max = _SL_CHUNKSIZE;
67 1.2 lukem sl->sl_str = malloc(sl->sl_max * sizeof(char *));
68 1.2 lukem if (sl->sl_str == NULL)
69 1.5 enami err(1, "stringlist");
70 1.2 lukem return sl;
71 1.2 lukem }
72 1.2 lukem
73 1.2 lukem
74 1.2 lukem /*
75 1.2 lukem * sl_add(): Add an item to the string list
76 1.2 lukem */
77 1.2 lukem void
78 1.2 lukem sl_add(sl, name)
79 1.2 lukem StringList *sl;
80 1.2 lukem char *name;
81 1.2 lukem {
82 1.2 lukem if (sl->sl_cur == sl->sl_max - 1) {
83 1.2 lukem sl->sl_max += _SL_CHUNKSIZE;
84 1.2 lukem sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof(char *));
85 1.2 lukem if (sl->sl_str == NULL)
86 1.5 enami err(1, "stringlist");
87 1.2 lukem }
88 1.2 lukem sl->sl_str[sl->sl_cur++] = name;
89 1.2 lukem }
90 1.2 lukem
91 1.2 lukem
92 1.2 lukem /*
93 1.2 lukem * sl_free(): Free a stringlist
94 1.2 lukem */
95 1.2 lukem void
96 1.2 lukem sl_free(sl, all)
97 1.2 lukem StringList *sl;
98 1.2 lukem int all;
99 1.2 lukem {
100 1.2 lukem size_t i;
101 1.2 lukem
102 1.2 lukem if (sl == NULL)
103 1.2 lukem return;
104 1.2 lukem if (sl->sl_str) {
105 1.2 lukem if (all)
106 1.2 lukem for (i = 0; i < sl->sl_cur; i++)
107 1.2 lukem free(sl->sl_str[i]);
108 1.2 lukem free(sl->sl_str);
109 1.2 lukem }
110 1.2 lukem free(sl);
111 1.2 lukem }
112 1.2 lukem
113 1.2 lukem
114 1.2 lukem /*
115 1.2 lukem * sl_find(): Find a name in the string list
116 1.2 lukem */
117 1.2 lukem char *
118 1.2 lukem sl_find(sl, name)
119 1.2 lukem StringList *sl;
120 1.2 lukem char *name;
121 1.2 lukem {
122 1.2 lukem size_t i;
123 1.2 lukem
124 1.2 lukem for (i = 0; i < sl->sl_cur; i++)
125 1.2 lukem if (strcmp(sl->sl_str[i], name) == 0)
126 1.2 lukem return sl->sl_str[i];
127 1.2 lukem
128 1.2 lukem return NULL;
129 1.2 lukem }
130