stringlist.c revision 39327
1189251Ssam/*	$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $	*/
2214734Srpaulo
3214734Srpaulo/*
4189251Ssam * Copyright (c) 1994 Christos Zoulas
5252726Srpaulo * All rights reserved.
6252726Srpaulo *
7189251Ssam * Redistribution and use in source and binary forms, with or without
8189251Ssam * modification, are permitted provided that the following conditions
9189251Ssam * are met:
10189251Ssam * 1. Redistributions of source code must retain the above copyright
11189251Ssam *    notice, this list of conditions and the following disclaimer.
12189251Ssam * 2. Redistributions in binary form must reproduce the above copyright
13189251Ssam *    notice, this list of conditions and the following disclaimer in the
14189251Ssam *    documentation and/or other materials provided with the distribution.
15189251Ssam * 3. All advertising materials mentioning features or use of this software
16189251Ssam *    must display the following acknowledgement:
17189251Ssam *	This product includes software developed by Christos Zoulas.
18189251Ssam * 4. The name of the author may not be used to endorse or promote products
19189251Ssam *    derived from this software without specific prior written permission.
20189251Ssam *
21189251Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22189251Ssam * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23189251Ssam * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24189251Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25189251Ssam * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26189251Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27189251Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28189251Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29189251Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30189251Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31189251Ssam * SUCH DAMAGE.
32189251Ssam */
33189251Ssam
34189251Ssam#if defined(LIBC_SCCS) && !defined(lint)
35189251Ssamstatic char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $";
36189251Ssam#endif /* LIBC_SCCS and not lint */
37189251Ssam
38189251Ssam#include <stdio.h>
39189251Ssam#include <string.h>
40189251Ssam#include <err.h>
41189251Ssam#include <stdlib.h>
42189251Ssam#include <stringlist.h>
43189251Ssam
44189251Ssam#define _SL_CHUNKSIZE	20
45189251Ssam
46189251Ssam/*
47189251Ssam * sl_init(): Initialize a string list
48189251Ssam */
49189251SsamStringList *
50189251Ssamsl_init()
51189251Ssam{
52189251Ssam	StringList *sl = malloc(sizeof(StringList));
53189251Ssam	if (sl == NULL)
54189251Ssam		err(1, "stringlist: %m");
55189251Ssam
56189251Ssam	sl->sl_cur = 0;
57189251Ssam	sl->sl_max = _SL_CHUNKSIZE;
58189251Ssam	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
59189251Ssam	if (sl->sl_str == NULL)
60189251Ssam		err(1, "stringlist: %m");
61189251Ssam	return sl;
62189251Ssam}
63189251Ssam
64189251Ssam
65189251Ssam/*
66189251Ssam * sl_add(): Add an item to the string list
67252726Srpaulo */
68252726Srpaulovoid
69252726Srpaulosl_add(sl, name)
70252726Srpaulo	StringList *sl;
71252726Srpaulo	char *name;
72252726Srpaulo{
73252726Srpaulo	if (sl->sl_cur == sl->sl_max - 1) {
74252726Srpaulo		sl->sl_max += _SL_CHUNKSIZE;
75189251Ssam		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
76252726Srpaulo		if (sl->sl_str == NULL)
77252726Srpaulo			err(1, "stringlist: %m");
78189251Ssam	}
79189251Ssam	sl->sl_str[sl->sl_cur++] = name;
80189251Ssam}
81189251Ssam
82189251Ssam
83189251Ssam/*
84189251Ssam * sl_free(): Free a stringlist
85189251Ssam */
86189251Ssamvoid
87189251Ssamsl_free(sl, all)
88189251Ssam	StringList *sl;
89189251Ssam	int all;
90189251Ssam{
91189251Ssam	size_t i;
92189251Ssam
93189251Ssam	if (sl == NULL)
94189251Ssam		return;
95189251Ssam	if (sl->sl_str) {
96189251Ssam		if (all)
97189251Ssam			for (i = 0; i < sl->sl_cur; i++)
98189251Ssam				free(sl->sl_str[i]);
99189251Ssam		free(sl->sl_str);
100189251Ssam	}
101189251Ssam	free(sl);
102189251Ssam}
103189251Ssam
104189251Ssam
105189251Ssam/*
106189251Ssam * sl_find(): Find a name in the string list
107189251Ssam */
108189251Ssamchar *
109189251Ssamsl_find(sl, name)
110189251Ssam	StringList *sl;
111189251Ssam	char *name;
112189251Ssam{
113189251Ssam	size_t i;
114189251Ssam
115189251Ssam	for (i = 0; i < sl->sl_cur; i++)
116189251Ssam		if (strcmp(sl->sl_str[i], name) == 0)
117189251Ssam			return sl->sl_str[i];
118189251Ssam
119189251Ssam	return NULL;
120189251Ssam}
121189251Ssam