xref: /csrg-svn/old/groups/groups.c (revision 34034)
121560Sdist /*
221560Sdist  * Copyright (c) 1980 Regents of the University of California.
3*34034Sbostic  * All rights reserved.
4*34034Sbostic  *
5*34034Sbostic  * Redistribution and use in source and binary forms are permitted
6*34034Sbostic  * provided that this notice is preserved and that due credit is given
7*34034Sbostic  * to the University of California at Berkeley. The name of the University
8*34034Sbostic  * may not be used to endorse or promote products derived from this
9*34034Sbostic  * software without specific prior written permission. This software
10*34034Sbostic  * is provided ``as is'' without express or implied warranty.
1121560Sdist  */
1221560Sdist 
1314517Ssam #ifndef lint
1421560Sdist char copyright[] =
1521560Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1621560Sdist  All rights reserved.\n";
17*34034Sbostic #endif /* not lint */
186013Swnj 
1921560Sdist #ifndef lint
20*34034Sbostic static char sccsid[] = "@(#)groups.c	5.2 (Berkeley) 04/20/88";
21*34034Sbostic #endif /* not lint */
2221560Sdist 
236013Swnj /*
246013Swnj  * groups
256013Swnj  */
266013Swnj 
276013Swnj #include <sys/param.h>
286013Swnj #include <grp.h>
296013Swnj #include <pwd.h>
3015082Ssam #include <stdio.h>
316013Swnj 
3211072Smckusick int	groups[NGROUPS];
336013Swnj 
346013Swnj main(argc, argv)
356013Swnj 	int argc;
366013Swnj 	char *argv[];
376013Swnj {
3813440Ssam 	int ngroups, i;
396013Swnj 	char *sep = "";
4013440Ssam 	struct group *gr;
416013Swnj 
4213440Ssam 	if (argc > 1)
4313440Ssam 		showgroups(argv[1]);
449249Ssam 	ngroups = getgroups(NGROUPS, groups);
459249Ssam 	for (i = 0; i < ngroups; i++) {
469249Ssam 		gr = getgrgid(groups[i]);
479249Ssam 		if (gr == NULL)
489249Ssam 			printf("%s%d", sep, groups[i]);
499249Ssam 		else
509249Ssam 			printf("%s%s", sep, gr->gr_name);
519249Ssam 		sep = " ";
529249Ssam 	}
536013Swnj 	printf("\n");
546013Swnj 	exit(0);
556013Swnj }
5613440Ssam 
5713440Ssam showgroups(user)
5813440Ssam 	register char *user;
5913440Ssam {
6013440Ssam 	register struct group *gr;
6115082Ssam 	register struct passwd *pw;
6213440Ssam 	register char **cp;
6313440Ssam 	char *sep = "";
6413440Ssam 
6515082Ssam 	if ((pw = getpwnam(user)) == NULL) {
66*34034Sbostic 		fprintf(stderr, "groups: no such user.\n");
6715082Ssam 		exit(1);
6815082Ssam 	}
6915082Ssam 	while (gr = getgrent()) {
7015082Ssam 		if (pw->pw_gid == gr->gr_gid) {
7115082Ssam 			printf("%s%s", sep, gr->gr_name);
7215082Ssam 			sep = " ";
7315082Ssam 			continue;
74*34034Sbostic 		}
7513440Ssam 		for (cp = gr->gr_mem; cp && *cp; cp++)
7613440Ssam 			if (strcmp(*cp, user) == 0) {
7713440Ssam 				printf("%s%s", sep, gr->gr_name);
7813440Ssam 				sep = " ";
7913440Ssam 				break;
8013440Ssam 			}
8115082Ssam 	}
8213440Ssam 	printf("\n");
8313440Ssam 	exit(0);
8413440Ssam }
85