xref: /freebsd/lib/libc/gen/stringlist.c (revision ac8e56a7f941dd3a327bfebbcc6568d05d184989)
14ce2d5b5SMike Smith /*	$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $	*/
24ce2d5b5SMike Smith 
34ce2d5b5SMike Smith /*
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  * 3. All advertising materials mentioning features or use of this software
164ce2d5b5SMike Smith  *    must display the following acknowledgement:
174ce2d5b5SMike Smith  *	This product includes software developed by Christos Zoulas.
184ce2d5b5SMike Smith  * 4. The name of the author may not be used to endorse or promote products
194ce2d5b5SMike Smith  *    derived from this software without specific prior written permission.
204ce2d5b5SMike Smith  *
214ce2d5b5SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
224ce2d5b5SMike Smith  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
234ce2d5b5SMike Smith  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244ce2d5b5SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
254ce2d5b5SMike Smith  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264ce2d5b5SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274ce2d5b5SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284ce2d5b5SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294ce2d5b5SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304ce2d5b5SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314ce2d5b5SMike Smith  * SUCH DAMAGE.
324ce2d5b5SMike Smith  */
334ce2d5b5SMike Smith 
34ac8e56a7SBruce Evans #if 0
354ce2d5b5SMike Smith #if defined(LIBC_SCCS) && !defined(lint)
364ce2d5b5SMike Smith static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $";
374ce2d5b5SMike Smith #endif /* LIBC_SCCS and not lint */
38ac8e56a7SBruce Evans #endif
39ac8e56a7SBruce Evans 
40ac8e56a7SBruce Evans #include <sys/cdefs.h>
41ac8e56a7SBruce Evans __FBSDID("$FreeBSD$");
424ce2d5b5SMike Smith 
434ce2d5b5SMike Smith #include <stdio.h>
444ce2d5b5SMike Smith #include <string.h>
454ce2d5b5SMike Smith #include <err.h>
464ce2d5b5SMike Smith #include <stdlib.h>
474ce2d5b5SMike Smith #include <stringlist.h>
484ce2d5b5SMike Smith 
494ce2d5b5SMike Smith #define _SL_CHUNKSIZE	20
504ce2d5b5SMike Smith 
514ce2d5b5SMike Smith /*
524ce2d5b5SMike Smith  * sl_init(): Initialize a string list
534ce2d5b5SMike Smith  */
544ce2d5b5SMike Smith StringList *
554ce2d5b5SMike Smith sl_init()
564ce2d5b5SMike Smith {
574ce2d5b5SMike Smith 	StringList *sl = malloc(sizeof(StringList));
584ce2d5b5SMike Smith 	if (sl == NULL)
59ac8e56a7SBruce Evans 		_err(1, "stringlist: %m");
604ce2d5b5SMike Smith 
614ce2d5b5SMike Smith 	sl->sl_cur = 0;
624ce2d5b5SMike Smith 	sl->sl_max = _SL_CHUNKSIZE;
634ce2d5b5SMike Smith 	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
644ce2d5b5SMike Smith 	if (sl->sl_str == NULL)
65ac8e56a7SBruce Evans 		_err(1, "stringlist: %m");
664ce2d5b5SMike Smith 	return sl;
674ce2d5b5SMike Smith }
684ce2d5b5SMike Smith 
694ce2d5b5SMike Smith 
704ce2d5b5SMike Smith /*
714ce2d5b5SMike Smith  * sl_add(): Add an item to the string list
724ce2d5b5SMike Smith  */
734ce2d5b5SMike Smith void
744ce2d5b5SMike Smith sl_add(sl, name)
754ce2d5b5SMike Smith 	StringList *sl;
764ce2d5b5SMike Smith 	char *name;
774ce2d5b5SMike Smith {
784ce2d5b5SMike Smith 	if (sl->sl_cur == sl->sl_max - 1) {
794ce2d5b5SMike Smith 		sl->sl_max += _SL_CHUNKSIZE;
80e8420087SWarner Losh 		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
814ce2d5b5SMike Smith 		if (sl->sl_str == NULL)
82ac8e56a7SBruce Evans 			_err(1, "stringlist: %m");
834ce2d5b5SMike Smith 	}
844ce2d5b5SMike Smith 	sl->sl_str[sl->sl_cur++] = name;
854ce2d5b5SMike Smith }
864ce2d5b5SMike Smith 
874ce2d5b5SMike Smith 
884ce2d5b5SMike Smith /*
894ce2d5b5SMike Smith  * sl_free(): Free a stringlist
904ce2d5b5SMike Smith  */
914ce2d5b5SMike Smith void
924ce2d5b5SMike Smith sl_free(sl, all)
934ce2d5b5SMike Smith 	StringList *sl;
944ce2d5b5SMike Smith 	int all;
954ce2d5b5SMike Smith {
964ce2d5b5SMike Smith 	size_t i;
974ce2d5b5SMike Smith 
984ce2d5b5SMike Smith 	if (sl == NULL)
994ce2d5b5SMike Smith 		return;
1004ce2d5b5SMike Smith 	if (sl->sl_str) {
1014ce2d5b5SMike Smith 		if (all)
1024ce2d5b5SMike Smith 			for (i = 0; i < sl->sl_cur; i++)
1034ce2d5b5SMike Smith 				free(sl->sl_str[i]);
1044ce2d5b5SMike Smith 		free(sl->sl_str);
1054ce2d5b5SMike Smith 	}
1064ce2d5b5SMike Smith 	free(sl);
1074ce2d5b5SMike Smith }
1084ce2d5b5SMike Smith 
1094ce2d5b5SMike Smith 
1104ce2d5b5SMike Smith /*
1114ce2d5b5SMike Smith  * sl_find(): Find a name in the string list
1124ce2d5b5SMike Smith  */
1134ce2d5b5SMike Smith char *
1144ce2d5b5SMike Smith sl_find(sl, name)
1154ce2d5b5SMike Smith 	StringList *sl;
1164ce2d5b5SMike Smith 	char *name;
1174ce2d5b5SMike Smith {
1184ce2d5b5SMike Smith 	size_t i;
1194ce2d5b5SMike Smith 
1204ce2d5b5SMike Smith 	for (i = 0; i < sl->sl_cur; i++)
1214ce2d5b5SMike Smith 		if (strcmp(sl->sl_str[i], name) == 0)
1224ce2d5b5SMike Smith 			return sl->sl_str[i];
1234ce2d5b5SMike Smith 
1244ce2d5b5SMike Smith 	return NULL;
1254ce2d5b5SMike Smith }
126