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>
17 /* The number of errors encountered so far. */
18 static int problems = 0;
20 /* Print the name or value of group ID GID. */
21 static void print_group(gid_t gid)
23 struct group *grp = NULL;
27 fprintf(stderr, "cannot find name for group ID %u\n", gid);
32 printf("%u", (unsigned)gid);
34 printf("%s", grp->gr_name);
37 static int xgetgroups(gid_t gid, int *n_groups, gid_t ** groups)
44 max_n_groups = getgroups(0, NULL);
46 /* Add 1 just in case max_n_groups is zero. */
47 g = (gid_t *) malloc(max_n_groups * sizeof(gid_t) + 1);
49 fprintf(stderr, "out of memory\n");
52 ng = getgroups(max_n_groups, g);
55 fprintf(stderr, "cannot get supplemental group list\n");
66 /* Print all of the distinct groups the user is in. */
67 int main(int argc, char *argv[])
71 pwd = getpwuid(getuid());
75 print_group(getgid());
76 if (getegid() != getgid()) {
78 print_group(getegid());
86 if (xgetgroups((pwd ? pwd->pw_gid : (gid_t) - 1),
87 &n_groups, &groups)) {
91 for (i = 0; i < n_groups; i++)
92 if (groups[i] != getgid() && groups[i] != getegid()) {
94 print_group(groups[i]);
99 return (problems != 0);