stringlist.c revision 249802
1106266Sjulian/*
2106266Sjulian * Copyright (c) 1994 Christos Zoulas
3139823Simp * All rights reserved.
4139823Simp *
5139823Simp * Redistribution and use in source and binary forms, with or without
6144674Sglebius * modification, are permitted provided that the following conditions
7106266Sjulian * are met:
8106266Sjulian * 1. Redistributions of source code must retain the above copyright
9106266Sjulian *    notice, this list of conditions and the following disclaimer.
10106266Sjulian * 2. Redistributions in binary form must reproduce the above copyright
11106266Sjulian *    notice, this list of conditions and the following disclaimer in the
12106319Sjulian *    documentation and/or other materials provided with the distribution.
13106266Sjulian * 4. The name of the author may not be used to endorse or promote products
14106319Sjulian *    derived from this software without specific prior written permission.
15106319Sjulian *
16106266Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17106319Sjulian * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18106319Sjulian * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19106266Sjulian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20106266Sjulian * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21106266Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22106319Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23106319Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24106319Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25106266Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26106266Sjulian * SUCH DAMAGE.
27106266Sjulian */
28106266Sjulian
29106266Sjulian#if defined(LIBC_SCCS) && !defined(lint)
30106319Sjulianstatic char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $";
31106266Sjulian#endif /* LIBC_SCCS and not lint */
32106266Sjulian#include <sys/cdefs.h>
33106266Sjulian__FBSDID("$FreeBSD: head/lib/libc/gen/stringlist.c 249802 2013-04-23 13:03:03Z eadler $");
34106266Sjulian
35106266Sjulian#include "namespace.h"
36106266Sjulian#include <stdio.h>
37106266Sjulian#include <string.h>
38106266Sjulian#include <err.h>
39106266Sjulian#include <stdlib.h>
40106266Sjulian#include <stringlist.h>
41125077Sharti#include "un-namespace.h"
42125077Sharti
43125077Sharti#define _SL_CHUNKSIZE	20
44106266Sjulian
45106266Sjulian/*
46143387Sbmilekic * sl_init(): Initialize a string list
47143387Sbmilekic */
48143387SbmilekicStringList *
49143387Sbmilekicsl_init(void)
50106266Sjulian{
51106266Sjulian	StringList *sl;
52106266Sjulian
53106266Sjulian	sl = malloc(sizeof(StringList));
54106266Sjulian	if (sl == NULL)
55106266Sjulian		_err(1, "stringlist: %m");
56143387Sbmilekic
57106266Sjulian	sl->sl_cur = 0;
58106266Sjulian	sl->sl_max = _SL_CHUNKSIZE;
59106266Sjulian	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
60106266Sjulian	if (sl->sl_str == NULL)
61106266Sjulian		_err(1, "stringlist: %m");
62106266Sjulian	return sl;
63106266Sjulian}
64106266Sjulian
65106266Sjulian
66106266Sjulian/*
67106266Sjulian * sl_add(): Add an item to the string list
68144674Sglebius */
69106266Sjulianint
70106266Sjuliansl_add(StringList *sl, char *name)
71106266Sjulian{
72106266Sjulian	if (sl->sl_cur == sl->sl_max - 1) {
73106266Sjulian		sl->sl_max += _SL_CHUNKSIZE;
74106266Sjulian		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
75106266Sjulian		if (sl->sl_str == NULL)
76106266Sjulian			return (-1);
77106266Sjulian	}
78106266Sjulian	sl->sl_str[sl->sl_cur++] = name;
79106266Sjulian	return (0);
80106266Sjulian}
81106266Sjulian
82106266Sjulian
83144674Sglebius/*
84144674Sglebius * sl_free(): Free a stringlist
85106266Sjulian */
86106319Sjulianvoid
87106266Sjuliansl_free(StringList *sl, int all)
88137138Sglebius{
89144674Sglebius	size_t i;
90144674Sglebius
91106266Sjulian	if (sl == NULL)
92106266Sjulian		return;
93106266Sjulian	if (sl->sl_str) {
94106266Sjulian		if (all)
95106266Sjulian			for (i = 0; i < sl->sl_cur; i++)
96106266Sjulian				free(sl->sl_str[i]);
97106266Sjulian		free(sl->sl_str);
98106266Sjulian	}
99106266Sjulian	free(sl);
100106266Sjulian}
101106266Sjulian
102144674Sglebius
103106266Sjulian/*
104106266Sjulian * sl_find(): Find a name in the string list
105106266Sjulian */
106106266Sjulianchar *
107125243Shartisl_find(StringList *sl, const char *name)
108106266Sjulian{
109144674Sglebius	size_t i;
110106266Sjulian
111106266Sjulian	for (i = 0; i < sl->sl_cur; i++)
112144674Sglebius		if (strcmp(sl->sl_str[i], name) == 0)
113106266Sjulian			return sl->sl_str[i];
114106266Sjulian
115125077Sharti	return NULL;
116106266Sjulian}
117106266Sjulian