This source file includes following definitions.
- free_group_info
- get_group_info
- group_member
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include <unistd.h>
23
24 #include <stdio.h>
25 #include <sys/types.h>
26 #include <stdlib.h>
27
28 #include "intprops.h"
29
30
31
32 enum { GROUPBUF_SIZE = 100 };
33
34 struct group_info
35 {
36 gid_t *group;
37 gid_t groupbuf[GROUPBUF_SIZE];
38 };
39
40 static void
41 free_group_info (struct group_info const *g)
42 {
43 if (g->group != g->groupbuf)
44 free (g->group);
45 }
46
47 static int
48 get_group_info (struct group_info *gi)
49 {
50 int n_groups = getgroups (GROUPBUF_SIZE, gi->groupbuf);
51 gi->group = gi->groupbuf;
52
53 if (n_groups < 0)
54 {
55 int n_group_slots = getgroups (0, NULL);
56 size_t nbytes;
57 if (! INT_MULTIPLY_WRAPV (n_group_slots, sizeof *gi->group, &nbytes))
58 {
59 gi->group = malloc (nbytes);
60 if (gi->group)
61 n_groups = getgroups (n_group_slots, gi->group);
62 }
63 }
64
65
66 return n_groups;
67 }
68
69
70
71
72
73
74 int
75 group_member (gid_t gid)
76 {
77 int i;
78 int found;
79 struct group_info gi;
80 int n_groups = get_group_info (&gi);
81
82
83 found = 0;
84 for (i = 0; i < n_groups; i++)
85 {
86 if (gid == gi.group[i])
87 {
88 found = 1;
89 break;
90 }
91 }
92
93 free_group_info (&gi);
94
95 return found;
96 }
97
98 #ifdef TEST
99
100 int
101 main (int argc, char **argv)
102 {
103 int i;
104
105 for (i = 1; i < argc; i++)
106 {
107 gid_t gid;
108
109 gid = atoi (argv[i]);
110 printf ("%d: %s\n", gid, group_member (gid) ? "yes" : "no");
111 }
112 exit (0);
113 }
114
115 #endif