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) {
144 /*@only@*/ /*@out@*/char **dup_list (char *const *list)
149 assert (NULL != list);
151 for (i = 0; NULL != list[i]; i++);
153 tmp = (char **) xmalloc ((i + 1) * sizeof (char *));
156 while (NULL != *list) {
157 tmp[i] = xstrdup (*list);
166 bool is_on_list (char *const *list, const char *member)
168 assert (NULL != member);
169 assert (NULL != list);
171 while (NULL != *list) {
172 if (strcmp (*list, member) == 0) {
182 * comma_to_list - convert comma-separated list to (char *) array
185 /*@only@*/char **comma_to_list (const char *comma)
193 assert (NULL != comma);
196 * Make a copy since we are going to be modifying the list
199 members = xstrdup (comma);
202 * Count the number of commas in the list
205 for (cp = members, i = 0;; i++) {
206 cp2 = strchr (cp, ',');
215 * Add 2 - one for the ending NULL, the other for the last item
221 * Allocate the array we're going to store the pointers into.
224 array = (char **) xmalloc (sizeof (char *) * i);
227 * Empty list is special - 0 members, not 1 empty member. --marekm
230 if ('\0' == *members) {
236 * Now go walk that list all over again, this time building the
240 for (cp = members, i = 0;; i++) {
242 cp2 = strchr (cp, ',');
248 array[i + 1] = (char *) 0;
254 * Return the new array of pointers