xref: /csrg-svn/old/groups/groups.c (revision 42732)
121560Sdist /*
221560Sdist  * Copyright (c) 1980 Regents of the University of California.
334034Sbostic  * All rights reserved.
434034Sbostic  *
5*42732Sbostic  * %sccs.include.redist.c%
621560Sdist  */
721560Sdist 
814517Ssam #ifndef lint
921560Sdist char copyright[] =
1021560Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1121560Sdist  All rights reserved.\n";
1234034Sbostic #endif /* not lint */
136013Swnj 
1421560Sdist #ifndef lint
15*42732Sbostic static char sccsid[] = "@(#)groups.c	5.4 (Berkeley) 06/01/90";
1634034Sbostic #endif /* not lint */
1721560Sdist 
186013Swnj /*
196013Swnj  * groups
206013Swnj  */
216013Swnj 
226013Swnj #include <sys/param.h>
236013Swnj #include <grp.h>
246013Swnj #include <pwd.h>
2515082Ssam #include <stdio.h>
266013Swnj 
2711072Smckusick int	groups[NGROUPS];
286013Swnj 
main(argc,argv)296013Swnj main(argc, argv)
306013Swnj 	int argc;
316013Swnj 	char *argv[];
326013Swnj {
3313440Ssam 	int ngroups, i;
346013Swnj 	char *sep = "";
3513440Ssam 	struct group *gr;
366013Swnj 
3713440Ssam 	if (argc > 1)
3813440Ssam 		showgroups(argv[1]);
399249Ssam 	ngroups = getgroups(NGROUPS, groups);
409249Ssam 	for (i = 0; i < ngroups; i++) {
419249Ssam 		gr = getgrgid(groups[i]);
429249Ssam 		if (gr == NULL)
439249Ssam 			printf("%s%d", sep, groups[i]);
449249Ssam 		else
459249Ssam 			printf("%s%s", sep, gr->gr_name);
469249Ssam 		sep = " ";
479249Ssam 	}
486013Swnj 	printf("\n");
496013Swnj 	exit(0);
506013Swnj }
5113440Ssam 
showgroups(user)5213440Ssam showgroups(user)
5313440Ssam 	register char *user;
5413440Ssam {
5513440Ssam 	register struct group *gr;
5615082Ssam 	register struct passwd *pw;
5713440Ssam 	register char **cp;
5813440Ssam 	char *sep = "";
5913440Ssam 
6015082Ssam 	if ((pw = getpwnam(user)) == NULL) {
6134034Sbostic 		fprintf(stderr, "groups: no such user.\n");
6215082Ssam 		exit(1);
6315082Ssam 	}
6415082Ssam 	while (gr = getgrent()) {
6515082Ssam 		if (pw->pw_gid == gr->gr_gid) {
6615082Ssam 			printf("%s%s", sep, gr->gr_name);
6715082Ssam 			sep = " ";
6815082Ssam 			continue;
6934034Sbostic 		}
7013440Ssam 		for (cp = gr->gr_mem; cp && *cp; cp++)
7113440Ssam 			if (strcmp(*cp, user) == 0) {
7213440Ssam 				printf("%s%s", sep, gr->gr_name);
7313440Ssam 				sep = " ";
7413440Ssam 				break;
7513440Ssam 			}
7615082Ssam 	}
7713440Ssam 	printf("\n");
7813440Ssam 	exit(0);
7913440Ssam }
80