stringlist.c revision 108868
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 108868 2003-01-07 06:55:58Z tjr $"); 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{ 5426925Smsmith StringList *sl = malloc(sizeof(StringList)); 5526925Smsmith if (sl == NULL) 5686250Sbde _err(1, "stringlist: %m"); 5726925Smsmith 5826925Smsmith sl->sl_cur = 0; 5926925Smsmith sl->sl_max = _SL_CHUNKSIZE; 6026925Smsmith sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 6126925Smsmith if (sl->sl_str == NULL) 6286250Sbde _err(1, "stringlist: %m"); 6326925Smsmith return sl; 6426925Smsmith} 6526925Smsmith 6626925Smsmith 6726925Smsmith/* 6826925Smsmith * sl_add(): Add an item to the string list 6926925Smsmith */ 7026925Smsmithvoid 7126925Smsmithsl_add(sl, name) 7226925Smsmith StringList *sl; 7326925Smsmith char *name; 7426925Smsmith{ 7526925Smsmith if (sl->sl_cur == sl->sl_max - 1) { 7626925Smsmith sl->sl_max += _SL_CHUNKSIZE; 7739327Simp sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 7826925Smsmith if (sl->sl_str == NULL) 7986250Sbde _err(1, "stringlist: %m"); 8026925Smsmith } 8126925Smsmith sl->sl_str[sl->sl_cur++] = name; 8226925Smsmith} 8326925Smsmith 8426925Smsmith 8526925Smsmith/* 8626925Smsmith * sl_free(): Free a stringlist 8726925Smsmith */ 8826925Smsmithvoid 8926925Smsmithsl_free(sl, all) 9026925Smsmith StringList *sl; 9126925Smsmith int all; 9226925Smsmith{ 9326925Smsmith size_t i; 9426925Smsmith 9526925Smsmith if (sl == NULL) 9626925Smsmith return; 9726925Smsmith if (sl->sl_str) { 9826925Smsmith if (all) 9926925Smsmith for (i = 0; i < sl->sl_cur; i++) 10026925Smsmith free(sl->sl_str[i]); 10126925Smsmith free(sl->sl_str); 10226925Smsmith } 10326925Smsmith free(sl); 10426925Smsmith} 10526925Smsmith 10626925Smsmith 10726925Smsmith/* 10826925Smsmith * sl_find(): Find a name in the string list 10926925Smsmith */ 11026925Smsmithchar * 11126925Smsmithsl_find(sl, name) 11226925Smsmith StringList *sl; 11326925Smsmith char *name; 11426925Smsmith{ 11526925Smsmith size_t i; 11626925Smsmith 11726925Smsmith for (i = 0; i < sl->sl_cur; i++) 11826925Smsmith if (strcmp(sl->sl_str[i], name) == 0) 11926925Smsmith return sl->sl_str[i]; 12026925Smsmith 12126925Smsmith return NULL; 12226925Smsmith} 123