126925Smsmith/*
226925Smsmith * Copyright (c) 1994 Christos Zoulas
326925Smsmith * All rights reserved.
426925Smsmith *
526925Smsmith * Redistribution and use in source and binary forms, with or without
626925Smsmith * modification, are permitted provided that the following conditions
726925Smsmith * are met:
826925Smsmith * 1. Redistributions of source code must retain the above copyright
926925Smsmith *    notice, this list of conditions and the following disclaimer.
1026925Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1126925Smsmith *    notice, this list of conditions and the following disclaimer in the
1226925Smsmith *    documentation and/or other materials provided with the distribution.
1326925Smsmith * 4. The name of the author may not be used to endorse or promote products
1426925Smsmith *    derived from this software without specific prior written permission.
1526925Smsmith *
1626925Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1726925Smsmith * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1826925Smsmith * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1926925Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2026925Smsmith * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2126925Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2226925Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2326925Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2426925Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2526925Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2626925Smsmith * SUCH DAMAGE.
2726925Smsmith */
2826925Smsmith
2926925Smsmith#if defined(LIBC_SCCS) && !defined(lint)
3026925Smsmithstatic char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $";
3126925Smsmith#endif /* LIBC_SCCS and not lint */
3286250Sbde#include <sys/cdefs.h>
3386250Sbde__FBSDID("$FreeBSD$");
3486250Sbde
35108868Stjr#include "namespace.h"
3626925Smsmith#include <stdio.h>
3726925Smsmith#include <string.h>
3826925Smsmith#include <err.h>
3926925Smsmith#include <stdlib.h>
4026925Smsmith#include <stringlist.h>
41108868Stjr#include "un-namespace.h"
4226925Smsmith
4326925Smsmith#define _SL_CHUNKSIZE	20
4426925Smsmith
4526925Smsmith/*
4626925Smsmith * sl_init(): Initialize a string list
4726925Smsmith */
4826925SmsmithStringList *
49249801Seadlersl_init(void)
5026925Smsmith{
51109508Sobrien	StringList *sl;
52109508Sobrien
53109508Sobrien	sl = malloc(sizeof(StringList));
5426925Smsmith	if (sl == NULL)
5586250Sbde		_err(1, "stringlist: %m");
5626925Smsmith
5726925Smsmith	sl->sl_cur = 0;
5826925Smsmith	sl->sl_max = _SL_CHUNKSIZE;
5926925Smsmith	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
6026925Smsmith	if (sl->sl_str == NULL)
6186250Sbde		_err(1, "stringlist: %m");
6226925Smsmith	return sl;
6326925Smsmith}
6426925Smsmith
6526925Smsmith
6626925Smsmith/*
6726925Smsmith * sl_add(): Add an item to the string list
6826925Smsmith */
69109508Sobrienint
70249801Seadlersl_add(StringList *sl, char *name)
7126925Smsmith{
7226925Smsmith	if (sl->sl_cur == sl->sl_max - 1) {
7326925Smsmith		sl->sl_max += _SL_CHUNKSIZE;
7439327Simp		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
7526925Smsmith		if (sl->sl_str == NULL)
76109508Sobrien			return (-1);
7726925Smsmith	}
7826925Smsmith	sl->sl_str[sl->sl_cur++] = name;
79109508Sobrien	return (0);
8026925Smsmith}
8126925Smsmith
8226925Smsmith
8326925Smsmith/*
8426925Smsmith * sl_free(): Free a stringlist
8526925Smsmith */
8626925Smsmithvoid
87249801Seadlersl_free(StringList *sl, int all)
8826925Smsmith{
8926925Smsmith	size_t i;
9026925Smsmith
9126925Smsmith	if (sl == NULL)
9226925Smsmith		return;
9326925Smsmith	if (sl->sl_str) {
9426925Smsmith		if (all)
9526925Smsmith			for (i = 0; i < sl->sl_cur; i++)
9626925Smsmith				free(sl->sl_str[i]);
9726925Smsmith		free(sl->sl_str);
9826925Smsmith	}
9926925Smsmith	free(sl);
10026925Smsmith}
10126925Smsmith
10226925Smsmith
10326925Smsmith/*
10426925Smsmith * sl_find(): Find a name in the string list
10526925Smsmith */
10626925Smsmithchar *
107249802Seadlersl_find(StringList *sl, const char *name)
10826925Smsmith{
10926925Smsmith	size_t i;
11026925Smsmith
11126925Smsmith	for (i = 0; i < sl->sl_cur; i++)
11226925Smsmith		if (strcmp(sl->sl_str[i], name) == 0)
11326925Smsmith			return sl->sl_str[i];
11426925Smsmith
11526925Smsmith	return NULL;
11626925Smsmith}
117