1956e8222Scf46844 /* 2956e8222Scf46844 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3956e8222Scf46844 * Use is subject to license terms. 4956e8222Scf46844 */ 5956e8222Scf46844 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 16956e8222Scf46844 #pragma ident "%Z%%M% %I% %E% SMI" 177c478bd9Sstevel@tonic-gate 187c478bd9Sstevel@tonic-gate /* 197c478bd9Sstevel@tonic-gate * groups 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate 22*f22acdffSgbrunett #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> 27*f22acdffSgbrunett #include <stdlib.h> 28*f22acdffSgbrunett #include <unistd.h> 29*f22acdffSgbrunett #include <string.h> 307c478bd9Sstevel@tonic-gate 31*f22acdffSgbrunett static void showgroups(char *user); 327c478bd9Sstevel@tonic-gate 33956e8222Scf46844 int 34956e8222Scf46844 main(int argc, char *argv[]) 357c478bd9Sstevel@tonic-gate { 36*f22acdffSgbrunett int ngroups, i; 377c478bd9Sstevel@tonic-gate char *sep = ""; 38956e8222Scf46844 struct group *gr; 39*f22acdffSgbrunett gid_t groups[NGROUPS_UMAX]; 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate if (argc > 1) { 427c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) 437c478bd9Sstevel@tonic-gate showgroups(argv[i]); 447c478bd9Sstevel@tonic-gate exit(0); 457c478bd9Sstevel@tonic-gate } 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate ngroups = getgroups(NGROUPS_UMAX, groups); 48*f22acdffSgbrunett if (getpwuid(getuid()) == NULL) { 49*f22acdffSgbrunett (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*f22acdffSgbrunett (void) printf("%s%ld", sep, groups[i]); 577c478bd9Sstevel@tonic-gate sep = " "; 587c478bd9Sstevel@tonic-gate continue; 597c478bd9Sstevel@tonic-gate } 60*f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 617c478bd9Sstevel@tonic-gate sep = " "; 627c478bd9Sstevel@tonic-gate } 63*f22acdffSgbrunett 64*f22acdffSgbrunett (void) printf("\n"); 65956e8222Scf46844 return (0); 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 68*f22acdffSgbrunett static void 69956e8222Scf46844 showgroups(char *user) 707c478bd9Sstevel@tonic-gate { 71956e8222Scf46844 struct group *gr; 72956e8222Scf46844 struct passwd *pw; 73956e8222Scf46844 char **cp; 747c478bd9Sstevel@tonic-gate char *sep = ""; 757c478bd9Sstevel@tonic-gate int pwgid_printed = 0; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate if ((pw = getpwnam(user)) == NULL) { 78*f22acdffSgbrunett (void) fprintf(stderr, "groups: %s : No such user\n", user); 797c478bd9Sstevel@tonic-gate return; 807c478bd9Sstevel@tonic-gate } 817c478bd9Sstevel@tonic-gate setgrent(); 82*f22acdffSgbrunett (void) printf("%s : ", user); 837c478bd9Sstevel@tonic-gate while (gr = getgrent()) { 847c478bd9Sstevel@tonic-gate if (pw->pw_gid == gr->gr_gid) { 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * To avoid duplicate group entries 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate if (pwgid_printed == 0) { 89*f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 907c478bd9Sstevel@tonic-gate sep = " "; 917c478bd9Sstevel@tonic-gate pwgid_printed = 1; 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate continue; 947c478bd9Sstevel@tonic-gate } 957c478bd9Sstevel@tonic-gate for (cp = gr->gr_mem; cp && *cp; cp++) 967c478bd9Sstevel@tonic-gate if (strcmp(*cp, user) == 0) { 97*f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 987c478bd9Sstevel@tonic-gate sep = " "; 997c478bd9Sstevel@tonic-gate break; 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate } 102*f22acdffSgbrunett (void) printf("\n"); 1037c478bd9Sstevel@tonic-gate endgrent(); 1047c478bd9Sstevel@tonic-gate } 105