1 /* group-list.c --Print a list of group IDs or names.
2 Copyright (C) 1989-2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Arnold Robbins.
18 Major rewrite by David MacKenzie, djm@gnu.ai.mit.edu.
19 Extracted from id.c by James Youngman. */
23 #include <sys/types.h>
29 #include "mgetgroups.h"
31 #include "group-list.h"
34 /* Print all of the distinct groups the user is in. */
36 print_group_list (const char *username
,
37 uid_t ruid
, gid_t rgid
, gid_t egid
,
43 pwd
= getpwuid (ruid
);
47 if (!print_group (rgid
, use_names
))
53 if (!print_group (egid
, use_names
))
62 int n_groups
= mgetgroups (username
, (pwd
? pwd
->pw_gid
: (gid_t
) -1),
68 error (0, errno
, _("failed to get groups for user %s"),
73 error (0, errno
, _("failed to get groups for the current process"));
78 for (i
= 0; i
< n_groups
; i
++)
79 if (groups
[i
] != rgid
&& groups
[i
] != egid
)
82 if (!print_group (groups
[i
], use_names
))
87 #endif /* HAVE_GETGROUPS */
92 /* Print the name or value of group ID GID. */
94 print_group (gid_t gid
, bool use_name
)
96 struct group
*grp
= NULL
;
101 grp
= getgrgid (gid
);
104 error (0, 0, _("cannot find name for group ID %lu"),
105 (unsigned long int) gid
);
111 printf ("%lu", (unsigned long int) gid
);
113 printf ("%s", grp
->gr_name
);
119 * indent-tabs-mode: nil