Home | History | Annotate | Line # | Download | only in gen
stringlist.c revision 1.6.4.1
      1 /*	$NetBSD: stringlist.c,v 1.6.4.1 1999/12/27 18:29:38 wrstuden 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.6.4.1 1999/12/27 18:29:38 wrstuden 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 		sl->sl_max += _SL_CHUNKSIZE;
    100 		new = (char **)realloc(sl->sl_str, sl->sl_max * sizeof(char *));
    101 		if (new == NULL)
    102 			return (-1);
    103 		sl->sl_str = new;
    104 	}
    105 	sl->sl_str[sl->sl_cur++] = name;
    106 	return (0);
    107 }
    108 
    109 
    110 /*
    111  * sl_free(): Free a stringlist
    112  */
    113 void
    114 sl_free(sl, all)
    115 	StringList *sl;
    116 	int all;
    117 {
    118 	size_t i;
    119 
    120 	if (sl == NULL)
    121 		return;
    122 	if (sl->sl_str) {
    123 		if (all)
    124 			for (i = 0; i < sl->sl_cur; i++)
    125 				free(sl->sl_str[i]);
    126 		free(sl->sl_str);
    127 	}
    128 	free(sl);
    129 }
    130 
    131 
    132 /*
    133  * sl_find(): Find a name in the string list
    134  */
    135 char *
    136 sl_find(sl, name)
    137 	StringList *sl;
    138 	char *name;
    139 {
    140 	size_t i;
    141 
    142 	_DIAGASSERT(sl != NULL);
    143 
    144 	for (i = 0; i < sl->sl_cur; i++)
    145 			/*
    146 			 * XXX check sl->sl_str[i] != NULL?
    147 			 */
    148 		if (strcmp(sl->sl_str[i], name) == 0)
    149 			return sl->sl_str[i];
    150 
    151 	return (NULL);
    152 }
    153