stringlist.c revision 86250
126925Smsmith/* $NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $ */ 226925Smsmith 326925Smsmith/* 426925Smsmith * Copyright (c) 1994 Christos Zoulas 526925Smsmith * All rights reserved. 626925Smsmith * 726925Smsmith * Redistribution and use in source and binary forms, with or without 826925Smsmith * modification, are permitted provided that the following conditions 926925Smsmith * are met: 1026925Smsmith * 1. Redistributions of source code must retain the above copyright 1126925Smsmith * notice, this list of conditions and the following disclaimer. 1226925Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1326925Smsmith * notice, this list of conditions and the following disclaimer in the 1426925Smsmith * documentation and/or other materials provided with the distribution. 1526925Smsmith * 3. All advertising materials mentioning features or use of this software 1626925Smsmith * must display the following acknowledgement: 1726925Smsmith * This product includes software developed by Christos Zoulas. 1826925Smsmith * 4. The name of the author may not be used to endorse or promote products 1926925Smsmith * derived from this software without specific prior written permission. 2026925Smsmith * 2126925Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 2226925Smsmith * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 2326925Smsmith * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2426925Smsmith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 2526925Smsmith * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2626925Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2726925Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2826925Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2926925Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3026925Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3126925Smsmith * SUCH DAMAGE. 3226925Smsmith */ 3326925Smsmith 3486250Sbde#if 0 3526925Smsmith#if defined(LIBC_SCCS) && !defined(lint) 3626925Smsmithstatic char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; 3726925Smsmith#endif /* LIBC_SCCS and not lint */ 3886250Sbde#endif 3926925Smsmith 4086250Sbde#include <sys/cdefs.h> 4186250Sbde__FBSDID("$FreeBSD: head/lib/libc/gen/stringlist.c 86250 2001-11-11 02:48:09Z bde $"); 4286250Sbde 4326925Smsmith#include <stdio.h> 4426925Smsmith#include <string.h> 4526925Smsmith#include <err.h> 4626925Smsmith#include <stdlib.h> 4726925Smsmith#include <stringlist.h> 4826925Smsmith 4926925Smsmith#define _SL_CHUNKSIZE 20 5026925Smsmith 5126925Smsmith/* 5226925Smsmith * sl_init(): Initialize a string list 5326925Smsmith */ 5426925SmsmithStringList * 5526925Smsmithsl_init() 5626925Smsmith{ 5726925Smsmith StringList *sl = malloc(sizeof(StringList)); 5826925Smsmith if (sl == NULL) 5986250Sbde _err(1, "stringlist: %m"); 6026925Smsmith 6126925Smsmith sl->sl_cur = 0; 6226925Smsmith sl->sl_max = _SL_CHUNKSIZE; 6326925Smsmith sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 6426925Smsmith if (sl->sl_str == NULL) 6586250Sbde _err(1, "stringlist: %m"); 6626925Smsmith return sl; 6726925Smsmith} 6826925Smsmith 6926925Smsmith 7026925Smsmith/* 7126925Smsmith * sl_add(): Add an item to the string list 7226925Smsmith */ 7326925Smsmithvoid 7426925Smsmithsl_add(sl, name) 7526925Smsmith StringList *sl; 7626925Smsmith char *name; 7726925Smsmith{ 7826925Smsmith if (sl->sl_cur == sl->sl_max - 1) { 7926925Smsmith sl->sl_max += _SL_CHUNKSIZE; 8039327Simp sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 8126925Smsmith if (sl->sl_str == NULL) 8286250Sbde _err(1, "stringlist: %m"); 8326925Smsmith } 8426925Smsmith sl->sl_str[sl->sl_cur++] = name; 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