OSDN Git Service

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