Update.
[glibc.git] / nis / nis_removemember.c
blob71807a3ffe2332b634a31a03acadbdaf7468f8c2
1 /* Copyright (c) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <string.h>
21 #include <rpcsvc/nis.h>
23 nis_error
24 nis_removemember (const_nis_name member, const_nis_name group)
26 if (group != NULL && strlen (group) > 0)
28 char buf[strlen (group) + 14 + NIS_MAXNAMELEN];
29 char leafbuf[strlen (group) + 2];
30 char domainbuf[strlen (group) + 2];
31 nis_name *newmem;
32 nis_result *res, *res2;
33 nis_error status;
34 char *cp, *cp2;
35 unsigned long int i, j, k;
37 cp = stpcpy (buf, nis_leaf_of_r (group, leafbuf, sizeof (leafbuf) - 1));
38 cp = stpcpy (cp, ".groups_dir");
39 cp2 = nis_domain_of_r (group, domainbuf, sizeof (domainbuf) - 1);
40 if (cp2 != NULL && strlen (cp2) > 0)
42 cp = stpcpy (cp, ".");
43 stpcpy (cp, cp2);
45 res = nis_lookup (buf, FOLLOW_LINKS|EXPAND_NAME);
46 if (res->status != NIS_SUCCESS)
48 status = res->status;
49 nis_freeresult (res);
50 return status;
53 if ((res->objects.objects_len != 1) ||
54 (__type_of (NIS_RES_OBJECT (res)) != NIS_GROUP_OBJ))
55 return NIS_INVALIDOBJ;
57 newmem =
58 calloc (1, NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len *
59 sizeof (char *));
60 k = NIS_RES_OBJECT (res)[0].GR_data.gr_members.gr_members_len;
61 j = 0;
62 for (i = 0; i < NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_len;
63 ++i)
65 if (strcmp (NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_val[i],
66 member) != 0)
68 newmem[j] = NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_val[i];
69 ++j;
71 else
73 free (NIS_RES_OBJECT(res)->GR_data.gr_members.gr_members_val[i]);
74 --k;
77 free (NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val);
78 newmem = realloc (newmem, k * sizeof (char*));
79 NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_val = newmem;
80 NIS_RES_OBJECT (res)->GR_data.gr_members.gr_members_len = k;
82 cp = stpcpy (buf, NIS_RES_OBJECT (res)->zo_name);
83 *cp++ = '.';
84 strncpy (cp, NIS_RES_OBJECT (res)->zo_domain, NIS_MAXNAMELEN);
85 res2 = nis_modify (buf, NIS_RES_OBJECT (res));
86 status = res2->status;
87 nis_freeresult (res);
88 nis_freeresult (res2);
90 return status;
92 else
93 return NIS_FAIL;