stringlist.c revision 1.2 1 1.2 lukem /* $NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem 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.2 lukem #if defined(LIBC_SCCS) && !defined(lint)
35 1.2 lukem static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $";
36 1.2 lukem #endif /* LIBC_SCCS and not lint */
37 1.2 lukem
38 1.2 lukem #include <stdio.h>
39 1.2 lukem #include <string.h>
40 1.2 lukem #include <err.h>
41 1.2 lukem #include <stdlib.h>
42 1.2 lukem #include <stringlist.h>
43 1.2 lukem
44 1.2 lukem #define _SL_CHUNKSIZE 20
45 1.2 lukem
46 1.2 lukem /*
47 1.2 lukem * sl_init(): Initialize a string list
48 1.2 lukem */
49 1.2 lukem StringList *
50 1.2 lukem sl_init()
51 1.2 lukem {
52 1.2 lukem StringList *sl = malloc(sizeof(StringList));
53 1.2 lukem if (sl == NULL)
54 1.2 lukem _err(1, "stringlist: %m");
55 1.2 lukem
56 1.2 lukem sl->sl_cur = 0;
57 1.2 lukem sl->sl_max = _SL_CHUNKSIZE;
58 1.2 lukem sl->sl_str = malloc(sl->sl_max * sizeof(char *));
59 1.2 lukem if (sl->sl_str == NULL)
60 1.2 lukem _err(1, "stringlist: %m");
61 1.2 lukem return sl;
62 1.2 lukem }
63 1.2 lukem
64 1.2 lukem
65 1.2 lukem /*
66 1.2 lukem * sl_add(): Add an item to the string list
67 1.2 lukem */
68 1.2 lukem void
69 1.2 lukem sl_add(sl, name)
70 1.2 lukem StringList *sl;
71 1.2 lukem char *name;
72 1.2 lukem {
73 1.2 lukem if (sl->sl_cur == sl->sl_max - 1) {
74 1.2 lukem sl->sl_max += _SL_CHUNKSIZE;
75 1.2 lukem sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof(char *));
76 1.2 lukem if (sl->sl_str == NULL)
77 1.2 lukem _err(1, "stringlist: %m");
78 1.2 lukem }
79 1.2 lukem sl->sl_str[sl->sl_cur++] = name;
80 1.2 lukem }
81 1.2 lukem
82 1.2 lukem
83 1.2 lukem /*
84 1.2 lukem * sl_free(): Free a stringlist
85 1.2 lukem */
86 1.2 lukem void
87 1.2 lukem sl_free(sl, all)
88 1.2 lukem StringList *sl;
89 1.2 lukem int all;
90 1.2 lukem {
91 1.2 lukem size_t i;
92 1.2 lukem
93 1.2 lukem if (sl == NULL)
94 1.2 lukem return;
95 1.2 lukem if (sl->sl_str) {
96 1.2 lukem if (all)
97 1.2 lukem for (i = 0; i < sl->sl_cur; i++)
98 1.2 lukem free(sl->sl_str[i]);
99 1.2 lukem free(sl->sl_str);
100 1.2 lukem }
101 1.2 lukem free(sl);
102 1.2 lukem }
103 1.2 lukem
104 1.2 lukem
105 1.2 lukem /*
106 1.2 lukem * sl_find(): Find a name in the string list
107 1.2 lukem */
108 1.2 lukem char *
109 1.2 lukem sl_find(sl, name)
110 1.2 lukem StringList *sl;
111 1.2 lukem char *name;
112 1.2 lukem {
113 1.2 lukem size_t i;
114 1.2 lukem
115 1.2 lukem for (i = 0; i < sl->sl_cur; i++)
116 1.2 lukem if (strcmp(sl->sl_str[i], name) == 0)
117 1.2 lukem return sl->sl_str[i];
118 1.2 lukem
119 1.2 lukem return NULL;
120 1.2 lukem }
121