1 /* 2 * Copyright (c) 1994 Christos Zoulas 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 15 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #ifdef FSTACK 28 #include "compat.h" 29 #include <errno.h> 30 #endif 31 32 #if defined(LIBC_SCCS) && !defined(lint) 33 static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $"; 34 #endif /* LIBC_SCCS and not lint */ 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <stdio.h> 39 #include <string.h> 40 #include <err.h> 41 #include <stdlib.h> 42 #include <stringlist.h> 43 44 #define _SL_CHUNKSIZE 20 45 46 /* 47 * sl_init(): Initialize a string list 48 */ 49 StringList * 50 sl_init(void) 51 { 52 StringList *sl; 53 54 sl = malloc(sizeof(StringList)); 55 if (sl == NULL) 56 err(1, "stringlist: %d, %s\n", errno, strerror(errno)); 57 58 sl->sl_cur = 0; 59 sl->sl_max = _SL_CHUNKSIZE; 60 sl->sl_str = malloc(sl->sl_max * sizeof(char *)); 61 if (sl->sl_str == NULL) 62 err(1, "stringlist: %d, %s\n", errno, strerror(errno)); 63 return sl; 64 } 65 66 67 /* 68 * sl_add(): Add an item to the string list 69 */ 70 int 71 sl_add(StringList *sl, char *name) 72 { 73 if (sl->sl_cur == sl->sl_max - 1) { 74 sl->sl_max += _SL_CHUNKSIZE; 75 sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *)); 76 if (sl->sl_str == NULL) 77 return (-1); 78 } 79 sl->sl_str[sl->sl_cur++] = name; 80 return (0); 81 } 82 83 84 /* 85 * sl_free(): Free a stringlist 86 */ 87 void 88 sl_free(StringList *sl, int all) 89 { 90 size_t i; 91 92 if (sl == NULL) 93 return; 94 if (sl->sl_str) { 95 if (all) 96 for (i = 0; i < sl->sl_cur; i++) 97 free(sl->sl_str[i]); 98 free(sl->sl_str); 99 } 100 free(sl); 101 } 102 103 104 /* 105 * sl_find(): Find a name in the string list 106 */ 107 char * 108 sl_find(StringList *sl, const char *name) 109 { 110 size_t i; 111 112 for (i = 0; i < sl->sl_cur; i++) 113 if (strcmp(sl->sl_str[i], name) == 0) 114 return sl->sl_str[i]; 115 116 return NULL; 117 } 118 119