xref: /csrg-svn/old/groups/groups.c (revision 21560)
1*21560Sdist /*
2*21560Sdist  * Copyright (c) 1980 Regents of the University of California.
3*21560Sdist  * All rights reserved.  The Berkeley software License Agreement
4*21560Sdist  * specifies the terms and conditions for redistribution.
5*21560Sdist  */
6*21560Sdist 
714517Ssam #ifndef lint
8*21560Sdist char copyright[] =
9*21560Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*21560Sdist  All rights reserved.\n";
11*21560Sdist #endif not lint
126013Swnj 
13*21560Sdist #ifndef lint
14*21560Sdist static char sccsid[] = "@(#)groups.c	5.1 (Berkeley) 05/31/85";
15*21560Sdist #endif not lint
16*21560Sdist 
176013Swnj /*
186013Swnj  * groups
196013Swnj  */
206013Swnj 
216013Swnj #include <sys/param.h>
226013Swnj #include <grp.h>
236013Swnj #include <pwd.h>
2415082Ssam #include <stdio.h>
256013Swnj 
2611072Smckusick int	groups[NGROUPS];
276013Swnj 
286013Swnj main(argc, argv)
296013Swnj 	int argc;
306013Swnj 	char *argv[];
316013Swnj {
3213440Ssam 	int ngroups, i;
336013Swnj 	char *sep = "";
3413440Ssam 	struct group *gr;
356013Swnj 
3613440Ssam 	if (argc > 1)
3713440Ssam 		showgroups(argv[1]);
389249Ssam 	ngroups = getgroups(NGROUPS, groups);
399249Ssam 	for (i = 0; i < ngroups; i++) {
409249Ssam 		gr = getgrgid(groups[i]);
419249Ssam 		if (gr == NULL)
429249Ssam 			printf("%s%d", sep, groups[i]);
439249Ssam 		else
449249Ssam 			printf("%s%s", sep, gr->gr_name);
459249Ssam 		sep = " ";
469249Ssam 	}
476013Swnj 	printf("\n");
486013Swnj 	exit(0);
496013Swnj }
5013440Ssam 
5113440Ssam showgroups(user)
5213440Ssam 	register char *user;
5313440Ssam {
5413440Ssam 	register struct group *gr;
5515082Ssam 	register struct passwd *pw;
5613440Ssam 	register char **cp;
5713440Ssam 	char *sep = "";
5813440Ssam 
5915082Ssam 	if ((pw = getpwnam(user)) == NULL) {
6015082Ssam 		fprintf(stderr, "No such user\n");
6115082Ssam 		exit(1);
6215082Ssam 	}
6315082Ssam 	while (gr = getgrent()) {
6415082Ssam 		if (pw->pw_gid == gr->gr_gid) {
6515082Ssam 			printf("%s%s", sep, gr->gr_name);
6615082Ssam 			sep = " ";
6715082Ssam 			continue;
6815082Ssam 		}
6913440Ssam 		for (cp = gr->gr_mem; cp && *cp; cp++)
7013440Ssam 			if (strcmp(*cp, user) == 0) {
7113440Ssam 				printf("%s%s", sep, gr->gr_name);
7213440Ssam 				sep = " ";
7313440Ssam 				break;
7413440Ssam 			}
7515082Ssam 	}
7613440Ssam 	printf("\n");
7713440Ssam 	exit(0);
7813440Ssam }
79