1*6d38604fSBaptiste Daroussin /*	$Id: compat_stringlist.c,v 1.8 2020/06/15 21:48:09 schwarze Exp $ */
2*6d38604fSBaptiste Daroussin /*	$NetBSD: stringlist.c,v 1.14 2015/05/21 01:29:13 christos Exp $	*/
361d06d6bSBaptiste Daroussin 
4*6d38604fSBaptiste Daroussin /*-
5*6d38604fSBaptiste Daroussin  * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
661d06d6bSBaptiste Daroussin  * All rights reserved.
761d06d6bSBaptiste Daroussin  *
8*6d38604fSBaptiste Daroussin  * This code is derived from software contributed to The NetBSD Foundation
9*6d38604fSBaptiste Daroussin  * by Christos Zoulas.
10*6d38604fSBaptiste Daroussin  *
1161d06d6bSBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
1261d06d6bSBaptiste Daroussin  * modification, are permitted provided that the following conditions
1361d06d6bSBaptiste Daroussin  * are met:
1461d06d6bSBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
1561d06d6bSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
1661d06d6bSBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
1761d06d6bSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in the
1861d06d6bSBaptiste Daroussin  *    documentation and/or other materials provided with the distribution.
1961d06d6bSBaptiste Daroussin  *
20*6d38604fSBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*6d38604fSBaptiste Daroussin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*6d38604fSBaptiste Daroussin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*6d38604fSBaptiste Daroussin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*6d38604fSBaptiste Daroussin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*6d38604fSBaptiste Daroussin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*6d38604fSBaptiste Daroussin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*6d38604fSBaptiste Daroussin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*6d38604fSBaptiste Daroussin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*6d38604fSBaptiste Daroussin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*6d38604fSBaptiste Daroussin  * POSSIBILITY OF SUCH DAMAGE.
3161d06d6bSBaptiste Daroussin  */
32*6d38604fSBaptiste Daroussin #include "config.h"
3361d06d6bSBaptiste Daroussin 
3461d06d6bSBaptiste Daroussin #include <stdlib.h>
3561d06d6bSBaptiste Daroussin #include <string.h>
3661d06d6bSBaptiste Daroussin #include "compat_stringlist.h"
3761d06d6bSBaptiste Daroussin 
3861d06d6bSBaptiste Daroussin #define _SL_CHUNKSIZE	20
3961d06d6bSBaptiste Daroussin 
4061d06d6bSBaptiste Daroussin /*
4161d06d6bSBaptiste Daroussin  * sl_init(): Initialize a string list
4261d06d6bSBaptiste Daroussin  */
4361d06d6bSBaptiste Daroussin StringList *
sl_init(void)4461d06d6bSBaptiste Daroussin sl_init(void)
4561d06d6bSBaptiste Daroussin {
4661d06d6bSBaptiste Daroussin 	StringList *sl;
4761d06d6bSBaptiste Daroussin 
4861d06d6bSBaptiste Daroussin 	sl = malloc(sizeof(StringList));
4961d06d6bSBaptiste Daroussin 	if (sl == NULL)
50*6d38604fSBaptiste Daroussin 		return NULL;
5161d06d6bSBaptiste Daroussin 
5261d06d6bSBaptiste Daroussin 	sl->sl_cur = 0;
5361d06d6bSBaptiste Daroussin 	sl->sl_max = _SL_CHUNKSIZE;
5461d06d6bSBaptiste Daroussin 	sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
55*6d38604fSBaptiste Daroussin 	if (sl->sl_str == NULL) {
56*6d38604fSBaptiste Daroussin 		free(sl);
57*6d38604fSBaptiste Daroussin 		sl = NULL;
58*6d38604fSBaptiste Daroussin 	}
5961d06d6bSBaptiste Daroussin 	return sl;
6061d06d6bSBaptiste Daroussin }
6161d06d6bSBaptiste Daroussin 
6261d06d6bSBaptiste Daroussin 
6361d06d6bSBaptiste Daroussin /*
6461d06d6bSBaptiste Daroussin  * sl_add(): Add an item to the string list
6561d06d6bSBaptiste Daroussin  */
6661d06d6bSBaptiste Daroussin int
sl_add(StringList * sl,char * name)6761d06d6bSBaptiste Daroussin sl_add(StringList *sl, char *name)
6861d06d6bSBaptiste Daroussin {
6961d06d6bSBaptiste Daroussin 	if (sl->sl_cur == sl->sl_max - 1) {
70*6d38604fSBaptiste Daroussin 		char	**new;
71*6d38604fSBaptiste Daroussin 
72*6d38604fSBaptiste Daroussin 		new = reallocarray(sl->sl_str, (sl->sl_max + _SL_CHUNKSIZE),
73*6d38604fSBaptiste Daroussin 		    sizeof(char *));
74*6d38604fSBaptiste Daroussin 		if (new == NULL)
75*6d38604fSBaptiste Daroussin 			return -1;
7661d06d6bSBaptiste Daroussin 		sl->sl_max += _SL_CHUNKSIZE;
77*6d38604fSBaptiste Daroussin 		sl->sl_str = new;
7861d06d6bSBaptiste Daroussin 	}
7961d06d6bSBaptiste Daroussin 	sl->sl_str[sl->sl_cur++] = name;
80*6d38604fSBaptiste Daroussin 	return 0;
8161d06d6bSBaptiste Daroussin }
8261d06d6bSBaptiste Daroussin 
8361d06d6bSBaptiste Daroussin 
8461d06d6bSBaptiste Daroussin /*
8561d06d6bSBaptiste Daroussin  * sl_free(): Free a stringlist
8661d06d6bSBaptiste Daroussin  */
8761d06d6bSBaptiste Daroussin void
sl_free(StringList * sl,int all)8861d06d6bSBaptiste Daroussin sl_free(StringList *sl, int all)
8961d06d6bSBaptiste Daroussin {
9061d06d6bSBaptiste Daroussin 	size_t i;
9161d06d6bSBaptiste Daroussin 
9261d06d6bSBaptiste Daroussin 	if (sl == NULL)
9361d06d6bSBaptiste Daroussin 		return;
9461d06d6bSBaptiste Daroussin 	if (sl->sl_str) {
9561d06d6bSBaptiste Daroussin 		if (all)
9661d06d6bSBaptiste Daroussin 			for (i = 0; i < sl->sl_cur; i++)
9761d06d6bSBaptiste Daroussin 				free(sl->sl_str[i]);
9861d06d6bSBaptiste Daroussin 		free(sl->sl_str);
9961d06d6bSBaptiste Daroussin 	}
10061d06d6bSBaptiste Daroussin 	free(sl);
10161d06d6bSBaptiste Daroussin }
10261d06d6bSBaptiste Daroussin 
10361d06d6bSBaptiste Daroussin 
10461d06d6bSBaptiste Daroussin /*
10561d06d6bSBaptiste Daroussin  * sl_find(): Find a name in the string list
10661d06d6bSBaptiste Daroussin  */
10761d06d6bSBaptiste Daroussin char *
sl_find(StringList * sl,const char * name)10861d06d6bSBaptiste Daroussin sl_find(StringList *sl, const char *name)
10961d06d6bSBaptiste Daroussin {
11061d06d6bSBaptiste Daroussin 	size_t i;
11161d06d6bSBaptiste Daroussin 
11261d06d6bSBaptiste Daroussin 	for (i = 0; i < sl->sl_cur; i++)
11361d06d6bSBaptiste Daroussin 		if (strcmp(sl->sl_str[i], name) == 0)
11461d06d6bSBaptiste Daroussin 			return sl->sl_str[i];
11561d06d6bSBaptiste Daroussin 
11661d06d6bSBaptiste Daroussin 	return NULL;
11761d06d6bSBaptiste Daroussin }
11861d06d6bSBaptiste Daroussin 
119*6d38604fSBaptiste Daroussin int
sl_delete(StringList * sl,const char * name,int all)120*6d38604fSBaptiste Daroussin sl_delete(StringList *sl, const char *name, int all)
121*6d38604fSBaptiste Daroussin {
122*6d38604fSBaptiste Daroussin 	size_t i, j;
123*6d38604fSBaptiste Daroussin 
124*6d38604fSBaptiste Daroussin 	for (i = 0; i < sl->sl_cur; i++)
125*6d38604fSBaptiste Daroussin 		if (strcmp(sl->sl_str[i], name) == 0) {
126*6d38604fSBaptiste Daroussin 			if (all)
127*6d38604fSBaptiste Daroussin 				free(sl->sl_str[i]);
128*6d38604fSBaptiste Daroussin 			for (j = i + 1; j < sl->sl_cur; j++)
129*6d38604fSBaptiste Daroussin 				sl->sl_str[j - 1] = sl->sl_str[j];
130*6d38604fSBaptiste Daroussin 			sl->sl_str[--sl->sl_cur] = NULL;
131*6d38604fSBaptiste Daroussin 			return 0;
132*6d38604fSBaptiste Daroussin 		}
133*6d38604fSBaptiste Daroussin 	return -1;
134*6d38604fSBaptiste Daroussin }
135*6d38604fSBaptiste Daroussin 
136