2 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" A few pieces remain from an earlier version written in
6 .\" 2002 by Walter Harms (walter.harms@informatik.uni-oldenburg.de)
8 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
10 .TH getgrouplist 3 (date) "Linux man-pages (unreleased)"
12 getgrouplist \- get list of groups to which a user belongs
15 .RI ( libc ", " \-lc )
20 .BI "int getgrouplist(const char *" user ", gid_t " group ,
21 .BI " gid_t *" groups ", int *" ngroups );
25 Feature Test Macro Requirements for glibc (see
26 .BR feature_test_macros (7)):
33 glibc 2.19 and earlier:
39 function scans the group database (see
41 to obtain the list of groups that
46 of these groups are returned in the array
49 If it was not among the groups defined for
51 in the group database, then
53 is included in the list of groups returned by
55 typically this argument is specified as the group ID from
56 the password record for
61 argument is a value-result argument:
62 on return it always contains the number of groups found for
66 this value may be greater than the number of groups stored in
69 If the number of groups of which
71 is a member is less than or equal to
77 If the user is a member of more than
82 In this case, the value returned in
84 can be used to resize the buffer passed to a further call to
87 For an explanation of the terms used in this section, see
93 Interface Attribute Value
98 T} Thread safety MT-Safe locale
106 the implementation of this function contains a buffer-overrun bug:
107 it returns the complete list of groups for
111 even when the number of groups exceeds
114 The program below displays the group list for the user named in its
115 first command-line argument.
116 The second command-line argument specifies the
118 value to be supplied to
120 The following shell session shows examples of the use of this program:
124 .RB "$" " ./a.out cecilia 0"
125 getgrouplist() returned \-1; ngroups = 3
126 .RB "$" " ./a.out cecilia 3"
135 .\" SRC BEGIN (getgrouplist.c)
143 main(int argc, char *argv[])
151 fprintf(stderr, "Usage: %s <user> <ngroups>\en", argv[0]);
155 ngroups = atoi(argv[2]);
157 groups = malloc(sizeof(*groups) * ngroups);
158 if (groups == NULL) {
163 /* Fetch passwd structure (contains first group ID for user). */
165 pw = getpwnam(argv[1]);
171 /* Retrieve group list. */
173 if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
174 fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\en",
179 /* Display list of retrieved groups, along with group names. */
181 fprintf(stderr, "ngroups = %d\en", ngroups);
182 for (size_t j = 0; j < ngroups; j++) {
183 printf("%d", groups[j]);
184 gr = getgrgid(groups[j]);
186 printf(" (%s)", gr\->gr_name);
198 .BR group_member (3),