xref: /freebsd/lib/libc/gen/stringlist.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1994 Christos Zoulas
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: stringlist.c,v 1.2 1997/01/17 07:26:20 lukem Exp $");
31 #include "namespace.h"
32 #include <stdio.h>
33 #include <string.h>
34 #include <err.h>
35 #include <stdlib.h>
36 #include <stringlist.h>
37 #include "un-namespace.h"
38 
39 #define _SL_CHUNKSIZE	20
40 
41 /*
42  * sl_init(): Initialize a string list
43  */
44 StringList *
45 sl_init(void)
46 {
47 	StringList *sl;
48 
49 	sl = malloc(sizeof(StringList));
50 	if (sl == NULL)
51 		_err(1, "stringlist: %m");
52 
53 	sl->sl_cur = 0;
54 	sl->sl_max = _SL_CHUNKSIZE;
55 	sl->sl_str = malloc(sl->sl_max * sizeof(char *));
56 	if (sl->sl_str == NULL)
57 		_err(1, "stringlist: %m");
58 	return sl;
59 }
60 
61 
62 /*
63  * sl_add(): Add an item to the string list
64  */
65 int
66 sl_add(StringList *sl, char *name)
67 {
68 	if (sl->sl_cur == sl->sl_max - 1) {
69 		sl->sl_max += _SL_CHUNKSIZE;
70 		sl->sl_str = reallocf(sl->sl_str, sl->sl_max * sizeof(char *));
71 		if (sl->sl_str == NULL)
72 			return (-1);
73 	}
74 	sl->sl_str[sl->sl_cur++] = name;
75 	return (0);
76 }
77 
78 
79 /*
80  * sl_free(): Free a stringlist
81  */
82 void
83 sl_free(StringList *sl, int all)
84 {
85 	size_t i;
86 
87 	if (sl == NULL)
88 		return;
89 	if (sl->sl_str) {
90 		if (all)
91 			for (i = 0; i < sl->sl_cur; i++)
92 				free(sl->sl_str[i]);
93 		free(sl->sl_str);
94 	}
95 	free(sl);
96 }
97 
98 
99 /*
100  * sl_find(): Find a name in the string list
101  */
102 char *
103 sl_find(StringList *sl, const char *name)
104 {
105 	size_t i;
106 
107 	for (i = 0; i < sl->sl_cur; i++)
108 		if (strcmp(sl->sl_str[i], name) == 0)
109 			return sl->sl_str[i];
110 
111 	return NULL;
112 }
113