xref: /illumos-gate/usr/src/cmd/mandoc/compat_stringlist.c (revision 417127eb7a4962848176eeecdc1f2fb597c451f2)
1 /*	$Id: compat_stringlist.c,v 1.8 2020/06/15 21:48:09 schwarze Exp $ */
2 /*	$NetBSD: stringlist.c,v 1.14 2015/05/21 01:29:13 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Christos Zoulas.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #include "config.h"
33 
34 #include <stdlib.h>
35 #include <string.h>
36 #include "compat_stringlist.h"
37 
38 #define _SL_CHUNKSIZE	20
39 
40 /*
41  * sl_init(): Initialize a string list
42  */
43 StringList *
44 sl_init(void)
45 {
46 	StringList *sl;
47 
48 	sl = malloc(sizeof(StringList));
49 	if (sl == NULL)
50 		return NULL;
51 
52 	sl->sl_cur = 0;
53 	sl->sl_max = _SL_CHUNKSIZE;
54 	sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
55 	if (sl->sl_str == NULL) {
56 		free(sl);
57 		sl = NULL;
58 	}
59 	return sl;
60 }
61 
62 
63 /*
64  * sl_add(): Add an item to the string list
65  */
66 int
67 sl_add(StringList *sl, char *name)
68 {
69 	if (sl->sl_cur == sl->sl_max - 1) {
70 		char	**new;
71 
72 		new = reallocarray(sl->sl_str, (sl->sl_max + _SL_CHUNKSIZE),
73 		    sizeof(char *));
74 		if (new == NULL)
75 			return -1;
76 		sl->sl_max += _SL_CHUNKSIZE;
77 		sl->sl_str = new;
78 	}
79 	sl->sl_str[sl->sl_cur++] = name;
80 	return 0;
81 }
82 
83 
84 /*
85  * sl_free(): Free a stringlist
86  */
87 void
88 sl_free(StringList *sl, int all)
89 {
90 	size_t i;
91 
92 	if (sl == NULL)
93 		return;
94 	if (sl->sl_str) {
95 		if (all)
96 			for (i = 0; i < sl->sl_cur; i++)
97 				free(sl->sl_str[i]);
98 		free(sl->sl_str);
99 	}
100 	free(sl);
101 }
102 
103 
104 /*
105  * sl_find(): Find a name in the string list
106  */
107 char *
108 sl_find(StringList *sl, const char *name)
109 {
110 	size_t i;
111 
112 	for (i = 0; i < sl->sl_cur; i++)
113 		if (strcmp(sl->sl_str[i], name) == 0)
114 			return sl->sl_str[i];
115 
116 	return NULL;
117 }
118 
119 int
120 sl_delete(StringList *sl, const char *name, int all)
121 {
122 	size_t i, j;
123 
124 	for (i = 0; i < sl->sl_cur; i++)
125 		if (strcmp(sl->sl_str[i], name) == 0) {
126 			if (all)
127 				free(sl->sl_str[i]);
128 			for (j = i + 1; j < sl->sl_cur; j++)
129 				sl->sl_str[j - 1] = sl->sl_str[j];
130 			sl->sl_str[--sl->sl_cur] = NULL;
131 			return 0;
132 		}
133 	return -1;
134 }
135 
136