fix for ANSI C
[unix-history] / usr / src / old / groups / groups.c
CommitLineData
22e155fc
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
25875f65
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
22e155fc
DF
11 */
12
13#ifndef lint
14char copyright[] =
15"@(#) Copyright (c) 1980 Regents of the University of California.\n\
16 All rights reserved.\n";
25875f65 17#endif /* not lint */
22e155fc 18
476fcd16 19#ifndef lint
25875f65
KB
20static char sccsid[] = "@(#)groups.c 5.2 (Berkeley) %G%";
21#endif /* not lint */
90aff665
BJ
22
23/*
24 * groups
25 */
26
27#include <sys/param.h>
28#include <grp.h>
29#include <pwd.h>
6c905009 30#include <stdio.h>
90aff665 31
933c83d0 32int groups[NGROUPS];
90aff665
BJ
33
34main(argc, argv)
35 int argc;
36 char *argv[];
37{
a15fc49a 38 int ngroups, i;
90aff665 39 char *sep = "";
a15fc49a 40 struct group *gr;
90aff665 41
a15fc49a
SL
42 if (argc > 1)
43 showgroups(argv[1]);
a43be079
SL
44 ngroups = getgroups(NGROUPS, groups);
45 for (i = 0; i < ngroups; i++) {
46 gr = getgrgid(groups[i]);
47 if (gr == NULL)
48 printf("%s%d", sep, groups[i]);
49 else
50 printf("%s%s", sep, gr->gr_name);
51 sep = " ";
52 }
90aff665
BJ
53 printf("\n");
54 exit(0);
55}
a15fc49a
SL
56
57showgroups(user)
58 register char *user;
59{
60 register struct group *gr;
6c905009 61 register struct passwd *pw;
a15fc49a
SL
62 register char **cp;
63 char *sep = "";
64
6c905009 65 if ((pw = getpwnam(user)) == NULL) {
25875f65 66 fprintf(stderr, "groups: no such user.\n");
6c905009
SL
67 exit(1);
68 }
69 while (gr = getgrent()) {
70 if (pw->pw_gid == gr->gr_gid) {
71 printf("%s%s", sep, gr->gr_name);
72 sep = " ";
73 continue;
25875f65 74 }
a15fc49a
SL
75 for (cp = gr->gr_mem; cp && *cp; cp++)
76 if (strcmp(*cp, user) == 0) {
77 printf("%s%s", sep, gr->gr_name);
78 sep = " ";
79 break;
80 }
6c905009 81 }
a15fc49a
SL
82 printf("\n");
83 exit(0);
84}