OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / man3 / getgrouplist.3
1 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .\" A few pieces remain from an earlier version written in
25 .\" 2002 by Walter Harms (walter.harms@informatik.uni-oldenburg.de)
26 .\"
27 .TH GETGROUPLIST 3 2008-07-03 "GNU" "Linux Programmer's Manual"
28 .SH NAME
29 getgrouplist \- get list of groups to which a user belongs
30 .SH SYNOPSIS
31 .B #include <grp.h>
32 .sp
33 .BI "int getgrouplist(const char *" user ", gid_t " group ,
34 .br
35 .BI "                 gid_t *" groups ", int *" ngroups );
36 .sp
37 .in -4n
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .in
41 .sp
42 .BR getgrouplist ():
43 _BSD_SOURCE
44 .SH DESCRIPTION
45 The
46 .BR getgrouplist ()
47 function scans the group database (see
48 .BR group (5))
49 to obtain the list of groups that
50 .I user
51 belongs to.
52 Up to
53 .I *ngroups
54 of these groups are returned in the array
55 .IR groups .
56
57 If it was not among the groups defined for
58 .I user
59 in the group database, then
60 .I group
61 is included in the list of groups returned by
62 .BR getgrouplist ();
63 typically this argument is specified as the group ID from
64 the password record for
65 .IR user .
66
67 The
68 .I ngroups
69 argument is a value-result argument:
70 on return it always contains the number of groups found for
71 .IR user ,
72 including
73 .IR group ;
74 this value may be greater than the number of groups stored in
75 .IR groups .
76 .SH "RETURN VALUE"
77 If the number of groups of which
78 .I user
79 is a member is less than or equal to
80 .IR *ngroups ,
81 then the value
82 .I *ngroups
83 is returned.
84
85 If the user is a member of more than
86 .I *ngroups
87 groups, then
88 .BR getgrouplist ()
89 returns \-1.
90 In this case the value returned in
91 .IR *ngroups
92 can be used to resize the buffer passed to a further call
93 .BR getgrouplist ().
94 .SH "VERSIONS"
95 This function is present since glibc 2.2.4.
96 .SH "CONFORMING TO"
97 This function is nonstandard; it appears on most BSDs.
98 .SH BUGS
99 In glibc versions before 2.3.3,
100 the implementation of this function contains a buffer-overrun bug:
101 it returns the complete list of groups for
102 .IR user
103 in the array
104 .IR groups ,
105 even when the number of groups exceeds
106 .IR *ngroups .
107 .SH EXAMPLE
108 .PP
109 The program below displays the group list for the user named in its
110 first command-line argument.
111 The second command-line argument specifies the
112 .I ngroups
113 value to be supplied to
114 .BR getgrouplist ().
115 The following shell session shows examples of the use of this program:
116 .in +4n
117 .nf
118
119 .RB "$" " ./a.out cecilia 0"
120 getgrouplist() returned -1; ngroups = 3
121 .RB "$" " ./a.out cecilia 3"
122 ngroups = 3
123 16 (dialout)
124 33 (video)
125 100 (users)
126 .fi
127 .in
128 .SS Program source
129 \&
130 .nf
131 #include <stdio.h>
132 #include <stdlib.h>
133 #include <grp.h>
134 #include <pwd.h>
135
136 int
137 main(int argc, char *argv[])
138 {
139     int j, ngroups;
140     gid_t *groups;
141     struct passwd *pw;
142     struct group *gr;
143
144     if (argc != 3) {
145         fprintf(stderr, "Usage: %s <user> <ngroups>\\n", argv[0]);
146         exit(EXIT_FAILURE);
147     }
148
149     ngroups = atoi(argv[2]);
150
151     groups = malloc(ngroups * sizeof (gid_t));
152     if (groups == NULL) {
153         perror("malloc");
154         exit(EXIT_FAILURE);
155     }
156
157     /* Fetch passwd structure (contains first group ID for user) */
158
159     pw = getpwnam(argv[1]);
160     if (pw == NULL) {
161         perror("getpwnam");
162         exit(EXIT_SUCCESS);
163     }
164
165     /* Retrieve group list */
166
167     if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
168         fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\\n",
169                 ngroups);
170         exit(EXIT_FAILURE);
171     }
172
173     /* Display list of retrieved groups, along with group names */
174
175     fprintf(stderr, "ngroups = %d\\n", ngroups);
176     for (j = 0; j < ngroups; j++) {
177         printf("%d", groups[j]);
178         gr = getgrgid(groups[j]);
179         if (gr != NULL)
180             printf(" (%s)", gr\->gr_name);
181         printf("\\n");
182     }
183
184     exit(EXIT_SUCCESS);
185 }
186 .fi
187 .SH "SEE ALSO"
188 .BR getgroups (2),
189 .BR setgroups (2),
190 .BR getgrent (3),
191 .BR group (5),
192 .BR passwd (5)