2 * Copyright (c) 1991 - 1994, Julianne Frances Haugh
3 * Copyright (c) 1996 - 1997, Marek Michałkiewicz
4 * Copyright (c) 2003 - 2005, Tomasz Kłoczko
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. The name of the copyright holders or contributors may not be used to
16 * endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include "prototypes.h"
40 * add_list - add a member to a list of group members
42 * the array of member names is searched for the new member
43 * name, and if not present it is added to a freshly allocated
46 /*@only@*/ /*@out@*/char **add_list (/*@returned@*/ /*@only@*/char **list, const char *member)
51 assert (NULL != member);
52 assert (NULL != list);
55 * Scan the list for the new name. Return the original list
56 * pointer if it is present.
59 for (i = 0; list[i] != (char *) 0; i++) {
60 if (strcmp (list[i], member) == 0) {
66 * Allocate a new list pointer large enough to hold all the
67 * old entries, and the new entries as well.
70 tmp = (char **) xmalloc ((i + 2) * sizeof member);
73 * Copy the original list to the new list, then append the
74 * new member and NULL terminate the result. This new list
75 * is returned to the invoker.
78 for (i = 0; list[i] != (char *) 0; i++) {
82 tmp[i] = xstrdup (member);
83 tmp[i+1] = (char *) 0;
89 * del_list - delete a member from a list of group members
91 * the array of member names is searched for the old member
92 * name, and if present it is deleted from a freshly allocated
96 /*@only@*/ /*@out@*/char **del_list (/*@returned@*/ /*@only@*/char **list, const char *member)
101 assert (NULL != member);
102 assert (NULL != list);
105 * Scan the list for the old name. Return the original list
106 * pointer if it is not present.
109 for (i = j = 0; list[i] != (char *) 0; i++) {
110 if (strcmp (list[i], member) != 0) {
120 * Allocate a new list pointer large enough to hold all the
124 tmp = (char **) xmalloc ((j + 1) * sizeof member);
127 * Copy the original list except the deleted members to the
128 * new list, then NULL terminate the result. This new list
129 * is returned to the invoker.
132 for (i = j = 0; list[i] != (char *) 0; i++) {
133 if (strcmp (list[i], member) != 0) {
146 * The input list is not modified, but in order to allow the use of this
147 * function with list of members, the list elements are not enforced to be
148 * constant strings here.
150 /*@only@*/ /*@out@*/char **dup_list (char *const *list)
155 assert (NULL != list);
157 for (i = 0; NULL != list[i]; i++);
159 tmp = (char **) xmalloc ((i + 1) * sizeof (char *));
162 while (NULL != *list) {
163 tmp[i] = xstrdup (*list);
173 * Check if member is part of the input list
174 * The input list is not modified, but in order to allow the use of this
175 * function with list of members, the list elements are not enforced to be
176 * constant strings here.
178 bool is_on_list (char *const *list, const char *member)
180 assert (NULL != member);
181 assert (NULL != list);
183 while (NULL != *list) {
184 if (strcmp (*list, member) == 0) {
194 * comma_to_list - convert comma-separated list to (char *) array
197 /*@only@*/char **comma_to_list (const char *comma)
205 assert (NULL != comma);
208 * Make a copy since we are going to be modifying the list
211 members = xstrdup (comma);
214 * Count the number of commas in the list
217 for (cp = members, i = 0;; i++) {
218 cp2 = strchr (cp, ',');
227 * Add 2 - one for the ending NULL, the other for the last item
233 * Allocate the array we're going to store the pointers into.
236 array = (char **) xmalloc (sizeof (char *) * i);
239 * Empty list is special - 0 members, not 1 empty member. --marekm
242 if ('\0' == *members) {
248 * Now go walk that list all over again, this time building the
252 for (cp = members, i = 0;; i++) {
254 cp2 = strchr (cp, ',');
260 array[i + 1] = (char *) 0;
266 * Return the new array of pointers