Make unvis() have more reasonable argument types.
[unix-history] / usr / src / lib / libc / gen / initgroups.c
CommitLineData
bb0cfa24
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
eabc610b
KB
3 * All rights reserved.
4 *
269a7923 5 * %sccs.include.redist.c%
bb0cfa24
DF
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
269a7923 9static char sccsid[] = "@(#)initgroups.c 5.6 (Berkeley) %G%";
eabc610b 10#endif /* LIBC_SCCS and not lint */
a4f42d0e
SL
11
12/*
13 * initgroups
14 */
15#include <stdio.h>
16#include <sys/param.h>
17#include <grp.h>
18
19struct group *getgrent();
20
21initgroups(uname, agroup)
22 char *uname;
23 int agroup;
24{
25 int groups[NGROUPS], ngroups = 0;
26 register struct group *grp;
27 register int i;
28
60d94477
MK
29 /*
30 * If installing primary group, duplicate it;
31 * the first element of groups is the effective gid
32 * and will be overwritten when a setgid file is executed.
33 */
34 if (agroup >= 0) {
a4f42d0e 35 groups[ngroups++] = agroup;
60d94477
MK
36 groups[ngroups++] = agroup;
37 }
a4f42d0e 38 setgrent();
0fb3f429
SL
39 while (grp = getgrent()) {
40 if (grp->gr_gid == agroup)
41 continue;
a4f42d0e
SL
42 for (i = 0; grp->gr_mem[i]; i++)
43 if (!strcmp(grp->gr_mem[i], uname)) {
a4f42d0e 44 if (ngroups == NGROUPS) {
0fb3f429 45fprintf(stderr, "initgroups: %s is in too many groups\n", uname);
a4f42d0e
SL
46 goto toomany;
47 }
7479b5d0 48 groups[ngroups++] = grp->gr_gid;
a4f42d0e 49 }
0fb3f429 50 }
a4f42d0e 51toomany:
4ce247a6 52 endgrent();
a4f42d0e 53 if (setgroups(ngroups, groups) < 0) {
0fb3f429 54 perror("setgroups");
b3b3b313 55 return (-1);
a4f42d0e
SL
56 }
57 return (0);
58}