OSDN Git Service

test: Some more tests under conditionals
[uclinux-h8/uClibc.git] / test / pwd_grp / getgroups.c
1 /* This test was ripped out of GNU 'id' from coreutils-5.0
2  * by Erik Andersen.
3  *
4  *
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,
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <pwd.h>
15 #include <grp.h>
16
17 /* The number of errors encountered so far. */
18 static int problems = 0;
19
20 /* Print the name or value of group ID GID. */
21 static void print_group(gid_t gid)
22 {
23         struct group *grp = NULL;
24
25         grp = getgrgid(gid);
26         if (grp == NULL) {
27                 fprintf(stderr, "cannot find name for group ID %u\n", gid);
28                 problems++;
29         }
30
31         if (grp == NULL)
32                 printf("%u", (unsigned)gid);
33         else
34                 printf("%s", grp->gr_name);
35 }
36
37 static int xgetgroups(gid_t gid, int *n_groups, gid_t ** groups)
38 {
39         int max_n_groups;
40         int ng;
41         gid_t *g;
42         int fail = 0;
43
44         max_n_groups = getgroups(0, NULL);
45
46         /* Add 1 just in case max_n_groups is zero.  */
47         g = (gid_t *) malloc(max_n_groups * sizeof(gid_t) + 1);
48         if (g == NULL) {
49                 fprintf(stderr, "out of memory\n");
50                 exit(EXIT_FAILURE);
51         }
52         ng = getgroups(max_n_groups, g);
53
54         if (ng < 0) {
55                 fprintf(stderr, "cannot get supplemental group list\n");
56                 ++fail;
57                 free(g);
58         }
59         if (!fail) {
60                 *n_groups = ng;
61                 *groups = g;
62         }
63         return fail;
64 }
65
66 /* Print all of the distinct groups the user is in. */
67 int main(int argc, char *argv[])
68 {
69         struct passwd *pwd;
70
71         pwd = getpwuid(getuid());
72         if (pwd == NULL)
73                 problems++;
74
75         print_group(getgid());
76         if (getegid() != getgid()) {
77                 putchar(' ');
78                 print_group(getegid());
79         }
80
81         {
82                 int n_groups = 0;
83                 gid_t *groups;
84                 register int i;
85
86                 if (xgetgroups((pwd ? pwd->pw_gid : (gid_t) - 1),
87                                &n_groups, &groups)) {
88                         return ++problems;
89                 }
90
91                 for (i = 0; i < n_groups; i++)
92                         if (groups[i] != getgid() && groups[i] != getegid()) {
93                                 putchar(' ');
94                                 print_group(groups[i]);
95                         }
96                 free(groups);
97         }
98         putchar('\n');
99         return (problems != 0);
100 }