OSDN Git Service

(split) LDP: Update release from the latest PO files
[linuxjm/LDP_man-pages.git] / release / man3 / getgrouplist.3
1 .\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
4 .\" A few pieces remain from an earlier version written in
5 .\" 2002 by Walter Harms (walter.harms@informatik.uni-oldenburg.de)
6 .\"
7 .\" %%%LICENSE_START(VERBATIM)
8 .\" Permission is granted to make and distribute verbatim copies of this
9 .\" manual provided the copyright notice and this permission notice are
10 .\" preserved on all copies.
11 .\"
12 .\" Permission is granted to copy and distribute modified versions of this
13 .\" manual under the conditions for verbatim copying, provided that the
14 .\" entire resulting derived work is distributed under the terms of a
15 .\" permission notice identical to this one.
16 .\"
17 .\" Since the Linux kernel and libraries are constantly changing, this
18 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
19 .\" responsibility for errors or omissions, or for damages resulting from
20 .\" the use of the information contained herein.  The author(s) may not
21 .\" have taken the same level of care in the production of this manual,
22 .\" which is licensed free of charge, as they might when working
23 .\" professionally.
24 .\"
25 .\" Formatted or processed versions of this manual, if unaccompanied by
26 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" %%%LICENSE_END
28 .\"
29 .\"*******************************************************************
30 .\"
31 .\" This file was generated with po4a. Translate the source file.
32 .\"
33 .\"*******************************************************************
34 .TH GETGROUPLIST 3 2009\-07\-03 GNU "Linux Programmer's Manual"
35 .SH 名前
36 getgrouplist \- ユーザが所属するグループのリストを取得する
37 .SH 書式
38 \fB#include <grp.h>\fP
39 .sp
40 \fBint getgrouplist(const char *\fP\fIuser\fP\fB, gid_t \fP\fIgroup\fP\fB,\fP
41 .br
42 \fB gid_t *\fP\fIgroups\fP\fB, int *\fP\fIngroups\fP\fB);\fP
43 .sp
44 .in -4n
45 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
46 .in
47 .sp
48 \fBgetgrouplist\fP(): _BSD_SOURCE
49 .SH 説明
50 \fBgetgrouplist\fP()  関数は、グループデータベース (\fBgroup\fP(5)  参照) を調べて、 \fIuser\fP
51 が所属するグループのリストを取得する。 見つかったグループのうち最大 \fI*ngroups\fP 個のグループが、配列 \fIgroups\fP
52 に格納されて返される。
53
54 引き数 \fIgroup\fP がグループデータベースに \fIuser\fP が所属するグループがなかった場合、 \fBgetgrouplist\fP()
55 が返すグループのリストに引き数 \fIgroup\fP も追加される。 通常は、この引き数にはユーザ \fIuser\fP
56 のパスワードレコードに書かれているグループ ID を指定する。
57
58 引き数 \fIngroups\fP は、値渡しと結果の両方に使用される引き数 (value\-result argument) であり、 リターン時には、常に
59 \fIgroup\fP も含めた \fIuser\fP が所属するグループ数が格納される。 この値は \fIgroups\fP
60 に格納されたグループ数より大きくなる可能性がある。
61 .SH 返り値
62 \fIuser\fP が所属しているグループ数が \fI*ngroups\fP 以下の場合、 \fI*ngroups\fP の値が返される。
63
64 指定されたユーザが \fI*ngroups\fP より多くのグループに所属している場合、 \fBgetgrouplist\fP()  は \-1 を返す。 この場合、
65 \fI*ngroups\fP で返される値を使って、バッファのサイズを変更してから、 \fBgetgrouplist\fP()  をもう一度呼び出すことができる。
66 .SH バージョン
67 この関数は glibc 2.2.4 から存在する。
68 .SH 準拠
69 この関数は非標準である。ほとんどの BSD に存在する。
70 .SH バグ
71 バージョン 2.3.3 より前の glibc では、 この関数の実装にはバッファオーバーフローのバグがあり、 \fIuser\fP が所属するグループ数が
72 \fI*ngroups\fP より多い場合であっても、 \fIuser\fP が所属するグループの全リストを配列 \fIgroups\fP に格納してしまう。
73 .SH 例
74 .PP
75 以下のプログラムは、一つ目のコマンドライン引き数で指定された名前のユーザ が所属するグループのリストを表示する。 二番目のコマンドライン引き数には、
76 \fBgetgrouplist\fP()  に渡す \fIngroups\fP の値を指定する。
77 以下のシェルのセッションはこのプログラムの使用例を示したものである。
78 .in +4n
79 .nf
80
81 $\fB ./a.out cecilia 0\fP
82 getgrouplist() returned \-1; ngroups = 3
83 $\fB ./a.out cecilia 3\fP
84 ngroups = 3
85 16 (dialout)
86 33 (video)
87 100 (users)
88 .fi
89 .in
90 .SS プログラムのソース
91 \&
92 .nf
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <grp.h>
96 #include <pwd.h>
97
98 int
99 main(int argc, char *argv[])
100 {
101     int j, ngroups;
102     gid_t *groups;
103     struct passwd *pw;
104     struct group *gr;
105
106     if (argc != 3) {
107         fprintf(stderr, "Usage: %s <user> <ngroups>\en", argv[0]);
108         exit(EXIT_FAILURE);
109     }
110
111     ngroups = atoi(argv[2]);
112
113     groups = malloc(ngroups * sizeof (gid_t));
114     if (groups == NULL) {
115         perror("malloc");
116         exit(EXIT_FAILURE);
117     }
118
119     /* Fetch passwd structure (contains first group ID for user) */
120
121     pw = getpwnam(argv[1]);
122     if (pw == NULL) {
123         perror("getpwnam");
124         exit(EXIT_SUCCESS);
125     }
126
127     /* Retrieve group list */
128
129     if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
130         fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\en",
131                 ngroups);
132         exit(EXIT_FAILURE);
133     }
134
135     /* Display list of retrieved groups, along with group names */
136
137     fprintf(stderr, "ngroups = %d\en", ngroups);
138     for (j = 0; j < ngroups; j++) {
139         printf("%d", groups[j]);
140         gr = getgrgid(groups[j]);
141         if (gr != NULL)
142             printf(" (%s)", gr\->gr_name);
143         printf("\en");
144     }
145
146     exit(EXIT_SUCCESS);
147 }
148 .fi
149 .SH 関連項目
150 \fBgetgroups\fP(2), \fBsetgroups\fP(2), \fBgetgrent\fP(3), \fBgroup\fP(5), \fBpasswd\fP(5)
151 .SH この文書について
152 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
153 である。プロジェクトの説明とバグ報告に関する情報は
154 http://www.kernel.org/doc/man\-pages/ に書かれている。