1*2e2caf59SThomas Veerman /* $NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $ */
2*2e2caf59SThomas Veerman
3*2e2caf59SThomas Veerman /*-
4*2e2caf59SThomas Veerman * Copyright (c) 2008 - 2009 The NetBSD Foundation, Inc.
5*2e2caf59SThomas Veerman * All rights reserved.
6*2e2caf59SThomas Veerman *
7*2e2caf59SThomas Veerman * This code is derived from software contributed to The NetBSD Foundation
8*2e2caf59SThomas Veerman * by David Laight.
9*2e2caf59SThomas Veerman *
10*2e2caf59SThomas Veerman * Redistribution and use in source and binary forms, with or without
11*2e2caf59SThomas Veerman * modification, are permitted provided that the following conditions
12*2e2caf59SThomas Veerman * are met:
13*2e2caf59SThomas Veerman * 1. Redistributions of source code must retain the above copyright
14*2e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer.
15*2e2caf59SThomas Veerman * 2. Redistributions in binary form must reproduce the above copyright
16*2e2caf59SThomas Veerman * notice, this list of conditions and the following disclaimer in the
17*2e2caf59SThomas Veerman * documentation and/or other materials provided with the distribution.
18*2e2caf59SThomas Veerman * 3. Neither the name of The NetBSD Foundation nor the names of its
19*2e2caf59SThomas Veerman * contributors may be used to endorse or promote products derived
20*2e2caf59SThomas Veerman * from this software without specific prior written permission.
21*2e2caf59SThomas Veerman *
22*2e2caf59SThomas Veerman * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23*2e2caf59SThomas Veerman * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*2e2caf59SThomas Veerman * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25*2e2caf59SThomas Veerman * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26*2e2caf59SThomas Veerman * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27*2e2caf59SThomas Veerman * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28*2e2caf59SThomas Veerman * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29*2e2caf59SThomas Veerman * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30*2e2caf59SThomas Veerman * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31*2e2caf59SThomas Veerman * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32*2e2caf59SThomas Veerman * POSSIBILITY OF SUCH DAMAGE.
33*2e2caf59SThomas Veerman */
34*2e2caf59SThomas Veerman
35*2e2caf59SThomas Veerman #ifndef MAKE_NATIVE
36*2e2caf59SThomas Veerman static char rcsid[] = "$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $";
37*2e2caf59SThomas Veerman #else
38*2e2caf59SThomas Veerman #include <sys/cdefs.h>
39*2e2caf59SThomas Veerman #ifndef lint
40*2e2caf59SThomas Veerman __RCSID("$NetBSD: strlist.c,v 1.4 2009/01/24 11:59:39 dsl Exp $");
41*2e2caf59SThomas Veerman #endif /* not lint */
42*2e2caf59SThomas Veerman #endif
43*2e2caf59SThomas Veerman
44*2e2caf59SThomas Veerman #include <stddef.h>
45*2e2caf59SThomas Veerman #include <stdlib.h>
46*2e2caf59SThomas Veerman #include "strlist.h"
47*2e2caf59SThomas Veerman #include "make_malloc.h"
48*2e2caf59SThomas Veerman
49*2e2caf59SThomas Veerman void
strlist_init(strlist_t * sl)50*2e2caf59SThomas Veerman strlist_init(strlist_t *sl)
51*2e2caf59SThomas Veerman {
52*2e2caf59SThomas Veerman sl->sl_num = 0;
53*2e2caf59SThomas Veerman sl->sl_max = 0;
54*2e2caf59SThomas Veerman sl->sl_items = NULL;
55*2e2caf59SThomas Veerman }
56*2e2caf59SThomas Veerman
57*2e2caf59SThomas Veerman void
strlist_clean(strlist_t * sl)58*2e2caf59SThomas Veerman strlist_clean(strlist_t *sl)
59*2e2caf59SThomas Veerman {
60*2e2caf59SThomas Veerman char *str;
61*2e2caf59SThomas Veerman int i;
62*2e2caf59SThomas Veerman
63*2e2caf59SThomas Veerman STRLIST_FOREACH(str, sl, i)
64*2e2caf59SThomas Veerman free(str);
65*2e2caf59SThomas Veerman free(sl->sl_items);
66*2e2caf59SThomas Veerman
67*2e2caf59SThomas Veerman sl->sl_num = 0;
68*2e2caf59SThomas Veerman sl->sl_max = 0;
69*2e2caf59SThomas Veerman sl->sl_items = NULL;
70*2e2caf59SThomas Veerman }
71*2e2caf59SThomas Veerman
72*2e2caf59SThomas Veerman void
strlist_add_str(strlist_t * sl,char * str,unsigned int info)73*2e2caf59SThomas Veerman strlist_add_str(strlist_t *sl, char *str, unsigned int info)
74*2e2caf59SThomas Veerman {
75*2e2caf59SThomas Veerman unsigned int n;
76*2e2caf59SThomas Veerman strlist_item_t *items;
77*2e2caf59SThomas Veerman
78*2e2caf59SThomas Veerman if (str == NULL)
79*2e2caf59SThomas Veerman return;
80*2e2caf59SThomas Veerman
81*2e2caf59SThomas Veerman n = sl->sl_num + 1;
82*2e2caf59SThomas Veerman sl->sl_num = n;
83*2e2caf59SThomas Veerman items = sl->sl_items;
84*2e2caf59SThomas Veerman if (n >= sl->sl_max) {
85*2e2caf59SThomas Veerman items = bmake_realloc(items, (n + 7) * sizeof *sl->sl_items);
86*2e2caf59SThomas Veerman sl->sl_items = items;
87*2e2caf59SThomas Veerman sl->sl_max = n + 6;
88*2e2caf59SThomas Veerman }
89*2e2caf59SThomas Veerman items += n - 1;
90*2e2caf59SThomas Veerman items->si_str = str;
91*2e2caf59SThomas Veerman items->si_info = info;
92*2e2caf59SThomas Veerman items[1].si_str = NULL; /* STRLIST_FOREACH() terminator */
93*2e2caf59SThomas Veerman }
94