summaryrefslogtreecommitdiffstats
path: root/libmisc/list.c
blob: 2da734a74d3cbb74c640df252e49808cd963049f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 1991 - 1994, Julianne Frances Haugh
 * Copyright (c) 1996 - 1997, Marek Michałkiewicz
 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the copyright holders or contributors may not be used to
 *    endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <config.h>

#ident "$Id$"

#include <assert.h>
#include "prototypes.h"
#include "defines.h"
/*
 * add_list - add a member to a list of group members
 *
 *	the array of member names is searched for the new member
 *	name, and if not present it is added to a freshly allocated
 *	list of users.
 */
/*@only@*/ /*@out@*/char **add_list (/*@returned@*/ /*@only@*/char **list, const char *member)
{
	int i;
	char **tmp;

	assert (NULL != member);
	assert (NULL != list);

	/*
	 * Scan the list for the new name.  Return the original list
	 * pointer if it is present.
	 */

	for (i = 0; list[i] != (char *) 0; i++) {
		if (strcmp (list[i], member) == 0) {
			return list;
		}
	}

	/*
	 * Allocate a new list pointer large enough to hold all the
	 * old entries, and the new entries as well.
	 */

	tmp = (char **) xmalloc ((i + 2) * sizeof member);

	/*
	 * Copy the original list to the new list, then append the
	 * new member and NULL terminate the result.  This new list
	 * is returned to the invoker.
	 */

	for (i = 0; list[i] != (char *) 0; i++) {
		tmp[i] = list[i];
	}

	tmp[i] = xstrdup (member);
	tmp[i+1] = (char *) 0;

	return tmp;
}

/*
 * del_list - delete a member from a list of group members
 *
 *	the array of member names is searched for the old member
 *	name, and if present it is deleted from a freshly allocated
 *	list of users.
 */

/*@only@*/ /*@out@*/char **del_list (/*@returned@*/ /*@only@*/char **list, const char *member)
{
	int i, j;
	char **tmp;

	assert (NULL != member);
	assert (NULL != list);

	/*
	 * Scan the list for the old name.  Return the original list
	 * pointer if it is not present.
	 */

	for (i = j = 0; list[i] != (char *) 0; i++) {
		if (strcmp (list[i], member) != 0) {
			j++;
		}
	}

	if (j == i) {
		return list;
	}

	/*
	 * Allocate a new list pointer large enough to hold all the
	 * old entries.
	 */

	tmp = (char **) xmalloc ((j + 1) * sizeof member);

	/*
	 * Copy the original list except the deleted members to the
	 * new list, then NULL terminate the result.  This new list
	 * is returned to the invoker.
	 */

	for (i = j = 0; list[i] != (char *) 0; i++) {
		if (strcmp (list[i], member) != 0) {
			tmp[j] = list[i];
			j++;
		}
	}

	tmp[j] = (char *) 0;

	return tmp;
}

/*
 * Duplicate a list.
 * The input list is not modified, but in order to allow the use of this
 * function with list of members, the list elements are not enforced to be
 * constant strings here.
 */
/*@only@*/ /*@out@*/char **dup_list (char *const *list)
{
	int i;
	char **tmp;

	assert (NULL != list);

	for (i = 0; NULL != list[i]; i++);

	tmp = (char **) xmalloc ((i + 1) * sizeof (char *));

	i = 0;
	while (NULL != *list) {
		tmp[i] = xstrdup (*list);
		i++;
		list++;
	}

	tmp[i] = (char *) 0;
	return tmp;
}

/*
 * Check if member is part of the input list
 * The input list is not modified, but in order to allow the use of this
 * function with list of members, the list elements are not enforced to be
 * constant strings here.
 */
bool is_on_list (char *const *list, const char *member)
{
	assert (NULL != member);
	assert (NULL != list);

	while (NULL != *list) {
		if (strcmp (*list, member) == 0) {
			return true;
		}
		list++;
	}

	return false;
}

/*
 * comma_to_list - convert comma-separated list to (char *) array
 */

/*@only@*/char **comma_to_list (const char *comma)
{
	char *members;
	char **array;
	int i;
	char *cp;
	char *cp2;

	assert (NULL != comma);

	/*
	 * Make a copy since we are going to be modifying the list
	 */

	members = xstrdup (comma);

	/*
	 * Count the number of commas in the list
	 */

	for (cp = members, i = 0;; i++) {
		cp2 = strchr (cp, ',');
		if (NULL != cp2) {
			cp = cp2 + 1;
		} else {
			break;
		}
	}

	/*
	 * Add 2 - one for the ending NULL, the other for the last item
	 */

	i += 2;

	/*
	 * Allocate the array we're going to store the pointers into.
	 */

	array = (char **) xmalloc (sizeof (char *) * i);

	/*
	 * Empty list is special - 0 members, not 1 empty member.  --marekm
	 */

	if ('\0' == *members) {
		*array = (char *) 0;
		return array;
	}

	/*
	 * Now go walk that list all over again, this time building the
	 * array of pointers.
	 */

	for (cp = members, i = 0;; i++) {
		array[i] = cp;
		cp2 = strchr (cp, ',');
		if (NULL != cp2) {
			*cp2 = '\0';
			cp2++;
			cp = cp2;
		} else {
			array[i + 1] = (char *) 0;
			break;
		}
	}

	/*
	 * Return the new array of pointers
	 */

	return array;
}