14ce2d5b5SMike Smith /* 24ce2d5b5SMike Smith * Copyright (c) 1994 Christos Zoulas 34ce2d5b5SMike Smith * All rights reserved. 44ce2d5b5SMike Smith * 54ce2d5b5SMike Smith * Redistribution and use in source and binary forms, with or without 64ce2d5b5SMike Smith * modification, are permitted provided that the following conditions 74ce2d5b5SMike Smith * are met: 84ce2d5b5SMike Smith * 1. Redistributions of source code must retain the above copyright 94ce2d5b5SMike Smith * notice, this list of conditions and the following disclaimer. 104ce2d5b5SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 114ce2d5b5SMike Smith * notice, this list of conditions and the following disclaimer in the 124ce2d5b5SMike Smith * documentation and/or other materials provided with the distribution. 134ce2d5b5SMike Smith * 3. All advertising materials mentioning features or use of this software 144ce2d5b5SMike Smith * must display the following acknowledgement: 154ce2d5b5SMike Smith * This product includes software developed by Christos Zoulas. 164ce2d5b5SMike Smith * 4. The name of the author may not be used to endorse or promote products 174ce2d5b5SMike Smith * derived from this software without specific prior written permission. 184ce2d5b5SMike Smith * 194ce2d5b5SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 204ce2d5b5SMike Smith * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 214ce2d5b5SMike Smith * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 224ce2d5b5SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 234ce2d5b5SMike Smith * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 244ce2d5b5SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 254ce2d5b5SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 264ce2d5b5SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 274ce2d5b5SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 284ce2d5b5SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 294ce2d5b5SMike Smith * SUCH DAMAGE. 304ce2d5b5SMike Smith */ 314ce2d5b5SMike Smith 324ce2d5b5SMike Smith #if defined(LIBC_SCCS) && !defined(lint) 334ce2d5b5SMike Smith static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; 344ce2d5b5SMike Smith #endif /* LIBC_SCCS and not lint */ 35ac8e56a7SBruce Evans #include <sys/cdefs.h> 36ac8e56a7SBruce Evans __FBSDID("$FreeBSD$"); 374ce2d5b5SMike Smith 386398b9c0STim J. Robbins #include "namespace.h" 394ce2d5b5SMike Smith #include <stdio.h> 404ce2d5b5SMike Smith #include <string.h> 414ce2d5b5SMike Smith #include <err.h> 424ce2d5b5SMike Smith #include <stdlib.h> 434ce2d5b5SMike Smith #include <stringlist.h> 446398b9c0STim J. Robbins #include "un-namespace.h" 454ce2d5b5SMike Smith 464ce2d5b5SMike Smith #define _SL_CHUNKSIZE 20 474ce2d5b5SMike Smith 484ce2d5b5SMike Smith /* 494ce2d5b5SMike Smith * sl_init(): Initialize a string list 504ce2d5b5SMike Smith */ 514ce2d5b5SMike Smith StringList * 524ce2d5b5SMike Smith sl_init() 534ce2d5b5SMike Smith { 544ce2d5b5SMike Smith StringList *sl = malloc(sizeof(StringList)); 554ce2d5b5SMike Smith if (sl == NULL) 56ac8e56a7SBruce Evans _err(1, "stringlist: %m"); 574ce2d5b5SMike Smith 584ce2d5b5SMike Smith sl->sl_cur = 0; 594ce2d5b5SMike Smith sl->sl_max = _SL_CHUNKSIZE; 604ce2d5b5SMike Smith sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 614ce2d5b5SMike Smith if (sl->sl_str == NULL) 62ac8e56a7SBruce Evans _err(1, "stringlist: %m"); 634ce2d5b5SMike Smith return sl; 644ce2d5b5SMike Smith } 654ce2d5b5SMike Smith 664ce2d5b5SMike Smith 674ce2d5b5SMike Smith /* 684ce2d5b5SMike Smith * sl_add(): Add an item to the string list 694ce2d5b5SMike Smith */ 704ce2d5b5SMike Smith void 714ce2d5b5SMike Smith sl_add(sl, name) 724ce2d5b5SMike Smith StringList *sl; 734ce2d5b5SMike Smith char *name; 744ce2d5b5SMike Smith { 754ce2d5b5SMike Smith if (sl->sl_cur == sl->sl_max - 1) { 764ce2d5b5SMike Smith sl->sl_max += _SL_CHUNKSIZE; 77e8420087SWarner Losh sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 784ce2d5b5SMike Smith if (sl->sl_str == NULL) 79ac8e56a7SBruce Evans _err(1, "stringlist: %m"); 804ce2d5b5SMike Smith } 814ce2d5b5SMike Smith sl->sl_str[sl->sl_cur++] = name; 824ce2d5b5SMike Smith } 834ce2d5b5SMike Smith 844ce2d5b5SMike Smith 854ce2d5b5SMike Smith /* 864ce2d5b5SMike Smith * sl_free(): Free a stringlist 874ce2d5b5SMike Smith */ 884ce2d5b5SMike Smith void 894ce2d5b5SMike Smith sl_free(sl, all) 904ce2d5b5SMike Smith StringList *sl; 914ce2d5b5SMike Smith int all; 924ce2d5b5SMike Smith { 934ce2d5b5SMike Smith size_t i; 944ce2d5b5SMike Smith 954ce2d5b5SMike Smith if (sl == NULL) 964ce2d5b5SMike Smith return; 974ce2d5b5SMike Smith if (sl->sl_str) { 984ce2d5b5SMike Smith if (all) 994ce2d5b5SMike Smith for (i = 0; i < sl->sl_cur; i++) 1004ce2d5b5SMike Smith free(sl->sl_str[i]); 1014ce2d5b5SMike Smith free(sl->sl_str); 1024ce2d5b5SMike Smith } 1034ce2d5b5SMike Smith free(sl); 1044ce2d5b5SMike Smith } 1054ce2d5b5SMike Smith 1064ce2d5b5SMike Smith 1074ce2d5b5SMike Smith /* 1084ce2d5b5SMike Smith * sl_find(): Find a name in the string list 1094ce2d5b5SMike Smith */ 1104ce2d5b5SMike Smith char * 1114ce2d5b5SMike Smith sl_find(sl, name) 1124ce2d5b5SMike Smith StringList *sl; 1134ce2d5b5SMike Smith char *name; 1144ce2d5b5SMike Smith { 1154ce2d5b5SMike Smith size_t i; 1164ce2d5b5SMike Smith 1174ce2d5b5SMike Smith for (i = 0; i < sl->sl_cur; i++) 1184ce2d5b5SMike Smith if (strcmp(sl->sl_str[i], name) == 0) 1194ce2d5b5SMike Smith return sl->sl_str[i]; 1204ce2d5b5SMike Smith 1214ce2d5b5SMike Smith return NULL; 1224ce2d5b5SMike Smith } 123