1 /* This test was ripped out of GNU 'id' from coreutils-5.0
5 * id is Copyright (C) 1989-2003 Free Software Foundation, Inc.
6 * and licensed under the GPL v2 or later, and was written by
7 * Arnold Robbins, with a major rewrite by David MacKenzie,
13 #include <sys/types.h>
18 /* The number of errors encountered so far. */
19 static int problems = 0;
21 /* Print the name or value of group ID GID. */
22 static void print_group(gid_t gid)
24 struct group *grp = NULL;
28 warn("cannot find name for group ID %u", gid);
33 printf("%u", (unsigned)gid);
35 printf("%s", grp->gr_name);
38 static int xgetgroups(gid_t gid, int *n_groups, gid_t ** groups)
45 max_n_groups = getgroups(0, NULL);
47 /* Add 1 just in case max_n_groups is zero. */
48 g = (gid_t *) malloc(max_n_groups * sizeof(gid_t) + 1);
50 err(EXIT_FAILURE, "out of memory");
51 ng = getgroups(max_n_groups, g);
54 warn("cannot get supplemental group list");
65 /* Print all of the distinct groups the user is in. */
66 int main(int argc, char *argv[])
70 pwd = getpwuid(getuid());
74 print_group(getgid());
75 if (getegid() != getgid()) {
77 print_group(getegid());
85 if (xgetgroups((pwd ? pwd->pw_gid : (gid_t) - 1),
86 &n_groups, &groups)) {
90 for (i = 0; i < n_groups; i++)
91 if (groups[i] != getgid() && groups[i] != getegid()) {
93 print_group(groups[i]);
98 return (problems != 0);