xref: /csrg-svn/old/groups/groups.c (revision 14517)
1*14517Ssam #ifndef lint
2*14517Ssam static char sccsid[] = "@(#)groups.c	4.7 (Berkeley) 08/11/83";
3*14517Ssam #endif
46013Swnj 
56013Swnj /*
66013Swnj  * groups
76013Swnj  */
86013Swnj 
96013Swnj #include <sys/param.h>
106013Swnj #include <grp.h>
116013Swnj #include <pwd.h>
126013Swnj 
1311072Smckusick int	groups[NGROUPS];
146013Swnj 
156013Swnj main(argc, argv)
166013Swnj 	int argc;
176013Swnj 	char *argv[];
186013Swnj {
1913440Ssam 	int ngroups, i;
206013Swnj 	char *sep = "";
2113440Ssam 	struct group *gr;
226013Swnj 
2313440Ssam 	if (argc > 1)
2413440Ssam 		showgroups(argv[1]);
259249Ssam 	ngroups = getgroups(NGROUPS, groups);
269249Ssam 	for (i = 0; i < ngroups; i++) {
279249Ssam 		gr = getgrgid(groups[i]);
289249Ssam 		if (gr == NULL)
299249Ssam 			printf("%s%d", sep, groups[i]);
309249Ssam 		else
319249Ssam 			printf("%s%s", sep, gr->gr_name);
329249Ssam 		sep = " ";
339249Ssam 	}
346013Swnj 	printf("\n");
356013Swnj 	exit(0);
366013Swnj }
3713440Ssam 
3813440Ssam showgroups(user)
3913440Ssam 	register char *user;
4013440Ssam {
4113440Ssam 	register struct group *gr;
4213440Ssam 	register char **cp;
4313440Ssam 	char *sep = "";
4413440Ssam 
4513440Ssam 	while (gr = getgrent())
4613440Ssam 		for (cp = gr->gr_mem; cp && *cp; cp++)
4713440Ssam 			if (strcmp(*cp, user) == 0) {
4813440Ssam 				printf("%s%s", sep, gr->gr_name);
4913440Ssam 				sep = " ";
5013440Ssam 				break;
5113440Ssam 			}
5213440Ssam 	printf("\n");
5313440Ssam 	exit(0);
5413440Ssam }
55