1273Scf46844 /*
2*11134SCasper.Dik@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
3273Scf46844 * Use is subject to license terms.
4273Scf46844 */
5273Scf46844
60Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
70Sstevel@tonic-gate /* All Rights Reserved */
80Sstevel@tonic-gate
90Sstevel@tonic-gate
100Sstevel@tonic-gate /*
110Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
120Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
130Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
140Sstevel@tonic-gate */
150Sstevel@tonic-gate
160Sstevel@tonic-gate /*
170Sstevel@tonic-gate * groups
180Sstevel@tonic-gate */
190Sstevel@tonic-gate
20319Sgbrunett #include <sys/types.h>
210Sstevel@tonic-gate #include <sys/param.h>
22*11134SCasper.Dik@Sun.COM #include <alloca.h>
230Sstevel@tonic-gate #include <grp.h>
240Sstevel@tonic-gate #include <pwd.h>
250Sstevel@tonic-gate #include <stdio.h>
26319Sgbrunett #include <stdlib.h>
27319Sgbrunett #include <unistd.h>
28319Sgbrunett #include <string.h>
290Sstevel@tonic-gate
30319Sgbrunett static void showgroups(char *user);
310Sstevel@tonic-gate
32273Scf46844 int
main(int argc,char * argv[])33273Scf46844 main(int argc, char *argv[])
340Sstevel@tonic-gate {
35319Sgbrunett int ngroups, i;
360Sstevel@tonic-gate char *sep = "";
37273Scf46844 struct group *gr;
38*11134SCasper.Dik@Sun.COM gid_t *groups;
39*11134SCasper.Dik@Sun.COM int maxgrp = sysconf(_SC_NGROUPS_MAX);
40*11134SCasper.Dik@Sun.COM
41*11134SCasper.Dik@Sun.COM groups = alloca(maxgrp * sizeof (gid_t));
420Sstevel@tonic-gate
430Sstevel@tonic-gate if (argc > 1) {
44319Sgbrunett for (i = 1; i < argc; i++)
45319Sgbrunett showgroups(argv[i]);
46319Sgbrunett exit(0);
470Sstevel@tonic-gate }
480Sstevel@tonic-gate
49*11134SCasper.Dik@Sun.COM ngroups = getgroups(maxgrp, groups);
50319Sgbrunett if (getpwuid(getuid()) == NULL) {
51319Sgbrunett (void) fprintf(stderr, "groups: could not find passwd entry\n");
520Sstevel@tonic-gate exit(1);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate
550Sstevel@tonic-gate for (i = 0; i < ngroups; i++) {
560Sstevel@tonic-gate gr = getgrgid(groups[i]);
570Sstevel@tonic-gate if (gr == NULL) {
584321Scasper (void) printf("%s%u", sep, groups[i]);
590Sstevel@tonic-gate sep = " ";
600Sstevel@tonic-gate continue;
610Sstevel@tonic-gate }
62319Sgbrunett (void) printf("%s%s", sep, gr->gr_name);
630Sstevel@tonic-gate sep = " ";
640Sstevel@tonic-gate }
65319Sgbrunett
66319Sgbrunett (void) printf("\n");
67273Scf46844 return (0);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
70319Sgbrunett static void
showgroups(char * user)71273Scf46844 showgroups(char *user)
720Sstevel@tonic-gate {
73273Scf46844 struct group *gr;
74273Scf46844 struct passwd *pw;
75273Scf46844 char **cp;
760Sstevel@tonic-gate char *sep = "";
77319Sgbrunett int pwgid_printed = 0;
780Sstevel@tonic-gate
790Sstevel@tonic-gate if ((pw = getpwnam(user)) == NULL) {
80319Sgbrunett (void) fprintf(stderr, "groups: %s : No such user\n", user);
810Sstevel@tonic-gate return;
820Sstevel@tonic-gate }
83319Sgbrunett setgrent();
84319Sgbrunett (void) printf("%s : ", user);
850Sstevel@tonic-gate while (gr = getgrent()) {
860Sstevel@tonic-gate if (pw->pw_gid == gr->gr_gid) {
87319Sgbrunett /*
88319Sgbrunett * To avoid duplicate group entries
890Sstevel@tonic-gate */
90319Sgbrunett if (pwgid_printed == 0) {
91*11134SCasper.Dik@Sun.COM (void) printf("%s%s", sep, gr->gr_name);
92*11134SCasper.Dik@Sun.COM sep = " ";
93*11134SCasper.Dik@Sun.COM pwgid_printed = 1;
940Sstevel@tonic-gate }
95319Sgbrunett continue;
96319Sgbrunett }
970Sstevel@tonic-gate for (cp = gr->gr_mem; cp && *cp; cp++)
980Sstevel@tonic-gate if (strcmp(*cp, user) == 0) {
99319Sgbrunett (void) printf("%s%s", sep, gr->gr_name);
1000Sstevel@tonic-gate sep = " ";
1010Sstevel@tonic-gate break;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate }
104319Sgbrunett (void) printf("\n");
105319Sgbrunett endgrent();
1060Sstevel@tonic-gate }
107