2 * Copyright 1991 - 1993, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 RCSID (PKG_VER "$Id: groups.c,v 1.8 2002/01/05 15:41:43 kloczek Exp $")
37 #include "prototypes.h"
39 /* local function prototypes */
40 static void print_groups (const char *);
43 * print_groups - print the groups which the named user is a member of
45 * print_groups() scans the groups file for the list of groups which
46 * the user is listed as being a member of.
49 static void print_groups (const char *member)
58 if ((pwd = getpwnam (member)) == 0) {
59 fprintf (stderr, _("unknown user %s\n"), member);
62 while ((grp = getgrent ())) {
63 if (is_on_list (grp->gr_mem, member)) {
67 printf ("%s", grp->gr_name);
68 if (grp->gr_gid == pwd->pw_gid)
72 if (!flag && (grp = getgrgid (pwd->pw_gid))) {
76 printf ("%s", grp->gr_name);
83 * groups - print out the groups a process is a member of
86 int main (int argc, char **argv)
101 sys_ngroups = sysconf (_SC_NGROUPS_MAX);
102 #ifdef HAVE_GETGROUPS
103 groups = malloc (sys_ngroups * sizeof (GETGROUPS_T));
105 setlocale (LC_ALL, "");
106 bindtextdomain (PACKAGE, LOCALEDIR);
107 textdomain (PACKAGE);
112 * Called with no arguments - give the group set for the
116 #ifdef HAVE_GETGROUPS
118 * This system supports concurrent group sets, so I can ask
119 * the system to tell me which groups are currently set for
123 ngroups = getgroups (sys_ngroups, groups);
125 perror ("getgroups");
130 * The groupset includes the primary group as well.
133 pri_grp = getegid ();
134 for (i = 0; i < ngroups; i++)
135 if (pri_grp == (int) groups[i])
142 * Print out the name of every group in the current group
143 * set. Unknown groups are printed as their decimal group ID
148 if ((gr = getgrgid (pri_grp)))
149 printf ("%s", gr->gr_name);
151 printf ("%d", pri_grp);
154 for (i = 0; i < ngroups; i++) {
155 if (i || pri_grp != -1)
158 if ((gr = getgrgid (groups[i])))
159 printf ("%s", gr->gr_name);
161 printf ("%ld", (long) groups[i]);
166 * This system does not have the getgroups() system call, so
167 * I must check the groups file directly.
170 if ((logname = getlogin ()))
171 print_groups (logname);
178 * The invoker wanted to know about some other user. Use
179 * that name to look up the groups instead.
182 print_groups (argv[1]);