1 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" A few pieces remain from an earlier version written in
25 .\" 2002 by Walter Harms (walter.harms@informatik.uni-oldenburg.de)
27 .\" Japanese Version Copyright (c) 2004 Yuichi SATO
28 .\" all rights reserved.
29 .\" Translated 2004-08-07, Yuichi SATO <ysato444@yahoo.co.jp>
30 .\" Updated 2008-08-11, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
32 .TH GETGROUPLIST 3 2009-07-03 "GNU" "Linux Programmer's Manual"
35 .\"O getgrouplist \- get list of groups to which a user belongs
36 getgrouplist \- ユーザが所属するグループのリストを取得する
41 .BI "int getgrouplist(const char *" user ", gid_t " group ,
43 .BI " gid_t *" groups ", int *" ngroups );
46 .\"O Feature Test Macro Requirements for glibc (see
47 .\"O .BR feature_test_macros (7)):
49 .RB ( feature_test_macros (7)
58 .\"O .BR getgrouplist ()
59 .\"O function scans the group database (see
61 .\"O to obtain the list of groups that
66 .\"O of these groups are returned in the array
80 .\"O If it was not among the groups defined for
82 .\"O in the group database, then
84 .\"O is included in the list of groups returned by
85 .\"O .BR getgrouplist ();
86 .\"O typically this argument is specified as the group ID from
87 .\"O the password record for
100 のパスワードレコードに書かれているグループ ID を指定する。
104 .\"O argument is a value-result argument:
105 .\"O on return it always contains the number of groups found for
109 .\"O this value may be greater than the number of groups stored in
113 は、値渡しと結果の両方に使用される引き数 (value-result argument) であり、
121 に格納されたグループ数より大きくなる可能性がある。
122 .\"O .SH "RETURN VALUE"
124 .\"O If the number of groups of which
126 .\"O is a member is less than or equal to
138 .\"O If the user is a member of more than
141 .\"O .BR getgrouplist ()
148 .\"O In this case the value returned in
150 .\"O can be used to resize the buffer passed to a further call
151 .\"O .BR getgrouplist ().
154 で返される値を使って、バッファのサイズを変更してから、
159 .\"O This function is present since glibc 2.2.4.
160 この関数は glibc 2.2.4 から存在する。
161 .\"O .SH "CONFORMING TO"
163 .\"O This function is nonstandard; it appears on most BSDs.
164 この関数は非標準である。ほとんどの BSD に存在する。
167 .\"O In glibc versions before 2.3.3,
168 .\"O the implementation of this function contains a buffer-overrun bug:
169 .\"O it returns the complete list of groups for
173 .\"O even when the number of groups exceeds
175 バージョン 2.3.3 より前の glibc では、
176 この関数の実装にはバッファオーバーフローのバグがあり、
188 .\"O The program below displays the group list for the user named in its
189 .\"O first command-line argument.
190 .\"O The second command-line argument specifies the
192 .\"O value to be supplied to
193 .\"O .BR getgrouplist ().
194 .\"O The following shell session shows examples of the use of this program:
195 以下のプログラムは、一つ目のコマンドライン引き数で指定された名前のユーザ
202 以下のシェルのセッションはこのプログラムの使用例を示したものである。
206 .RB "$" " ./a.out cecilia 0"
207 getgrouplist() returned \-1; ngroups = 3
208 .RB "$" " ./a.out cecilia 3"
215 .\"O .SS Program source
225 main(int argc, char *argv[])
233 fprintf(stderr, "Usage: %s <user> <ngroups>\\n", argv[0]);
237 ngroups = atoi(argv[2]);
239 groups = malloc(ngroups * sizeof (gid_t));
240 if (groups == NULL) {
245 /* Fetch passwd structure (contains first group ID for user) */
247 pw = getpwnam(argv[1]);
253 /* Retrieve group list */
255 if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
256 fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\\n",
261 /* Display list of retrieved groups, along with group names */
263 fprintf(stderr, "ngroups = %d\\n", ngroups);
264 for (j = 0; j < ngroups; j++) {
265 printf("%d", groups[j]);
266 gr = getgrgid(groups[j]);
268 printf(" (%s)", gr\->gr_name);