xref: /freebsd/lib/libc/gen/stringlist.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1d915a14eSPedro F. Giffuni /*-
2d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
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  *
164ce2d5b5SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
174ce2d5b5SMike Smith  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
184ce2d5b5SMike Smith  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194ce2d5b5SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
204ce2d5b5SMike Smith  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214ce2d5b5SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224ce2d5b5SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234ce2d5b5SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244ce2d5b5SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254ce2d5b5SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264ce2d5b5SMike Smith  * SUCH DAMAGE.
274ce2d5b5SMike Smith  */
284ce2d5b5SMike Smith 
29ac8e56a7SBruce Evans #include <sys/cdefs.h>
30*c1920558SJohn Baldwin __RCSID("$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $");
316398b9c0STim J. Robbins #include "namespace.h"
324ce2d5b5SMike Smith #include <stdio.h>
334ce2d5b5SMike Smith #include <string.h>
344ce2d5b5SMike Smith #include <err.h>
354ce2d5b5SMike Smith #include <stdlib.h>
364ce2d5b5SMike Smith #include <stringlist.h>
376398b9c0STim J. Robbins #include "un-namespace.h"
384ce2d5b5SMike Smith 
394ce2d5b5SMike Smith #define _SL_CHUNKSIZE	20
404ce2d5b5SMike Smith 
414ce2d5b5SMike Smith /*
424ce2d5b5SMike Smith  * sl_init(): Initialize a string list
434ce2d5b5SMike Smith  */
444ce2d5b5SMike Smith StringList *
sl_init(void)45240f872cSEitan Adler sl_init(void)
464ce2d5b5SMike Smith {
470f38d8d9SDavid E. O'Brien 	StringList *sl;
480f38d8d9SDavid E. O'Brien 
490f38d8d9SDavid E. O'Brien 	sl = malloc(sizeof(StringList));
504ce2d5b5SMike Smith 	if (sl == NULL)
51ac8e56a7SBruce Evans 		_err(1, "stringlist: %m");
524ce2d5b5SMike Smith 
534ce2d5b5SMike Smith 	sl->sl_cur = 0;
544ce2d5b5SMike Smith 	sl->sl_max = _SL_CHUNKSIZE;
554ce2d5b5SMike Smith 	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
564ce2d5b5SMike Smith 	if (sl->sl_str == NULL)
57ac8e56a7SBruce Evans 		_err(1, "stringlist: %m");
584ce2d5b5SMike Smith 	return sl;
594ce2d5b5SMike Smith }
604ce2d5b5SMike Smith 
614ce2d5b5SMike Smith 
624ce2d5b5SMike Smith /*
634ce2d5b5SMike Smith  * sl_add(): Add an item to the string list
644ce2d5b5SMike Smith  */
650f38d8d9SDavid E. O'Brien int
sl_add(StringList * sl,char * name)66240f872cSEitan Adler sl_add(StringList *sl, char *name)
674ce2d5b5SMike Smith {
684ce2d5b5SMike Smith 	if (sl->sl_cur == sl->sl_max - 1) {
694ce2d5b5SMike Smith 		sl->sl_max += _SL_CHUNKSIZE;
70e8420087SWarner Losh 		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
714ce2d5b5SMike Smith 		if (sl->sl_str == NULL)
720f38d8d9SDavid E. O'Brien 			return (-1);
734ce2d5b5SMike Smith 	}
744ce2d5b5SMike Smith 	sl->sl_str[sl->sl_cur++] = name;
750f38d8d9SDavid E. O'Brien 	return (0);
764ce2d5b5SMike Smith }
774ce2d5b5SMike Smith 
784ce2d5b5SMike Smith 
794ce2d5b5SMike Smith /*
804ce2d5b5SMike Smith  * sl_free(): Free a stringlist
814ce2d5b5SMike Smith  */
824ce2d5b5SMike Smith void
sl_free(StringList * sl,int all)83240f872cSEitan Adler sl_free(StringList *sl, int all)
844ce2d5b5SMike Smith {
854ce2d5b5SMike Smith 	size_t i;
864ce2d5b5SMike Smith 
874ce2d5b5SMike Smith 	if (sl == NULL)
884ce2d5b5SMike Smith 		return;
894ce2d5b5SMike Smith 	if (sl->sl_str) {
904ce2d5b5SMike Smith 		if (all)
914ce2d5b5SMike Smith 			for (i = 0; i < sl->sl_cur; i++)
924ce2d5b5SMike Smith 				free(sl->sl_str[i]);
934ce2d5b5SMike Smith 		free(sl->sl_str);
944ce2d5b5SMike Smith 	}
954ce2d5b5SMike Smith 	free(sl);
964ce2d5b5SMike Smith }
974ce2d5b5SMike Smith 
984ce2d5b5SMike Smith 
994ce2d5b5SMike Smith /*
1004ce2d5b5SMike Smith  * sl_find(): Find a name in the string list
1014ce2d5b5SMike Smith  */
1024ce2d5b5SMike Smith char *
sl_find(StringList * sl,const char * name)103d001beb2SEitan Adler sl_find(StringList *sl, const char *name)
1044ce2d5b5SMike Smith {
1054ce2d5b5SMike Smith 	size_t i;
1064ce2d5b5SMike Smith 
1074ce2d5b5SMike Smith 	for (i = 0; i < sl->sl_cur; i++)
1084ce2d5b5SMike Smith 		if (strcmp(sl->sl_str[i], name) == 0)
1094ce2d5b5SMike Smith 			return sl->sl_str[i];
1104ce2d5b5SMike Smith 
1114ce2d5b5SMike Smith 	return NULL;
1124ce2d5b5SMike Smith }
113