/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header: initgroups.c 1.3 89/08/07 $"

/* $Log:	initgroups.c,v $
 */

#include <sys/param.h>
#include <stdio.h>
#include <grp.h>

#define TOO_MANY_GROUPS "\n\t\t*** You are in too many groups. ***\n\n"

initgroups(u_name)
char *u_name;
{
	register struct group *grp;
	register int i;
	gid_t groups[NGROUPS];
	int ngroups = 0;

	setgrent();
	while (grp = getgrent()) {
		for (i = 0; grp->gr_mem[i]; i++) {
			if (strcmp(grp->gr_mem[i], u_name) == 0) {
				if (ngroups == NGROUPS) {
					fprintf(stderr, TOO_MANY_GROUPS);
					goto out;
				}
				groups[ngroups++] = grp->gr_gid;
			}
		}
	}
out:
	endgrent();
	if (setgroups(ngroups, groups) == -1) {
		perror("setgroups");
		return(1);
	}
	return(0);
}
