1 /* $NetBSD: stringlist.c,v 1.10 2000/01/25 16:24:40 enami Exp $ */ 2 3 /*- 4 * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 __RCSID("$NetBSD: stringlist.c,v 1.10 2000/01/25 16:24:40 enami Exp $"); 42 #endif /* LIBC_SCCS and not lint */ 43 44 #include "namespace.h" 45 46 #include <assert.h> 47 #include <err.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <stringlist.h> 52 53 #ifdef __weak_alias 54 __weak_alias(sl_add,_sl_add) 55 __weak_alias(sl_find,_sl_find) 56 __weak_alias(sl_free,_sl_free) 57 __weak_alias(sl_init,_sl_init) 58 #endif 59 60 #define _SL_CHUNKSIZE 20 61 62 /* 63 * sl_init(): Initialize a string list 64 */ 65 StringList * 66 sl_init() 67 { 68 StringList *sl; 69 70 sl = malloc(sizeof(StringList)); 71 if (sl == NULL) 72 return (NULL); 73 74 sl->sl_cur = 0; 75 sl->sl_max = _SL_CHUNKSIZE; 76 sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 77 if (sl->sl_str == NULL) { 78 free(sl); 79 sl = NULL; 80 } 81 return (sl); 82 } 83 84 85 /* 86 * sl_add(): Add an item to the string list 87 */ 88 int 89 sl_add(sl, name) 90 StringList *sl; 91 char *name; 92 { 93 94 _DIAGASSERT(sl != NULL); 95 96 if (sl->sl_cur == sl->sl_max - 1) { 97 char **new; 98 99 new = (char **)realloc(sl->sl_str, 100 (sl->sl_max + _SL_CHUNKSIZE) * sizeof(char *)); 101 if (new == NULL) 102 return (-1); 103 sl->sl_max += _SL_CHUNKSIZE; 104 sl->sl_str = new; 105 } 106 sl->sl_str[sl->sl_cur++] = name; 107 return (0); 108 } 109 110 111 /* 112 * sl_free(): Free a stringlist 113 */ 114 void 115 sl_free(sl, all) 116 StringList *sl; 117 int all; 118 { 119 size_t i; 120 121 if (sl == NULL) 122 return; 123 if (sl->sl_str) { 124 if (all) 125 for (i = 0; i < sl->sl_cur; i++) 126 free(sl->sl_str[i]); 127 free(sl->sl_str); 128 } 129 free(sl); 130 } 131 132 133 /* 134 * sl_find(): Find a name in the string list 135 */ 136 char * 137 sl_find(sl, name) 138 StringList *sl; 139 char *name; 140 { 141 size_t i; 142 143 _DIAGASSERT(sl != NULL); 144 145 for (i = 0; i < sl->sl_cur; i++) 146 /* 147 * XXX check sl->sl_str[i] != NULL? 148 */ 149 if (strcmp(sl->sl_str[i], name) == 0) 150 return (sl->sl_str[i]); 151 152 return (NULL); 153 } 154