stringlist.c revision 109508
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 * 3. All advertising materials mentioning features or use of this software
1426925Smsmith *    must display the following acknowledgement:
1526925Smsmith *	This product includes software developed by Christos Zoulas.
1626925Smsmith * 4. The name of the author may not be used to endorse or promote products
1726925Smsmith *    derived from this software without specific prior written permission.
1826925Smsmith *
1926925Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
2026925Smsmith * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2126925Smsmith * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2226925Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2326925Smsmith * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2426925Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2526925Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2626925Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2726925Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2826925Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2926925Smsmith * SUCH DAMAGE.
3026925Smsmith */
3126925Smsmith
3226925Smsmith#if defined(LIBC_SCCS) && !defined(lint)
3326925Smsmithstatic char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $";
3426925Smsmith#endif /* LIBC_SCCS and not lint */
3586250Sbde#include <sys/cdefs.h>
3686250Sbde__FBSDID("$FreeBSD: head/lib/libc/gen/stringlist.c 109508 2003-01-19 01:16:01Z obrien $");
3786250Sbde
38108868Stjr#include "namespace.h"
3926925Smsmith#include <stdio.h>
4026925Smsmith#include <string.h>
4126925Smsmith#include <err.h>
4226925Smsmith#include <stdlib.h>
4326925Smsmith#include <stringlist.h>
44108868Stjr#include "un-namespace.h"
4526925Smsmith
4626925Smsmith#define _SL_CHUNKSIZE	20
4726925Smsmith
4826925Smsmith/*
4926925Smsmith * sl_init(): Initialize a string list
5026925Smsmith */
5126925SmsmithStringList *
5226925Smsmithsl_init()
5326925Smsmith{
54109508Sobrien	StringList *sl;
55109508Sobrien
56109508Sobrien	sl = malloc(sizeof(StringList));
5726925Smsmith	if (sl == NULL)
5886250Sbde		_err(1, "stringlist: %m");
5926925Smsmith
6026925Smsmith	sl->sl_cur = 0;
6126925Smsmith	sl->sl_max = _SL_CHUNKSIZE;
6226925Smsmith	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
6326925Smsmith	if (sl->sl_str == NULL)
6486250Sbde		_err(1, "stringlist: %m");
6526925Smsmith	return sl;
6626925Smsmith}
6726925Smsmith
6826925Smsmith
6926925Smsmith/*
7026925Smsmith * sl_add(): Add an item to the string list
7126925Smsmith */
72109508Sobrienint
7326925Smsmithsl_add(sl, name)
7426925Smsmith	StringList *sl;
7526925Smsmith	char *name;
7626925Smsmith{
7726925Smsmith	if (sl->sl_cur == sl->sl_max - 1) {
7826925Smsmith		sl->sl_max += _SL_CHUNKSIZE;
7939327Simp		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
8026925Smsmith		if (sl->sl_str == NULL)
81109508Sobrien			return (-1);
8226925Smsmith	}
8326925Smsmith	sl->sl_str[sl->sl_cur++] = name;
84109508Sobrien	return (0);
8526925Smsmith}
8626925Smsmith
8726925Smsmith
8826925Smsmith/*
8926925Smsmith * sl_free(): Free a stringlist
9026925Smsmith */
9126925Smsmithvoid
9226925Smsmithsl_free(sl, all)
9326925Smsmith	StringList *sl;
9426925Smsmith	int all;
9526925Smsmith{
9626925Smsmith	size_t i;
9726925Smsmith
9826925Smsmith	if (sl == NULL)
9926925Smsmith		return;
10026925Smsmith	if (sl->sl_str) {
10126925Smsmith		if (all)
10226925Smsmith			for (i = 0; i < sl->sl_cur; i++)
10326925Smsmith				free(sl->sl_str[i]);
10426925Smsmith		free(sl->sl_str);
10526925Smsmith	}
10626925Smsmith	free(sl);
10726925Smsmith}
10826925Smsmith
10926925Smsmith
11026925Smsmith/*
11126925Smsmith * sl_find(): Find a name in the string list
11226925Smsmith */
11326925Smsmithchar *
11426925Smsmithsl_find(sl, name)
11526925Smsmith	StringList *sl;
11626925Smsmith	char *name;
11726925Smsmith{
11826925Smsmith	size_t i;
11926925Smsmith
12026925Smsmith	for (i = 0; i < sl->sl_cur; i++)
12126925Smsmith		if (strcmp(sl->sl_str[i], name) == 0)
12226925Smsmith			return sl->sl_str[i];
12326925Smsmith
12426925Smsmith	return NULL;
12526925Smsmith}
126