OSDN Git Service

(split) LDP: Restore and add Copyrights for draft pages
[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 2009\-07\-03 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 .SH バージョン
73 この関数は glibc 2.2.4 から存在する。
74 .SH 準拠
75 この関数は非標準である。ほとんどの BSD に存在する。
76 .SH バグ
77 バージョン 2.3.3 より前の glibc では、 この関数の実装にはバッファオーバーフローのバグがあり、 \fIuser\fP が所属するグループ数が
78 \fI*ngroups\fP より多い場合であっても、 \fIuser\fP が所属するグループの全リストを配列 \fIgroups\fP に格納してしまう。
79 .SH 例
80 .PP
81 以下のプログラムは、一つ目のコマンドライン引き数で指定された名前のユーザ が所属するグループのリストを表示する。 二番目のコマンドライン引き数には、
82 \fBgetgrouplist\fP()  に渡す \fIngroups\fP の値を指定する。
83 以下のシェルのセッションはこのプログラムの使用例を示したものである。
84 .in +4n
85 .nf
86
87 $\fB ./a.out cecilia 0\fP
88 getgrouplist() returned \-1; ngroups = 3
89 $\fB ./a.out cecilia 3\fP
90 ngroups = 3
91 16 (dialout)
92 33 (video)
93 100 (users)
94 .fi
95 .in
96 .SS プログラムのソース
97 \&
98 .nf
99 #include <stdio.h>
100 #include <stdlib.h>
101 #include <grp.h>
102 #include <pwd.h>
103
104 int
105 main(int argc, char *argv[])
106 {
107     int j, ngroups;
108     gid_t *groups;
109     struct passwd *pw;
110     struct group *gr;
111
112     if (argc != 3) {
113         fprintf(stderr, "Usage: %s <user> <ngroups>\en", argv[0]);
114         exit(EXIT_FAILURE);
115     }
116
117     ngroups = atoi(argv[2]);
118
119     groups = malloc(ngroups * sizeof (gid_t));
120     if (groups == NULL) {
121         perror("malloc");
122         exit(EXIT_FAILURE);
123     }
124
125     /* Fetch passwd structure (contains first group ID for user) */
126
127     pw = getpwnam(argv[1]);
128     if (pw == NULL) {
129         perror("getpwnam");
130         exit(EXIT_SUCCESS);
131     }
132
133     /* Retrieve group list */
134
135     if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
136         fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\en",
137                 ngroups);
138         exit(EXIT_FAILURE);
139     }
140
141     /* Display list of retrieved groups, along with group names */
142
143     fprintf(stderr, "ngroups = %d\en", ngroups);
144     for (j = 0; j < ngroups; j++) {
145         printf("%d", groups[j]);
146         gr = getgrgid(groups[j]);
147         if (gr != NULL)
148             printf(" (%s)", gr\->gr_name);
149         printf("\en");
150     }
151
152     exit(EXIT_SUCCESS);
153 }
154 .fi
155 .SH 関連項目
156 \fBgetgroups\fP(2), \fBsetgroups\fP(2), \fBgetgrent\fP(3), \fBgroup\fP(5), \fBpasswd\fP(5)
157 .SH この文書について
158 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
159 である。プロジェクトの説明とバグ報告に関する情報は
160 http://www.kernel.org/doc/man\-pages/ に書かれている。