This source file includes following definitions.
- getugroups
- getugroups
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22 #include "getugroups.h"
23
24 #include <errno.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #if !HAVE_GRP_H || defined __ANDROID__
31
32
33
34
35
36
37
38 int
39 getugroups (_GL_UNUSED int maxcount,
40 _GL_UNUSED gid_t *grouplist,
41 _GL_UNUSED char const *username,
42 _GL_UNUSED gid_t gid)
43 {
44 errno = ENOSYS;
45 return -1;
46 }
47
48 #else
49 # include <grp.h>
50
51 # define STREQ(a, b) (strcmp (a, b) == 0)
52
53
54
55
56
57
58
59
60 int
61 getugroups (int maxcount, gid_t *grouplist, char const *username,
62 gid_t gid)
63 {
64 int count = 0;
65
66 if (gid != (gid_t) -1)
67 {
68 if (maxcount != 0)
69 grouplist[count] = gid;
70 ++count;
71 }
72
73 setgrent ();
74 while (1)
75 {
76 char **cp;
77 struct group *grp;
78
79 errno = 0;
80 grp = getgrent ();
81 if (grp == NULL)
82 break;
83
84 for (cp = grp->gr_mem; *cp; ++cp)
85 {
86 int n;
87
88 if ( ! STREQ (username, *cp))
89 continue;
90
91
92 for (n = 0; n < count; ++n)
93 if (grouplist && grouplist[n] == grp->gr_gid)
94 break;
95
96
97 if (n == count)
98 {
99 if (maxcount != 0)
100 {
101 if (count >= maxcount)
102 goto done;
103 grouplist[count] = grp->gr_gid;
104 }
105 if (count == INT_MAX)
106 {
107 errno = EOVERFLOW;
108 goto done;
109 }
110 count++;
111 }
112 }
113 }
114
115 if (errno != 0)
116 count = -1;
117
118 done:
119 {
120 int saved_errno = errno;
121 endgrent ();
122 errno = saved_errno;
123 }
124
125 return count;
126 }
127
128 #endif