1956e8222Scf /* 2*67dbe2beSCasper H.S. Dik * Copyright 2009 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 167c478bd9Sstevel@tonic-gate /* 177c478bd9Sstevel@tonic-gate * groups 187c478bd9Sstevel@tonic-gate */ 197c478bd9Sstevel@tonic-gate 20f22acdffSgbrunett #include <sys/types.h> 217c478bd9Sstevel@tonic-gate #include <sys/param.h> 22*67dbe2beSCasper H.S. Dik #include <alloca.h> 237c478bd9Sstevel@tonic-gate #include <grp.h> 247c478bd9Sstevel@tonic-gate #include <pwd.h> 257c478bd9Sstevel@tonic-gate #include <stdio.h> 26f22acdffSgbrunett #include <stdlib.h> 27f22acdffSgbrunett #include <unistd.h> 28f22acdffSgbrunett #include <string.h> 297c478bd9Sstevel@tonic-gate 30f22acdffSgbrunett static void showgroups(char *user); 317c478bd9Sstevel@tonic-gate 32956e8222Scf int 33956e8222Scf main(int argc, char *argv[]) 347c478bd9Sstevel@tonic-gate { 35f22acdffSgbrunett int ngroups, i; 367c478bd9Sstevel@tonic-gate char *sep = ""; 37956e8222Scf struct group *gr; 38*67dbe2beSCasper H.S. Dik gid_t *groups; 39*67dbe2beSCasper H.S. Dik int maxgrp = sysconf(_SC_NGROUPS_MAX); 40*67dbe2beSCasper H.S. Dik 41*67dbe2beSCasper H.S. Dik groups = alloca(maxgrp * sizeof (gid_t)); 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate if (argc > 1) { 44f22acdffSgbrunett for (i = 1; i < argc; i++) 45f22acdffSgbrunett showgroups(argv[i]); 46f22acdffSgbrunett exit(0); 477c478bd9Sstevel@tonic-gate } 487c478bd9Sstevel@tonic-gate 49*67dbe2beSCasper H.S. Dik ngroups = getgroups(maxgrp, groups); 50f22acdffSgbrunett if (getpwuid(getuid()) == NULL) { 51f22acdffSgbrunett (void) fprintf(stderr, "groups: could not find passwd entry\n"); 527c478bd9Sstevel@tonic-gate exit(1); 537c478bd9Sstevel@tonic-gate } 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate for (i = 0; i < ngroups; i++) { 567c478bd9Sstevel@tonic-gate gr = getgrgid(groups[i]); 577c478bd9Sstevel@tonic-gate if (gr == NULL) { 58f48205beScasper (void) printf("%s%u", sep, groups[i]); 597c478bd9Sstevel@tonic-gate sep = " "; 607c478bd9Sstevel@tonic-gate continue; 617c478bd9Sstevel@tonic-gate } 62f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 637c478bd9Sstevel@tonic-gate sep = " "; 647c478bd9Sstevel@tonic-gate } 65f22acdffSgbrunett 66f22acdffSgbrunett (void) printf("\n"); 67956e8222Scf return (0); 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate 70f22acdffSgbrunett static void 71956e8222Scf showgroups(char *user) 727c478bd9Sstevel@tonic-gate { 73956e8222Scf struct group *gr; 74956e8222Scf struct passwd *pw; 75956e8222Scf char **cp; 767c478bd9Sstevel@tonic-gate char *sep = ""; 77f22acdffSgbrunett int pwgid_printed = 0; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate if ((pw = getpwnam(user)) == NULL) { 80f22acdffSgbrunett (void) fprintf(stderr, "groups: %s : No such user\n", user); 817c478bd9Sstevel@tonic-gate return; 827c478bd9Sstevel@tonic-gate } 83f22acdffSgbrunett setgrent(); 84f22acdffSgbrunett (void) printf("%s : ", user); 857c478bd9Sstevel@tonic-gate while (gr = getgrent()) { 867c478bd9Sstevel@tonic-gate if (pw->pw_gid == gr->gr_gid) { 87f22acdffSgbrunett /* 88f22acdffSgbrunett * To avoid duplicate group entries 897c478bd9Sstevel@tonic-gate */ 90f22acdffSgbrunett if (pwgid_printed == 0) { 91*67dbe2beSCasper H.S. Dik (void) printf("%s%s", sep, gr->gr_name); 92*67dbe2beSCasper H.S. Dik sep = " "; 93*67dbe2beSCasper H.S. Dik pwgid_printed = 1; 947c478bd9Sstevel@tonic-gate } 95f22acdffSgbrunett continue; 96f22acdffSgbrunett } 977c478bd9Sstevel@tonic-gate for (cp = gr->gr_mem; cp && *cp; cp++) 987c478bd9Sstevel@tonic-gate if (strcmp(*cp, user) == 0) { 99f22acdffSgbrunett (void) printf("%s%s", sep, gr->gr_name); 1007c478bd9Sstevel@tonic-gate sep = " "; 1017c478bd9Sstevel@tonic-gate break; 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate } 104f22acdffSgbrunett (void) printf("\n"); 105f22acdffSgbrunett endgrent(); 1067c478bd9Sstevel@tonic-gate } 107