xref: /csrg-svn/lib/libc/gen/initgroups.c (revision 35091)
121346Sdist /*
221346Sdist  * Copyright (c) 1983 Regents of the University of California.
3*35091Sbostic  * All rights reserved.
4*35091Sbostic  *
5*35091Sbostic  * Redistribution and use in source and binary forms are permitted
6*35091Sbostic  * provided that the above copyright notice and this paragraph are
7*35091Sbostic  * duplicated in all such forms and that any documentation,
8*35091Sbostic  * advertising materials, and other materials related to such
9*35091Sbostic  * distribution and use acknowledge that the software was developed
10*35091Sbostic  * by the University of California, Berkeley.  The name of the
11*35091Sbostic  * University may not be used to endorse or promote products derived
12*35091Sbostic  * from this software without specific prior written permission.
13*35091Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*35091Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*35091Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621346Sdist  */
179110Ssam 
1826567Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*35091Sbostic static char sccsid[] = "@(#)initgroups.c	5.4 (Berkeley) 07/18/88";
20*35091Sbostic #endif /* LIBC_SCCS and not lint */
2121346Sdist 
229110Ssam /*
239110Ssam  * initgroups
249110Ssam  */
259110Ssam #include <stdio.h>
269110Ssam #include <sys/param.h>
279110Ssam #include <grp.h>
289110Ssam 
299110Ssam struct group *getgrent();
309110Ssam 
319110Ssam initgroups(uname, agroup)
329110Ssam 	char *uname;
339110Ssam 	int agroup;
349110Ssam {
359110Ssam 	int groups[NGROUPS], ngroups = 0;
369110Ssam 	register struct group *grp;
379110Ssam 	register int i;
389110Ssam 
399110Ssam 	if (agroup >= 0)
409110Ssam 		groups[ngroups++] = agroup;
419110Ssam 	setgrent();
4212168Ssam 	while (grp = getgrent()) {
4312168Ssam 		if (grp->gr_gid == agroup)
4412168Ssam 			continue;
459110Ssam 		for (i = 0; grp->gr_mem[i]; i++)
469110Ssam 			if (!strcmp(grp->gr_mem[i], uname)) {
479110Ssam 				if (ngroups == NGROUPS) {
4812168Ssam fprintf(stderr, "initgroups: %s is in too many groups\n", uname);
499110Ssam 					goto toomany;
509110Ssam 				}
5111073Smckusick 				groups[ngroups++] = grp->gr_gid;
529110Ssam 			}
5312168Ssam 	}
549110Ssam toomany:
5513176Ssam 	endgrent();
569110Ssam 	if (setgroups(ngroups, groups) < 0) {
5712168Ssam 		perror("setgroups");
5827457Sdonn 		return (-1);
599110Ssam 	}
609110Ssam 	return (0);
619110Ssam }
62