]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 2002 Walter Harms (walter.harms@informatik.uni-oldenburg.de) |
2 | .\" Distributed under GPL | |
3 | .\" Thanks to glibc info pages | |
4 | .\" | |
5 | .\" Modified 2003-11-18, aeb: glibc is broken | |
cc4615cc | 6 | .TH GETGROUPLIST 3 2007-07-26 "GNU" "Linux Programmer's Manual" |
fea681da MK |
7 | .SH NAME |
8 | getgrouplist \- list of groups a user belongs to | |
9 | .SH SYNOPSIS | |
c13182ef | 10 | .sp |
fea681da MK |
11 | .B #include <grp.h> |
12 | .sp | |
b9f02710 | 13 | .BI "int getgrouplist(const char *" user ", gid_t " group , |
cc4615cc MK |
14 | .br |
15 | .BI " gid_t *" groups ", int *" ngroups ); | |
16 | .sp | |
17 | .in -4n | |
18 | Feature Test Macro Requirements for glibc (see | |
19 | .BR feature_test_macros (7)): | |
20 | .in | |
21 | .sp | |
22 | .BR getgrouplist (): | |
23 | _BSD_SOURCE | |
fea681da | 24 | .SH DESCRIPTION |
c13182ef | 25 | The |
63aa9df0 | 26 | .BR getgrouplist () |
fea681da MK |
27 | function scans the group database for all the groups |
28 | .I user | |
c13182ef MK |
29 | belongs to. |
30 | Up to | |
fea681da MK |
31 | .RI * ngroups |
32 | group IDs corresponding to these groups are stored in the array | |
33 | .IR groups ; | |
34 | the return value from the function is the number of group IDs | |
c13182ef MK |
35 | actually stored. |
36 | The group | |
fea681da | 37 | .I group |
c13182ef | 38 | is automatically included in the list of groups returned by |
4d52e8f8 | 39 | .BR getgrouplist (). |
fea681da MK |
40 | .SH "RETURN VALUE" |
41 | If | |
c13182ef | 42 | .RI * ngroups |
fea681da | 43 | is smaller than the total number of groups found, then |
63aa9df0 | 44 | .BR getgrouplist () |
2b0fa182 | 45 | returns \-1. |
fea681da MK |
46 | In all cases the actual number of groups is stored in |
47 | .RI * ngroups . | |
2b2581ee MK |
48 | .SH "VERSIONS" |
49 | This function is present since glibc 2.2.4. | |
50 | .SH "CONFORMING TO" | |
51 | This function is non-standard; it appears on most BSDs. | |
fea681da MK |
52 | .SH BUGS |
53 | The glibc 2.3.2 implementation of this function is broken: | |
54 | it overwrites memory when the actual number of groups is larger than | |
55 | .RI * ngroups . | |
fea681da MK |
56 | .SH EXAMPLE |
57 | .nf | |
58 | /* This crashes with glibc 2.3.2 */ | |
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include <grp.h> | |
62 | #include <pwd.h> | |
63 | ||
c13182ef MK |
64 | int |
65 | main(void) | |
cf0a9ace MK |
66 | { |
67 | int i, ng = 0; | |
68 | char *user = "who"; /* username here */ | |
69 | gid_t *groups = NULL; | |
70 | struct passwd *pw = getpwnam(user); | |
fea681da | 71 | |
cf0a9ace | 72 | if (pw == NULL) |
5bc8c34c | 73 | exit(EXIT_SUCCESS); |
fea681da | 74 | |
29059a65 | 75 | if (getgrouplist(user, pw\->pw_gid, NULL, &ng) < 0) { |
cf0a9ace | 76 | groups = (gid_t *) malloc(ng * sizeof (gid_t)); |
29059a65 | 77 | getgrouplist(user, pw\->pw_gid, groups, &ng); |
cf0a9ace | 78 | } |
fea681da | 79 | |
d4949190 | 80 | for (i = 0; i < ng; i++) |
cf0a9ace MK |
81 | printf("%d\en", groups[i]); |
82 | ||
5bc8c34c | 83 | exit(EXIT_SUCCESS); |
fea681da MK |
84 | } |
85 | .fi | |
86 | .SH "SEE ALSO" | |
f56f5b89 MK |
87 | .BR getgroups (2), |
88 | .BR setgroups (2) |