2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2008-04-27 06:10:09 +05:30
|
|
|
* Copyright (c) 1991 - 1994, Julianne Frances Haugh
|
|
|
|
* Copyright (c) 1996 - 1997, Marek Michałkiewicz
|
|
|
|
* Copyright (c) 2003 - 2005, Tomasz Kłoczko
|
2007-10-07 17:14:02 +05:30
|
|
|
* 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.
|
2008-04-27 06:10:09 +05:30
|
|
|
* 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.
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2008-04-27 06:10:09 +05:30
|
|
|
* 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.
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/* Removed duplicated code from gpmain.c, useradd.c, userdel.c and
|
|
|
|
usermod.c. --marekm */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#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.
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
char **add_list (char **list, const char *member)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
int i;
|
|
|
|
char **tmp;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan the list for the new name. Return the original list
|
|
|
|
* pointer if it is present.
|
|
|
|
*/
|
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
for (i = 0; list[i] != (char *) 0; i++) {
|
|
|
|
if (strcmp (list[i], member) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return list;
|
2008-05-24 19:39:35 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
for (i = 0; list[i] != (char *) 0; i++) {
|
2007-10-07 17:14:02 +05:30
|
|
|
tmp[i] = list[i];
|
2008-05-24 19:39:35 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
tmp[i] = xstrdup (member);
|
|
|
|
tmp[i+1] = (char *) 0;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
char **del_list (char **list, const char *member)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
int i, j;
|
|
|
|
char **tmp;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan the list for the old name. Return the original list
|
|
|
|
* pointer if it is not present.
|
|
|
|
*/
|
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
for (i = j = 0; list[i] != (char *) 0; i++) {
|
|
|
|
if (strcmp (list[i], member) != 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
j++;
|
2008-05-24 19:39:35 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
if (j == i) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return list;
|
2008-05-24 19:39:35 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
for (i = j = 0; list[i] != (char *) 0; i++) {
|
|
|
|
if (strcmp (list[i], member) != 0) {
|
|
|
|
tmp[j] = list[i];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
tmp[j] = (char *) 0;
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
char **dup_list (char *const *list)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char **tmp;
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
for (i = 0; list[i]; i++);
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
tmp = (char **) xmalloc ((i + 1) * sizeof (char *));
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
i = 0;
|
2008-05-24 19:39:35 +05:30
|
|
|
while (*list) {
|
|
|
|
tmp[i] = xstrdup (*list);
|
|
|
|
i++;
|
|
|
|
list++;
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
tmp[i] = (char *) 0;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2008-06-09 23:43:52 +05:30
|
|
|
bool is_on_list (char *const *list, const char *member)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
while (*list) {
|
2008-05-24 19:39:35 +05:30
|
|
|
if (strcmp (*list, member) == 0) {
|
2008-06-09 23:43:52 +05:30
|
|
|
return true;
|
2008-05-24 19:39:35 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
list++;
|
|
|
|
}
|
2008-06-09 23:43:52 +05:30
|
|
|
return false;
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* comma_to_list - convert comma-separated list to (char *) array
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
char **comma_to_list (const char *comma)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
char *members;
|
|
|
|
char **array;
|
|
|
|
int i;
|
|
|
|
char *cp, *cp2;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a copy since we are going to be modifying the list
|
|
|
|
*/
|
|
|
|
|
|
|
|
members = xstrdup (comma);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the number of commas in the list
|
|
|
|
*/
|
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
for (cp = members, i = 0;; i++) {
|
|
|
|
cp2 = strchr (cp, ',');
|
|
|
|
if (NULL != cp2) {
|
2007-10-07 17:14:02 +05:30
|
|
|
cp = cp2 + 1;
|
2008-05-24 19:39:35 +05:30
|
|
|
} else {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-05-24 19:39:35 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2008-05-24 19:39:35 +05:30
|
|
|
if ('\0' == *members) {
|
2007-10-07 17:14:02 +05:30
|
|
|
*array = (char *) 0;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now go walk that list all over again, this time building the
|
|
|
|
* array of pointers.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
for (cp = members, i = 0;; i++) {
|
2007-10-07 17:14:02 +05:30
|
|
|
array[i] = cp;
|
2008-05-24 19:39:35 +05:30
|
|
|
cp2 = strchr (cp, ',');
|
|
|
|
if (NULL != cp2) {
|
|
|
|
*cp2 = '\0';
|
|
|
|
cp2++;
|
2007-10-07 17:14:02 +05:30
|
|
|
cp = cp2;
|
|
|
|
} else {
|
|
|
|
array[i + 1] = (char *) 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the new array of pointers
|
|
|
|
*/
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
2008-05-24 19:39:35 +05:30
|
|
|
|