machine/param.h is gone
[unix-history] / usr / src / lib / libc / gen / initgroups.c
CommitLineData
bb0cfa24
DF
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8static char sccsid[] = "@(#)initgroups.c 5.1 (Berkeley) %G%";
9#endif not lint
a4f42d0e
SL
10
11/*
12 * initgroups
13 */
14#include <stdio.h>
15#include <sys/param.h>
16#include <grp.h>
17
18struct group *getgrent();
19
20initgroups(uname, agroup)
21 char *uname;
22 int agroup;
23{
24 int groups[NGROUPS], ngroups = 0;
25 register struct group *grp;
26 register int i;
27
28 if (agroup >= 0)
29 groups[ngroups++] = agroup;
30 setgrent();
0fb3f429
SL
31 while (grp = getgrent()) {
32 if (grp->gr_gid == agroup)
33 continue;
a4f42d0e
SL
34 for (i = 0; grp->gr_mem[i]; i++)
35 if (!strcmp(grp->gr_mem[i], uname)) {
a4f42d0e 36 if (ngroups == NGROUPS) {
0fb3f429 37fprintf(stderr, "initgroups: %s is in too many groups\n", uname);
a4f42d0e
SL
38 goto toomany;
39 }
7479b5d0 40 groups[ngroups++] = grp->gr_gid;
a4f42d0e 41 }
0fb3f429 42 }
a4f42d0e 43toomany:
4ce247a6 44 endgrent();
a4f42d0e 45 if (setgroups(ngroups, groups) < 0) {
0fb3f429 46 perror("setgroups");
a4f42d0e
SL
47 return (1);
48 }
49 return (0);
50}