xref: /titanic_44/usr/src/ucbcmd/groups/groups.c (revision 1a7c1b724419d3cb5fa6eea75123c6b2060ba31b)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1980 Regents of the University of California.
7  * All rights reserved. The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13  * All Rights Reserved.
14  */
15 
16 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
17 
18 /*
19  * groups
20  */
21 
22 #include <sys/param.h>
23 #include <grp.h>
24 #include <pwd.h>
25 #include <stdio.h>
26 
27 int	groups[NGROUPS_UMAX];
28 
29 main(argc, argv)
30 	int argc;
31 	char *argv[];
32 {
33 	int ngroups, i, j;
34 	char *sep = "";
35 	register struct group *gr;
36 	struct passwd *pw;
37 
38 	if (argc > 1) {
39 		for (i=1; i < argc ; i++)
40         		showgroups(argv[i]);
41 		exit(0) ;
42 	}
43 
44 	ngroups = getgroups(NGROUPS_UMAX, groups);
45 	if ((pw = getpwuid(getuid())) == NULL) {
46 		fprintf(stderr, "groups: could not find passwd entry\n");
47 		exit(1);
48 	}
49 
50 	for (i = 0; i < ngroups; i++) {
51 		gr = getgrgid(groups[i]);
52 		if (gr == NULL) {
53 			printf("%s%d", sep, groups[i]);
54 			sep = " ";
55 			continue;
56 		}
57 		printf("%s%s", sep, gr->gr_name);
58 		sep = " ";
59 	}
60 	printf("\n");
61 	exit(0);
62 	/* NOTREACHED */
63 }
64 
65 showgroups(user)
66 	register char *user;
67 {
68 	register struct group *gr;
69 	register struct passwd *pw;
70 	register char **cp;
71 	char *sep = "";
72 	int pwgid_printed = 0 ;
73 
74 	if ((pw = getpwnam(user)) == NULL) {
75 		fprintf(stderr, "groups: %s : No such user\n", user);
76 		return;
77 	}
78 	setgrent() ;
79 	printf("%s : ", user) ;
80 	while (gr = getgrent()) {
81 		if (pw->pw_gid == gr->gr_gid) {
82 			/*
83 			 * To avoid duplicate group entries
84 			 */
85 			if (pwgid_printed==0) {
86 			    printf("%s%s", sep, gr->gr_name);
87 			    sep = " ";
88 			    pwgid_printed = 1 ;
89 			}
90 			continue ;
91 		}
92 		for (cp = gr->gr_mem; cp && *cp; cp++)
93 			if (strcmp(*cp, user) == 0) {
94 				printf("%s%s", sep, gr->gr_name);
95 				sep = " ";
96 				break;
97 			}
98 	}
99 	printf("\n");
100 	endgrent() ;
101 }
102