1d915a14eSPedro F. Giffuni /*- 2d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause 3d915a14eSPedro F. Giffuni * 44ce2d5b5SMike Smith * Copyright (c) 1994 Christos Zoulas 54ce2d5b5SMike Smith * All rights reserved. 64ce2d5b5SMike Smith * 74ce2d5b5SMike Smith * Redistribution and use in source and binary forms, with or without 84ce2d5b5SMike Smith * modification, are permitted provided that the following conditions 94ce2d5b5SMike Smith * are met: 104ce2d5b5SMike Smith * 1. Redistributions of source code must retain the above copyright 114ce2d5b5SMike Smith * notice, this list of conditions and the following disclaimer. 124ce2d5b5SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 134ce2d5b5SMike Smith * notice, this list of conditions and the following disclaimer in the 144ce2d5b5SMike Smith * documentation and/or other materials provided with the distribution. 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 29ac8e56a7SBruce Evans #include <sys/cdefs.h> 30*c1920558SJohn Baldwin __RCSID("$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"); 31ac8e56a7SBruce Evans __FBSDID("$FreeBSD$"); 324ce2d5b5SMike Smith 336398b9c0STim J. Robbins #include "namespace.h" 344ce2d5b5SMike Smith #include <stdio.h> 354ce2d5b5SMike Smith #include <string.h> 364ce2d5b5SMike Smith #include <err.h> 374ce2d5b5SMike Smith #include <stdlib.h> 384ce2d5b5SMike Smith #include <stringlist.h> 396398b9c0STim J. Robbins #include "un-namespace.h" 404ce2d5b5SMike Smith 414ce2d5b5SMike Smith #define _SL_CHUNKSIZE 20 424ce2d5b5SMike Smith 434ce2d5b5SMike Smith /* 444ce2d5b5SMike Smith * sl_init(): Initialize a string list 454ce2d5b5SMike Smith */ 464ce2d5b5SMike Smith StringList * 47240f872cSEitan Adler sl_init(void) 484ce2d5b5SMike Smith { 490f38d8d9SDavid E. O'Brien StringList *sl; 500f38d8d9SDavid E. O'Brien 510f38d8d9SDavid E. O'Brien sl = malloc(sizeof(StringList)); 524ce2d5b5SMike Smith if (sl == NULL) 53ac8e56a7SBruce Evans _err(1, "stringlist: %m"); 544ce2d5b5SMike Smith 554ce2d5b5SMike Smith sl->sl_cur = 0; 564ce2d5b5SMike Smith sl->sl_max = _SL_CHUNKSIZE; 574ce2d5b5SMike Smith sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 584ce2d5b5SMike Smith if (sl->sl_str == NULL) 59ac8e56a7SBruce Evans _err(1, "stringlist: %m"); 604ce2d5b5SMike Smith return sl; 614ce2d5b5SMike Smith } 624ce2d5b5SMike Smith 634ce2d5b5SMike Smith 644ce2d5b5SMike Smith /* 654ce2d5b5SMike Smith * sl_add(): Add an item to the string list 664ce2d5b5SMike Smith */ 670f38d8d9SDavid E. O'Brien int 68240f872cSEitan Adler sl_add(StringList *sl, char *name) 694ce2d5b5SMike Smith { 704ce2d5b5SMike Smith if (sl->sl_cur == sl->sl_max - 1) { 714ce2d5b5SMike Smith sl->sl_max += _SL_CHUNKSIZE; 72e8420087SWarner Losh sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 734ce2d5b5SMike Smith if (sl->sl_str == NULL) 740f38d8d9SDavid E. O'Brien return (-1); 754ce2d5b5SMike Smith } 764ce2d5b5SMike Smith sl->sl_str[sl->sl_cur++] = name; 770f38d8d9SDavid E. O'Brien return (0); 784ce2d5b5SMike Smith } 794ce2d5b5SMike Smith 804ce2d5b5SMike Smith 814ce2d5b5SMike Smith /* 824ce2d5b5SMike Smith * sl_free(): Free a stringlist 834ce2d5b5SMike Smith */ 844ce2d5b5SMike Smith void 85240f872cSEitan Adler sl_free(StringList *sl, int all) 864ce2d5b5SMike Smith { 874ce2d5b5SMike Smith size_t i; 884ce2d5b5SMike Smith 894ce2d5b5SMike Smith if (sl == NULL) 904ce2d5b5SMike Smith return; 914ce2d5b5SMike Smith if (sl->sl_str) { 924ce2d5b5SMike Smith if (all) 934ce2d5b5SMike Smith for (i = 0; i < sl->sl_cur; i++) 944ce2d5b5SMike Smith free(sl->sl_str[i]); 954ce2d5b5SMike Smith free(sl->sl_str); 964ce2d5b5SMike Smith } 974ce2d5b5SMike Smith free(sl); 984ce2d5b5SMike Smith } 994ce2d5b5SMike Smith 1004ce2d5b5SMike Smith 1014ce2d5b5SMike Smith /* 1024ce2d5b5SMike Smith * sl_find(): Find a name in the string list 1034ce2d5b5SMike Smith */ 1044ce2d5b5SMike Smith char * 105d001beb2SEitan Adler sl_find(StringList *sl, const char *name) 1064ce2d5b5SMike Smith { 1074ce2d5b5SMike Smith size_t i; 1084ce2d5b5SMike Smith 1094ce2d5b5SMike Smith for (i = 0; i < sl->sl_cur; i++) 1104ce2d5b5SMike Smith if (strcmp(sl->sl_str[i], name) == 0) 1114ce2d5b5SMike Smith return sl->sl_str[i]; 1124ce2d5b5SMike Smith 1134ce2d5b5SMike Smith return NULL; 1144ce2d5b5SMike Smith } 115