1GETGROUPLIST(3)            Linux Programmer's Manual           GETGROUPLIST(3)
2
3
4

NAME

6       getgrouplist -  list of groups a user belongs to
7

SYNOPSIS

9       #include <grp.h>
10
11       int getgrouplist (const char *user, gid_t group,
12                         gid_t *groups, int *ngroups);
13

DESCRIPTION

15       The getgrouplist() function scans the group database for all the groups
16       user belongs to.  Up to  *ngroups  group  IDs  corresponding  to  these
17       groups  are stored in the array groups; the return value from the func‐
18       tion is the number of group IDs actually stored.  The  group  group  is
19       automatically  included  in  the  list  of  groups returned by getgrou‐
20       plist().
21

RETURN VALUE

23       If *ngroups is smaller than the total number of groups found, then get‐
24       grouplist() returns a value of `-1'.  In all cases the actual number of
25       groups is stored in *ngroups.
26

BUGS

28       The glibc 2.3.2 implementation of this function  is  broken:  it  over‐
29       writes memory when the actual number of groups is larger than *ngroups.
30

CONFORMING TO

32       This function is non-standard; it appears on most BSDs.
33

VERSIONS

35       This function is present since glibc 2.2.4.
36

EXAMPLE

38       /* This crashes with glibc 2.3.2 */
39       #include <stdio.h>
40       #include <stdlib.h>
41       #include <grp.h>
42       #include <pwd.h>
43
44       int main() {
45               int i, ng = 0;
46               char *user = "who";   /* username here */
47               gid_t *groups = NULL;
48               struct passwd *pw = getpwnam(user);
49               if (pw == NULL)
50                       return 0;
51
52               if (getgrouplist(user, pw->pw_gid, NULL, &ng) < 0) {
53                       groups = (gid_t *) malloc(ng * sizeof (gid_t));
54                       getgrouplist(user, pw->pw_gid, groups, &ng);
55               }
56
57               for(i = 0; i < ng; i++)
58                       printf("%d\n", groups[i]);
59
60               return 0;
61       }
62

SEE ALSO

64       getgroups(3), setgroups(3)
65
66
67
68GNU                               2003-11-18                   GETGROUPLIST(3)
Impressum