xref: /freebsd/lib/libc/gen/stringlist.c (revision e8420087b0ae4a2d0611cd2f6413d150cfc83554)
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 
344ce2d5b5SMike Smith #if defined(LIBC_SCCS) && !defined(lint)
354ce2d5b5SMike Smith static char *rcsid = "$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $";
364ce2d5b5SMike Smith #endif /* LIBC_SCCS and not lint */
374ce2d5b5SMike Smith 
384ce2d5b5SMike Smith #include <stdio.h>
394ce2d5b5SMike Smith #include <string.h>
404ce2d5b5SMike Smith #include <err.h>
414ce2d5b5SMike Smith #include <stdlib.h>
424ce2d5b5SMike Smith #include <stringlist.h>
434ce2d5b5SMike Smith 
444ce2d5b5SMike Smith #define _SL_CHUNKSIZE	20
454ce2d5b5SMike Smith 
464ce2d5b5SMike Smith /*
474ce2d5b5SMike Smith  * sl_init(): Initialize a string list
484ce2d5b5SMike Smith  */
494ce2d5b5SMike Smith StringList *
504ce2d5b5SMike Smith sl_init()
514ce2d5b5SMike Smith {
524ce2d5b5SMike Smith 	StringList *sl = malloc(sizeof(StringList));
534ce2d5b5SMike Smith 	if (sl == NULL)
5451e69860SJordan K. Hubbard 		err(1, "stringlist: %m");
554ce2d5b5SMike Smith 
564ce2d5b5SMike Smith 	sl->sl_cur = 0;
574ce2d5b5SMike Smith 	sl->sl_max = _SL_CHUNKSIZE;
584ce2d5b5SMike Smith 	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
594ce2d5b5SMike Smith 	if (sl->sl_str == NULL)
6051e69860SJordan K. Hubbard 		err(1, "stringlist: %m");
614ce2d5b5SMike Smith 	return sl;
624ce2d5b5SMike Smith }
634ce2d5b5SMike Smith 
644ce2d5b5SMike Smith 
654ce2d5b5SMike Smith /*
664ce2d5b5SMike Smith  * sl_add(): Add an item to the string list
674ce2d5b5SMike Smith  */
684ce2d5b5SMike Smith void
694ce2d5b5SMike Smith sl_add(sl, name)
704ce2d5b5SMike Smith 	StringList *sl;
714ce2d5b5SMike Smith 	char *name;
724ce2d5b5SMike Smith {
734ce2d5b5SMike Smith 	if (sl->sl_cur == sl->sl_max - 1) {
744ce2d5b5SMike Smith 		sl->sl_max += _SL_CHUNKSIZE;
75e8420087SWarner Losh 		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
764ce2d5b5SMike Smith 		if (sl->sl_str == NULL)
7751e69860SJordan K. Hubbard 			err(1, "stringlist: %m");
784ce2d5b5SMike Smith 	}
794ce2d5b5SMike Smith 	sl->sl_str[sl->sl_cur++] = name;
804ce2d5b5SMike Smith }
814ce2d5b5SMike Smith 
824ce2d5b5SMike Smith 
834ce2d5b5SMike Smith /*
844ce2d5b5SMike Smith  * sl_free(): Free a stringlist
854ce2d5b5SMike Smith  */
864ce2d5b5SMike Smith void
874ce2d5b5SMike Smith sl_free(sl, all)
884ce2d5b5SMike Smith 	StringList *sl;
894ce2d5b5SMike Smith 	int all;
904ce2d5b5SMike Smith {
914ce2d5b5SMike Smith 	size_t i;
924ce2d5b5SMike Smith 
934ce2d5b5SMike Smith 	if (sl == NULL)
944ce2d5b5SMike Smith 		return;
954ce2d5b5SMike Smith 	if (sl->sl_str) {
964ce2d5b5SMike Smith 		if (all)
974ce2d5b5SMike Smith 			for (i = 0; i < sl->sl_cur; i++)
984ce2d5b5SMike Smith 				free(sl->sl_str[i]);
994ce2d5b5SMike Smith 		free(sl->sl_str);
1004ce2d5b5SMike Smith 	}
1014ce2d5b5SMike Smith 	free(sl);
1024ce2d5b5SMike Smith }
1034ce2d5b5SMike Smith 
1044ce2d5b5SMike Smith 
1054ce2d5b5SMike Smith /*
1064ce2d5b5SMike Smith  * sl_find(): Find a name in the string list
1074ce2d5b5SMike Smith  */
1084ce2d5b5SMike Smith char *
1094ce2d5b5SMike Smith sl_find(sl, name)
1104ce2d5b5SMike Smith 	StringList *sl;
1114ce2d5b5SMike Smith 	char *name;
1124ce2d5b5SMike Smith {
1134ce2d5b5SMike Smith 	size_t i;
1144ce2d5b5SMike Smith 
1154ce2d5b5SMike Smith 	for (i = 0; i < sl->sl_cur; i++)
1164ce2d5b5SMike Smith 		if (strcmp(sl->sl_str[i], name) == 0)
1174ce2d5b5SMike Smith 			return sl->sl_str[i];
1184ce2d5b5SMike Smith 
1194ce2d5b5SMike Smith 	return NULL;
1204ce2d5b5SMike Smith }
121