do it anyway!
[unix-history] / usr / src / old / groups / groups.c
CommitLineData
22e155fc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
476fcd16 13#ifndef lint
22e155fc
DF
14static char sccsid[] = "@(#)groups.c 5.1 (Berkeley) %G%";
15#endif not lint
90aff665
BJ
16
17/*
18 * groups
19 */
20
21#include <sys/param.h>
22#include <grp.h>
23#include <pwd.h>
6c905009 24#include <stdio.h>
90aff665 25
933c83d0 26int groups[NGROUPS];
90aff665
BJ
27
28main(argc, argv)
29 int argc;
30 char *argv[];
31{
a15fc49a 32 int ngroups, i;
90aff665 33 char *sep = "";
a15fc49a 34 struct group *gr;
90aff665 35
a15fc49a
SL
36 if (argc > 1)
37 showgroups(argv[1]);
a43be079
SL
38 ngroups = getgroups(NGROUPS, groups);
39 for (i = 0; i < ngroups; i++) {
40 gr = getgrgid(groups[i]);
41 if (gr == NULL)
42 printf("%s%d", sep, groups[i]);
43 else
44 printf("%s%s", sep, gr->gr_name);
45 sep = " ";
46 }
90aff665
BJ
47 printf("\n");
48 exit(0);
49}
a15fc49a
SL
50
51showgroups(user)
52 register char *user;
53{
54 register struct group *gr;
6c905009 55 register struct passwd *pw;
a15fc49a
SL
56 register char **cp;
57 char *sep = "";
58
6c905009
SL
59 if ((pw = getpwnam(user)) == NULL) {
60 fprintf(stderr, "No such user\n");
61 exit(1);
62 }
63 while (gr = getgrent()) {
64 if (pw->pw_gid == gr->gr_gid) {
65 printf("%s%s", sep, gr->gr_name);
66 sep = " ";
67 continue;
68 }
a15fc49a
SL
69 for (cp = gr->gr_mem; cp && *cp; cp++)
70 if (strcmp(*cp, user) == 0) {
71 printf("%s%s", sep, gr->gr_name);
72 sep = " ";
73 break;
74 }
6c905009 75 }
a15fc49a
SL
76 printf("\n");
77 exit(0);
78}