OSDN Git Service

Update README
[linuxjm/LDP_man-pages.git] / draft / 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 .\"
35 .\" Japanese Version Copyright (c) 2004 Yuichi SATO
36 .\"         all rights reserved.
37 .\" Translated 2004-08-07, Yuichi SATO <ysato444@yahoo.co.jp>
38 .\" Updated 2008-08-11, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v3.05
39 .\"
40 .TH GETGROUPLIST 3 2014\-08\-19 GNU "Linux Programmer's Manual"
41 .SH 名前
42 getgrouplist \- ユーザーが所属するグループのリストを取得する
43 .SH 書式
44 \fB#include <grp.h>\fP
45 .sp
46 \fBint getgrouplist(const char *\fP\fIuser\fP\fB, gid_t \fP\fIgroup\fP\fB,\fP
47 .br
48 \fB gid_t *\fP\fIgroups\fP\fB, int *\fP\fIngroups\fP\fB);\fP
49 .sp
50 .in -4n
51 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
52 .in
53 .sp
54 \fBgetgrouplist\fP(): _BSD_SOURCE
55 .SH 説明
56 \fBgetgrouplist\fP()  関数は、グループデータベース (\fBgroup\fP(5)  参照) を調べて、 \fIuser\fP
57 が所属するグループのリストを取得する。 見つかったグループのうち最大 \fI*ngroups\fP 個のグループが、配列 \fIgroups\fP
58 に格納されて返される。
59
60 引き数 \fIgroup\fP がグループデータベースに \fIuser\fP が所属するグループがなかった場合、 \fBgetgrouplist\fP()
61 が返すグループのリストに引き数 \fIgroup\fP も追加される。 通常は、この引き数にはユーザー \fIuser\fP
62 のパスワードレコードに書かれているグループ ID を指定する。
63
64 引き数 \fIngroups\fP は、値渡しと結果の両方に使用される引き数 (value\-result argument) であり、 リターン時には、常に
65 \fIgroup\fP も含めた \fIuser\fP が所属するグループ数が格納される。 この値は \fIgroups\fP
66 に格納されたグループ数より大きくなる可能性がある。
67 .SH 返り値
68 \fIuser\fP が所属しているグループ数が \fI*ngroups\fP 以下の場合、 \fI*ngroups\fP の値が返される。
69
70 指定されたユーザーが \fI*ngroups\fP より多くのグループに所属している場合、 \fBgetgrouplist\fP()  は \-1 を返す。
71 この場合、 \fI*ngroups\fP で返される値を使って、バッファーのサイズを変更してから、 \fBgetgrouplist\fP()
72 をもう一度呼び出すことができる。
73 .SH バージョン
74 この関数は glibc 2.2.4 から存在する。
75 .SH 準拠
76 この関数は非標準である。ほとんどの BSD に存在する。
77 .SH バグ
78 バージョン 2.3.3 より前の glibc では、 この関数の実装にはバッファーオーバーフローのバグがあり、 \fIuser\fP が所属するグループ数が
79 \fI*ngroups\fP より多い場合であっても、 \fIuser\fP が所属するグループの全リストを配列 \fIgroups\fP に格納してしまう。
80 .SH 例
81 .PP
82 以下のプログラムは、一つ目のコマンドライン引き数で指定された名前のユーザー が所属するグループのリストを表示する。 二番目のコマンドライン引き数には、
83 \fBgetgrouplist\fP()  に渡す \fIngroups\fP の値を指定する。
84 以下のシェルのセッションはこのプログラムの使用例を示したものである。
85 .in +4n
86 .nf
87
88 $\fB ./a.out cecilia 0\fP
89 getgrouplist() returned \-1; ngroups = 3
90 $\fB ./a.out cecilia 3\fP
91 ngroups = 3
92 16 (dialout)
93 33 (video)
94 100 (users)
95 .fi
96 .in
97 .SS プログラムのソース
98 \&
99 .nf
100 #include <stdio.h>
101 #include <stdlib.h>
102 #include <grp.h>
103 #include <pwd.h>
104
105 int
106 main(int argc, char *argv[])
107 {
108     int j, ngroups;
109     gid_t *groups;
110     struct passwd *pw;
111     struct group *gr;
112
113     if (argc != 3) {
114         fprintf(stderr, "Usage: %s <user> <ngroups>\en", argv[0]);
115         exit(EXIT_FAILURE);
116     }
117
118     ngroups = atoi(argv[2]);
119
120     groups = malloc(ngroups * sizeof (gid_t));
121     if (groups == NULL) {
122         perror("malloc");
123         exit(EXIT_FAILURE);
124     }
125
126     /* Fetch passwd structure (contains first group ID for user) */
127
128     pw = getpwnam(argv[1]);
129     if (pw == NULL) {
130         perror("getpwnam");
131         exit(EXIT_SUCCESS);
132     }
133
134     /* Retrieve group list */
135
136     if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
137         fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\en",
138                 ngroups);
139         exit(EXIT_FAILURE);
140     }
141
142     /* Display list of retrieved groups, along with group names */
143
144     fprintf(stderr, "ngroups = %d\en", ngroups);
145     for (j = 0; j < ngroups; j++) {
146         printf("%d", groups[j]);
147         gr = getgrgid(groups[j]);
148         if (gr != NULL)
149             printf(" (%s)", gr\->gr_name);
150         printf("\en");
151     }
152
153     exit(EXIT_SUCCESS);
154 }
155 .fi
156 .SH 関連項目
157 \fBgetgroups\fP(2), \fBsetgroups\fP(2), \fBgetgrent\fP(3), \fBgroup_member\fP(3),
158 \fBgroup\fP(5), \fBpasswd\fP(5)
159 .SH この文書について
160 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
161 である。プロジェクトの説明とバグ報告に関する情報は
162 http://www.kernel.org/doc/man\-pages/ に書かれている。