xref: /original-bsd/old/groups/groups.c (revision 8b225582)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)groups.c	5.4 (Berkeley) 06/01/90";
16 #endif /* not lint */
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];
28 
29 main(argc, argv)
30 	int argc;
31 	char *argv[];
32 {
33 	int ngroups, i;
34 	char *sep = "";
35 	struct group *gr;
36 
37 	if (argc > 1)
38 		showgroups(argv[1]);
39 	ngroups = getgroups(NGROUPS, groups);
40 	for (i = 0; i < ngroups; i++) {
41 		gr = getgrgid(groups[i]);
42 		if (gr == NULL)
43 			printf("%s%d", sep, groups[i]);
44 		else
45 			printf("%s%s", sep, gr->gr_name);
46 		sep = " ";
47 	}
48 	printf("\n");
49 	exit(0);
50 }
51 
52 showgroups(user)
53 	register char *user;
54 {
55 	register struct group *gr;
56 	register struct passwd *pw;
57 	register char **cp;
58 	char *sep = "";
59 
60 	if ((pw = getpwnam(user)) == NULL) {
61 		fprintf(stderr, "groups: no such user.\n");
62 		exit(1);
63 	}
64 	while (gr = getgrent()) {
65 		if (pw->pw_gid == gr->gr_gid) {
66 			printf("%s%s", sep, gr->gr_name);
67 			sep = " ";
68 			continue;
69 		}
70 		for (cp = gr->gr_mem; cp && *cp; cp++)
71 			if (strcmp(*cp, user) == 0) {
72 				printf("%s%s", sep, gr->gr_name);
73 				sep = " ";
74 				break;
75 			}
76 	}
77 	printf("\n");
78 	exit(0);
79 }
80