xref: /csrg-svn/lib/libc/gen/initgroups.c (revision 21346)
1*21346Sdist /*
2*21346Sdist  * Copyright (c) 1983 Regents of the University of California.
3*21346Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21346Sdist  * specifies the terms and conditions for redistribution.
5*21346Sdist  */
69110Ssam 
7*21346Sdist #ifndef lint
8*21346Sdist static char sccsid[] = "@(#)initgroups.c	5.1 (Berkeley) 05/30/85";
9*21346Sdist #endif not lint
10*21346Sdist 
119110Ssam /*
129110Ssam  * initgroups
139110Ssam  */
149110Ssam #include <stdio.h>
159110Ssam #include <sys/param.h>
169110Ssam #include <grp.h>
179110Ssam 
189110Ssam struct group *getgrent();
199110Ssam 
209110Ssam initgroups(uname, agroup)
219110Ssam 	char *uname;
229110Ssam 	int agroup;
239110Ssam {
249110Ssam 	int groups[NGROUPS], ngroups = 0;
259110Ssam 	register struct group *grp;
269110Ssam 	register int i;
279110Ssam 
289110Ssam 	if (agroup >= 0)
299110Ssam 		groups[ngroups++] = agroup;
309110Ssam 	setgrent();
3112168Ssam 	while (grp = getgrent()) {
3212168Ssam 		if (grp->gr_gid == agroup)
3312168Ssam 			continue;
349110Ssam 		for (i = 0; grp->gr_mem[i]; i++)
359110Ssam 			if (!strcmp(grp->gr_mem[i], uname)) {
369110Ssam 				if (ngroups == NGROUPS) {
3712168Ssam fprintf(stderr, "initgroups: %s is in too many groups\n", uname);
389110Ssam 					goto toomany;
399110Ssam 				}
4011073Smckusick 				groups[ngroups++] = grp->gr_gid;
419110Ssam 			}
4212168Ssam 	}
439110Ssam toomany:
4413176Ssam 	endgrent();
459110Ssam 	if (setgroups(ngroups, groups) < 0) {
4612168Ssam 		perror("setgroups");
479110Ssam 		return (1);
489110Ssam 	}
499110Ssam 	return (0);
509110Ssam }
51