1956e822cf/*
267dbe2bCasper H.S. Dik * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3956e822cf * Use is subject to license terms.
4956e822cf */
5956e822cf
67c478bdstevel@tonic-gate/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
87c478bdstevel@tonic-gate
97c478bdstevel@tonic-gate
107c478bdstevel@tonic-gate/*
117c478bdstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
127c478bdstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
137c478bdstevel@tonic-gate * specifies the terms and conditions for redistribution.
147c478bdstevel@tonic-gate */
157c478bdstevel@tonic-gate
167c478bdstevel@tonic-gate/*
177c478bdstevel@tonic-gate * groups
187c478bdstevel@tonic-gate */
197c478bdstevel@tonic-gate
20f22acdfgbrunett#include <sys/types.h>
217c478bdstevel@tonic-gate#include <sys/param.h>
2267dbe2bCasper H.S. Dik#include <alloca.h>
237c478bdstevel@tonic-gate#include <grp.h>
247c478bdstevel@tonic-gate#include <pwd.h>
257c478bdstevel@tonic-gate#include <stdio.h>
26f22acdfgbrunett#include <stdlib.h>
27f22acdfgbrunett#include <unistd.h>
28f22acdfgbrunett#include <string.h>
297c478bdstevel@tonic-gate
30f22acdfgbrunettstatic void showgroups(char *user);
317c478bdstevel@tonic-gate
32956e822cfint
33956e822cfmain(int argc, char *argv[])
347c478bdstevel@tonic-gate{
35f22acdfgbrunett	int ngroups, i;
367c478bdstevel@tonic-gate	char *sep = "";
37956e822cf	struct group *gr;
3867dbe2bCasper H.S. Dik	gid_t *groups;
3967dbe2bCasper H.S. Dik	int maxgrp = sysconf(_SC_NGROUPS_MAX);
4067dbe2bCasper H.S. Dik
4167dbe2bCasper H.S. Dik	groups = alloca(maxgrp * sizeof (gid_t));
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate	if (argc > 1) {
44f22acdfgbrunett		for (i = 1; i < argc; i++)
45f22acdfgbrunett			showgroups(argv[i]);
46f22acdfgbrunett		exit(0);
477c478bdstevel@tonic-gate	}
487c478bdstevel@tonic-gate
4967dbe2bCasper H.S. Dik	ngroups = getgroups(maxgrp, groups);
50f22acdfgbrunett	if (getpwuid(getuid()) == NULL) {
51f22acdfgbrunett		(void) fprintf(stderr, "groups: could not find passwd entry\n");
527c478bdstevel@tonic-gate		exit(1);
537c478bdstevel@tonic-gate	}
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gate	for (i = 0; i < ngroups; i++) {
567c478bdstevel@tonic-gate		gr = getgrgid(groups[i]);
577c478bdstevel@tonic-gate		if (gr == NULL) {
58f48205bcasper			(void) printf("%s%u", sep, groups[i]);
597c478bdstevel@tonic-gate			sep = " ";
607c478bdstevel@tonic-gate			continue;
617c478bdstevel@tonic-gate		}
62f22acdfgbrunett		(void) printf("%s%s", sep, gr->gr_name);
637c478bdstevel@tonic-gate		sep = " ";
647c478bdstevel@tonic-gate	}
65f22acdfgbrunett
66f22acdfgbrunett	(void) printf("\n");
67956e822cf	return (0);
687c478bdstevel@tonic-gate}
697c478bdstevel@tonic-gate
70f22acdfgbrunettstatic void
71956e822cfshowgroups(char *user)
727c478bdstevel@tonic-gate{
73956e822cf	struct group *gr;
74956e822cf	struct passwd *pw;
75956e822cf	char **cp;
767c478bdstevel@tonic-gate	char *sep = "";
77f22acdfgbrunett	int pwgid_printed = 0;
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate	if ((pw = getpwnam(user)) == NULL) {
80f22acdfgbrunett		(void) fprintf(stderr, "groups: %s : No such user\n", user);
817c478bdstevel@tonic-gate		return;
827c478bdstevel@tonic-gate	}
83f22acdfgbrunett	setgrent();
84f22acdfgbrunett	(void) printf("%s : ", user);
857c478bdstevel@tonic-gate	while (gr = getgrent()) {
867c478bdstevel@tonic-gate		if (pw->pw_gid == gr->gr_gid) {
87f22acdfgbrunett			/*
88f22acdfgbrunett			 * To avoid duplicate group entries
897c478bdstevel@tonic-gate			 */
90f22acdfgbrunett			if (pwgid_printed == 0) {
9167dbe2bCasper H.S. Dik				(void) printf("%s%s", sep, gr->gr_name);
9267dbe2bCasper H.S. Dik				sep = " ";
9367dbe2bCasper H.S. Dik				pwgid_printed = 1;
947c478bdstevel@tonic-gate			}
95f22acdfgbrunett			continue;
96f22acdfgbrunett		}
977c478bdstevel@tonic-gate		for (cp = gr->gr_mem; cp && *cp; cp++)
987c478bdstevel@tonic-gate			if (strcmp(*cp, user) == 0) {
99f22acdfgbrunett				(void) printf("%s%s", sep, gr->gr_name);
1007c478bdstevel@tonic-gate				sep = " ";
1017c478bdstevel@tonic-gate				break;
1027c478bdstevel@tonic-gate			}
1037c478bdstevel@tonic-gate	}
104f22acdfgbrunett	(void) printf("\n");
105f22acdfgbrunett	endgrent();
1067c478bdstevel@tonic-gate}
107