1956e8222Scf /* 2*f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 3956e8222Scf * Use is subject to license terms. 4956e8222Scf */ 5956e8222Scf 67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 77c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 87c478bd9Sstevel@tonic-gate 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate /* 117c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 127c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 137c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 147c478bd9Sstevel@tonic-gate */ 157c478bd9Sstevel@tonic-gate 16956e8222Scf #pragma ident "%Z%%M% %I% %E% SMI" 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate /* 197c478bd9Sstevel@tonic-gate * groups 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 22f22acdffSgbrunett #include <sys/types.h> 237c478bd9Sstevel@tonic-gate #include <sys/param.h> 247c478bd9Sstevel@tonic-gate #include <grp.h> 257c478bd9Sstevel@tonic-gate #include <pwd.h> 267c478bd9Sstevel@tonic-gate #include <stdio.h> 27f22acdffSgbrunett #include <stdlib.h> 28f22acdffSgbrunett #include <unistd.h> 29f22acdffSgbrunett #include <string.h> 307c478bd9Sstevel@tonic-gate 31f22acdffSgbrunett static void showgroups(char *user); 327c478bd9Sstevel@tonic-gate 33956e8222Scf int 34956e8222Scf main(int argc, char *argv[]) 357c478bd9Sstevel@tonic-gate { 36f22acdffSgbrunett int ngroups, i; 377c478bd9Sstevel@tonic-gate char *sep = ""; 38956e8222Scf struct group *gr; 39f22acdffSgbrunett gid_t groups[NGROUPS_UMAX]; 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate if (argc > 1) { 42f22acdffSgbrunett for (i = 1; i < argc; i++) 43f22acdffSgbrunett showgroups(argv[i]); 44f22acdffSgbrunett exit(0); 457c478bd9Sstevel@tonic-gate } 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate ngroups = getgroups(NGROUPS_UMAX, groups); 48f22acdffSgbrunett if (getpwuid(getuid()) == NULL) { 49f22acdffSgbrunett (void) fprintf(stderr, "groups: could not find passwd entry\n"); 507c478bd9Sstevel@tonic-gate exit(1); 517c478bd9Sstevel@tonic-gate } 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate for (i = 0; i < ngroups; i++) { 547c478bd9Sstevel@tonic-gate gr = getgrgid(groups[i]); 557c478bd9Sstevel@tonic-gate if (gr == NULL) { 56*f48205beScasper (void) printf("%s%u", sep, groups[i]); 577c478bd9Sstevel@tonic-gate sep = " "; 587c478bd9Sstevel@tonic-gate continue; 597c478bd9Sstevel@tonic-gate } 60f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 617c478bd9Sstevel@tonic-gate sep = " "; 627c478bd9Sstevel@tonic-gate } 63f22acdffSgbrunett 64f22acdffSgbrunett (void) printf("\n"); 65956e8222Scf return (0); 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 68f22acdffSgbrunett static void 69956e8222Scf showgroups(char *user) 707c478bd9Sstevel@tonic-gate { 71956e8222Scf struct group *gr; 72956e8222Scf struct passwd *pw; 73956e8222Scf char **cp; 747c478bd9Sstevel@tonic-gate char *sep = ""; 75f22acdffSgbrunett int pwgid_printed = 0; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate if ((pw = getpwnam(user)) == NULL) { 78f22acdffSgbrunett (void) fprintf(stderr, "groups: %s : No such user\n", user); 797c478bd9Sstevel@tonic-gate return; 807c478bd9Sstevel@tonic-gate } 81f22acdffSgbrunett setgrent(); 82f22acdffSgbrunett (void) printf("%s : ", user); 837c478bd9Sstevel@tonic-gate while (gr = getgrent()) { 847c478bd9Sstevel@tonic-gate if (pw->pw_gid == gr->gr_gid) { 85f22acdffSgbrunett /* 86f22acdffSgbrunett * To avoid duplicate group entries 877c478bd9Sstevel@tonic-gate */ 88f22acdffSgbrunett if (pwgid_printed == 0) { 89f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 907c478bd9Sstevel@tonic-gate sep = " "; 91f22acdffSgbrunett pwgid_printed = 1; 927c478bd9Sstevel@tonic-gate } 93f22acdffSgbrunett continue; 94f22acdffSgbrunett } 957c478bd9Sstevel@tonic-gate for (cp = gr->gr_mem; cp && *cp; cp++) 967c478bd9Sstevel@tonic-gate if (strcmp(*cp, user) == 0) { 97f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 987c478bd9Sstevel@tonic-gate sep = " "; 997c478bd9Sstevel@tonic-gate break; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate } 102f22acdffSgbrunett (void) printf("\n"); 103f22acdffSgbrunett endgrent(); 1047c478bd9Sstevel@tonic-gate } 105