stringlist.c revision 27039
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 3426925Smsmith#if defined(LIBC_SCCS) && !defined(lint) 3526925Smsmithstatic char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; 3626925Smsmith#endif /* LIBC_SCCS and not lint */ 3726925Smsmith 3826925Smsmith#include <stdio.h> 3926925Smsmith#include <string.h> 4026925Smsmith#include <err.h> 4126925Smsmith#include <stdlib.h> 4226925Smsmith#include <stringlist.h> 4326925Smsmith 4426925Smsmith#define _SL_CHUNKSIZE 20 4526925Smsmith 4626925Smsmith/* 4726925Smsmith * sl_init(): Initialize a string list 4826925Smsmith */ 4926925SmsmithStringList * 5026925Smsmithsl_init() 5126925Smsmith{ 5226925Smsmith StringList *sl = malloc(sizeof(StringList)); 5326925Smsmith if (sl == NULL) 5427039Sjkh err(1, "stringlist: %m"); 5526925Smsmith 5626925Smsmith sl->sl_cur = 0; 5726925Smsmith sl->sl_max = _SL_CHUNKSIZE; 5826925Smsmith sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 5926925Smsmith if (sl->sl_str == NULL) 6027039Sjkh err(1, "stringlist: %m"); 6126925Smsmith return sl; 6226925Smsmith} 6326925Smsmith 6426925Smsmith 6526925Smsmith/* 6626925Smsmith * sl_add(): Add an item to the string list 6726925Smsmith */ 6826925Smsmithvoid 6926925Smsmithsl_add(sl, name) 7026925Smsmith StringList *sl; 7126925Smsmith char *name; 7226925Smsmith{ 7326925Smsmith if (sl->sl_cur == sl->sl_max - 1) { 7426925Smsmith sl->sl_max += _SL_CHUNKSIZE; 7526925Smsmith sl->sl_str = realloc(sl->sl_str, sl->sl_max * sizeof(char *)); 7626925Smsmith if (sl->sl_str == NULL) 7727039Sjkh err(1, "stringlist: %m"); 7826925Smsmith } 7926925Smsmith sl->sl_str[sl->sl_cur++] = name; 8026925Smsmith} 8126925Smsmith 8226925Smsmith 8326925Smsmith/* 8426925Smsmith * sl_free(): Free a stringlist 8526925Smsmith */ 8626925Smsmithvoid 8726925Smsmithsl_free(sl, all) 8826925Smsmith StringList *sl; 8926925Smsmith int all; 9026925Smsmith{ 9126925Smsmith size_t i; 9226925Smsmith 9326925Smsmith if (sl == NULL) 9426925Smsmith return; 9526925Smsmith if (sl->sl_str) { 9626925Smsmith if (all) 9726925Smsmith for (i = 0; i < sl->sl_cur; i++) 9826925Smsmith free(sl->sl_str[i]); 9926925Smsmith free(sl->sl_str); 10026925Smsmith } 10126925Smsmith free(sl); 10226925Smsmith} 10326925Smsmith 10426925Smsmith 10526925Smsmith/* 10626925Smsmith * sl_find(): Find a name in the string list 10726925Smsmith */ 10826925Smsmithchar * 10926925Smsmithsl_find(sl, name) 11026925Smsmith StringList *sl; 11126925Smsmith char *name; 11226925Smsmith{ 11326925Smsmith size_t i; 11426925Smsmith 11526925Smsmith for (i = 0; i < sl->sl_cur; i++) 11626925Smsmith if (strcmp(sl->sl_str[i], name) == 0) 11726925Smsmith return sl->sl_str[i]; 11826925Smsmith 11926925Smsmith return NULL; 12026925Smsmith} 121