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 * 4. The name of the author may not be used to endorse or promote products 144ce2d5b5SMike Smith * derived from this software without specific prior written permission. 154ce2d5b5SMike Smith * 164ce2d5b5SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 174ce2d5b5SMike Smith * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 184ce2d5b5SMike Smith * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 194ce2d5b5SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 204ce2d5b5SMike Smith * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 214ce2d5b5SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 224ce2d5b5SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 234ce2d5b5SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 244ce2d5b5SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 254ce2d5b5SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 264ce2d5b5SMike Smith * SUCH DAMAGE. 274ce2d5b5SMike Smith */ 284ce2d5b5SMike Smith 294ce2d5b5SMike Smith #if defined(LIBC_SCCS) && !defined(lint) 304ce2d5b5SMike Smith static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; 314ce2d5b5SMike Smith #endif /* LIBC_SCCS and not lint */ 32ac8e56a7SBruce Evans #include <sys/cdefs.h> 33ac8e56a7SBruce Evans __FBSDID("$FreeBSD$"); 344ce2d5b5SMike Smith 356398b9c0STim J. Robbins #include "namespace.h" 364ce2d5b5SMike Smith #include <stdio.h> 374ce2d5b5SMike Smith #include <string.h> 384ce2d5b5SMike Smith #include <err.h> 394ce2d5b5SMike Smith #include <stdlib.h> 404ce2d5b5SMike Smith #include <stringlist.h> 416398b9c0STim J. Robbins #include "un-namespace.h" 424ce2d5b5SMike Smith 434ce2d5b5SMike Smith #define _SL_CHUNKSIZE 20 444ce2d5b5SMike Smith 454ce2d5b5SMike Smith /* 464ce2d5b5SMike Smith * sl_init(): Initialize a string list 474ce2d5b5SMike Smith */ 484ce2d5b5SMike Smith StringList * 49240f872cSEitan Adler sl_init(void) 504ce2d5b5SMike Smith { 510f38d8d9SDavid E. O'Brien StringList *sl; 520f38d8d9SDavid E. O'Brien 530f38d8d9SDavid E. O'Brien sl = malloc(sizeof(StringList)); 544ce2d5b5SMike Smith if (sl == NULL) 55ac8e56a7SBruce Evans _err(1, "stringlist: %m"); 564ce2d5b5SMike Smith 574ce2d5b5SMike Smith sl->sl_cur = 0; 584ce2d5b5SMike Smith sl->sl_max = _SL_CHUNKSIZE; 594ce2d5b5SMike Smith sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 604ce2d5b5SMike Smith if (sl->sl_str == NULL) 61ac8e56a7SBruce Evans _err(1, "stringlist: %m"); 624ce2d5b5SMike Smith return sl; 634ce2d5b5SMike Smith } 644ce2d5b5SMike Smith 654ce2d5b5SMike Smith 664ce2d5b5SMike Smith /* 674ce2d5b5SMike Smith * sl_add(): Add an item to the string list 684ce2d5b5SMike Smith */ 690f38d8d9SDavid E. O'Brien int 70240f872cSEitan Adler sl_add(StringList *sl, char *name) 714ce2d5b5SMike Smith { 724ce2d5b5SMike Smith if (sl->sl_cur == sl->sl_max - 1) { 734ce2d5b5SMike Smith sl->sl_max += _SL_CHUNKSIZE; 74e8420087SWarner Losh sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 754ce2d5b5SMike Smith if (sl->sl_str == NULL) 760f38d8d9SDavid E. O'Brien return (-1); 774ce2d5b5SMike Smith } 784ce2d5b5SMike Smith sl->sl_str[sl->sl_cur++] = name; 790f38d8d9SDavid E. O'Brien return (0); 804ce2d5b5SMike Smith } 814ce2d5b5SMike Smith 824ce2d5b5SMike Smith 834ce2d5b5SMike Smith /* 844ce2d5b5SMike Smith * sl_free(): Free a stringlist 854ce2d5b5SMike Smith */ 864ce2d5b5SMike Smith void 87240f872cSEitan Adler sl_free(StringList *sl, int all) 884ce2d5b5SMike Smith { 894ce2d5b5SMike Smith size_t i; 904ce2d5b5SMike Smith 914ce2d5b5SMike Smith if (sl == NULL) 924ce2d5b5SMike Smith return; 934ce2d5b5SMike Smith if (sl->sl_str) { 944ce2d5b5SMike Smith if (all) 954ce2d5b5SMike Smith for (i = 0; i < sl->sl_cur; i++) 964ce2d5b5SMike Smith free(sl->sl_str[i]); 974ce2d5b5SMike Smith free(sl->sl_str); 984ce2d5b5SMike Smith } 994ce2d5b5SMike Smith free(sl); 1004ce2d5b5SMike Smith } 1014ce2d5b5SMike Smith 1024ce2d5b5SMike Smith 1034ce2d5b5SMike Smith /* 1044ce2d5b5SMike Smith * sl_find(): Find a name in the string list 1054ce2d5b5SMike Smith */ 1064ce2d5b5SMike Smith char * 107*d001beb2SEitan Adler sl_find(StringList *sl, const char *name) 1084ce2d5b5SMike Smith { 1094ce2d5b5SMike Smith size_t i; 1104ce2d5b5SMike Smith 1114ce2d5b5SMike Smith for (i = 0; i < sl->sl_cur; i++) 1124ce2d5b5SMike Smith if (strcmp(sl->sl_str[i], name) == 0) 1134ce2d5b5SMike Smith return sl->sl_str[i]; 1144ce2d5b5SMike Smith 1154ce2d5b5SMike Smith return NULL; 1164ce2d5b5SMike Smith } 117