OSDN Git Service

(split) LDP: Addresses fuzzy strings in 3.53
[linuxjm/LDP_man-pages.git] / draft / man3 / getpwnam.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
3 .\"     <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" References consulted:
28 .\"     Linux libc source code
29 .\"     Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
30 .\"     386BSD man pages
31 .\"
32 .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
33 .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
34 .\" Modified 2003-11-15 by aeb
35 .\" 2008-11-07, mtk, Added an example program for getpwnam_r().
36 .\"
37 .\"*******************************************************************
38 .\"
39 .\" This file was generated with po4a. Translate the source file.
40 .\"
41 .\"*******************************************************************
42 .TH GETPWNAM 3 2013\-07\-22 GNU "Linux Programmer's Manual"
43 .SH 名前
44 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- パスワードファイルのエントリの取得
45 .SH 書式
46 .nf
47 \fB#include <sys/types.h>\fP
48 \fB#include <pwd.h>\fP
49 .sp
50 \fBstruct passwd *getpwnam(const char *\fP\fIname\fP\fB);\fP
51 .sp
52 \fBstruct passwd *getpwuid(uid_t \fP\fIuid\fP\fB);\fP
53 .sp
54 \fBint getpwnam_r(const char *\fP\fIname\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
55 .br
56 \fB            char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
57 .sp
58 \fBint getpwuid_r(uid_t \fP\fIuid\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
59 .br
60 \fB            char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
61 .fi
62 .sp
63 .in -4n
64 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
65 .in
66 .sp
67 .ad l
68 \fBgetpwnam_r\fP(), \fBgetpwuid_r\fP():
69 .RS 4
70 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
71 || _POSIX_SOURCE
72 .RE
73 .ad b
74 .SH 説明
75 \fBgetpwnam\fP()  関数は、ユーザ名 \fIname\fP にマッチするパスワード・データベースのエントリを
76 要素毎に分解し、各要素を格納した構造体へのポインタを返す (パスワード・データベースの例: ローカルのパスワードファイル \fI/etc/passwd\fP,
77 NIS, LDAP)。
78 .PP
79 \fBgetpwuid\fP()  関数は、ユーザ ID \fIuid\fP にマッチするパスワード・データベースのエントリを
80 要素毎に分解し、各要素を格納した構造体へのポインタを返す。
81 .PP
82 \fIpasswd\fP 構造体は、\fI<pwd.h>\fP で以下のように定義されている:
83 .sp
84 .in +4n
85 .nf
86 struct passwd {
87     char   *pw_name;       /* ユーザ名 */
88     char   *pw_passwd;     /* ユーザのパスワード */
89     uid_t   pw_uid;        /* ユーザ ID */
90     gid_t   pw_gid;        /* グループ ID */
91     char   *pw_gecos;      /* ユーザ情報 */
92     char   *pw_dir;        /* ホームディレクトリ */
93     char   *pw_shell;      /* シェルプログラム */
94 };
95 .fi
96 .in
97 .PP
98 これらのフィールドの詳しい情報については \fBpasswd\fP(5) を参照のこと。
99 .PP
100 \fBgetpwnam_r\fP() と \fBgetpwuid_r\fP() 関数は、それぞれ\fBgetpwnam\fP() と
101 \fBgetpwuid\fP() と同じ情報を取得するが、取得した \fIpasswd\fP 構造体を
102 \fIpwd\fP が指す領域に格納する。\fIpasswd\fP 構造体のメンバーが指す文字列は、
103 サイズ \fIbuflen\fP のバッファ \fIbuf\fP に格納される。成功した場合
104 \fI*result\fP には結果へのポインタが格納される。エントリが見つからなかった
105 場合やエラーが発生した場合には \fI*result\fP には NULL が入る。
106 .PP
107 呼び出し
108
109     sysconf(_SC_GETPW_R_SIZE_MAX)
110
111 は、 \fIerrno\fP を変更せずに \-1 を返すか、 \fIbuf\fP の初期サイズの推奨値を
112 返す。(このサイズが小さすぎる場合、呼び出しは \fBERANGE\fP で失敗し、この
113 場合には呼び出し側はバッファを大きくしてから再度呼び出すことができる。)
114 .SH 返り値
115 \fBgetpwnam\fP()  と \fBgetpwuid\fP()  関数は、 \fIpasswd\fP 構造体へのポインタを返す。
116 一致するエントリが見つからなかった場合や、エラーが発生した場合は NULL を返す。 エラーが起こった場合、 \fIerrno\fP が適切に設定される。
117 呼び出しの後で \fIerrno\fP をチェックしたい場合は、 呼び出しの前に (この値を) 0 に設定しておくべきである。
118 .LP
119 返り値は静的な領域を指しており、その後の \fBgetpwent\fP(3), \fBgetpwnam\fP(), \fBgetpwuid\fP()
120 の呼び出しで上書きされるかもしれない。 (返されたポインタを \fBfree\fP(3)  に渡さないこと。)
121 .LP
122 成功すると、 \fBgetpwnam_r\fP()  と \fBgetpwuid_r\fP()  は 0 を返し、 \fI*result\fP に \fIpwd\fP
123 を設定する。 マッチするパスワード・エントリが見つからなかった場合には、 0 を返し、 \fI*result\fP に NULL を設定する。
124 エラーの場合、エラー番号を返し、 \fI*result\fP に NULL を設定する。
125 .SH エラー
126 .TP 
127 \fB0\fP または \fBENOENT\fP または \fBESRCH\fP または \fBEBADF\fP または \fBEPERM\fP または ... 
128 指定された \fIname\fP または \fIuid\fP が見つからなかった。
129 .TP 
130 \fBEINTR\fP
131 シグナルが捕捉された。
132 .TP 
133 \fBEIO\fP
134 I/O エラー。
135 .TP 
136 \fBEMFILE\fP
137 呼び出し元プロセスがオープンしているファイル数が すでに上限 (\fBOPEN_MAX\fP)  であった。
138 .TP 
139 \fBENFILE\fP
140 システムでオープンされているファイル数がすでに上限であった。
141 .TP 
142 \fBENOMEM\fP
143 .\" not in POSIX
144 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
145 \fIpasswd\fP 構造体に割り当てるメモリが十分なかった。
146 .TP 
147 \fBERANGE\fP
148 与えられたバッファ空間が不十分である。
149 .SH ファイル
150 .TP 
151 \fI/etc/passwd\fP
152 ローカルのパスワード・データベースファイル
153 .SH 属性
154 .SS "マルチスレッディング (pthreads(7) 参照)"
155 関数 \fBgetpwnam\fP() と \fBgetpwuid\fP() はスレッドセーフではない。
156 .LP
157 関数 \fBgetpwnam_r\fP() と \fBgetpwuid_r\fP() はスレッドセーフである。
158 .SH 準拠
159 SVr4, 4.3BSD, POSIX.1\-2001.  \fIpw_gecos\fP フィールドは POSIX では規定されていないが、
160 ほとんどの実装に存在する。
161 .SH 注意
162 .\" more precisely:
163 .\" AIX 5.1 - gives ESRCH
164 .\" OSF1 4.0g - gives EWOULDBLOCK
165 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
166 .\" glibc since version 2.7 - give 0
167 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
168 .\" SunOS 5.8 - gives EBADF
169 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
170 上記の「返り値」以下の記述は POSIX.1\-2001 に拠る。 この標準は「(エントリが) 見つからないこと」をエラーとしていないので、
171 そのような場合に \fIerrno\fP がどのような値になるかを定めていない。 そのため、エラーを認識することは不可能である。 POSIX
172 に準拠して、エントリが見つからない場合は \fIerrno\fP を変更しないようにすべきである、と主張する人もいるかもしれない。 様々な UNIX
173 系のシステムで試してみると、そのような場合には 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM
174 といった様々な値が返される。 他の値が返されるかもしれない。
175
176 フィールド \fIpw_dir\fP には、ユーザの作業ディレクトリ名の初期値が格納される。 ログインプロセスは、このフィールドの値を使って、
177 ログインシェルの \fBHOME\fP 環境変数を初期化する。 アプリケーションが、ユーザのホーム・ディレクトリを決定する場合には、
178 (\fIgetpwuid(getuid())\->pw_dir\fP の値ではなく)  \fBHOME\fP の値を検査するようにすべきである。
179 なぜなら、このようにすることで、ユーザがログイン・セッション中で 「ホーム・ディレクトリ」の意味を変更できるようになるからである。
180 別のユーザのホーム・ディレクトリ (の初期値) を知るには \fIgetpwnam("username")\->pw_dir\fP
181 か同様の方法を使う必要がある。
182 .SH 例
183 以下のプログラムは \fBgetpwnam_r\fP()  の使用例を示したもので、コマンドライン引き数で渡されたユーザ名に対する 完全なユーザ名とユーザ
184 ID を探すものである。
185
186 .nf
187 #include <pwd.h>
188 #include <stdio.h>
189 #include <stdlib.h>
190 #include <unistd.h>
191 #include <errno.h>
192
193 int
194 main(int argc, char *argv[])
195 {
196     struct passwd pwd;
197     struct passwd *result;
198     char *buf;
199     size_t bufsize;
200     int s;
201
202     if (argc != 2) {
203         fprintf(stderr, "Usage: %s username\en", argv[0]);
204         exit(EXIT_FAILURE);
205     }
206
207     bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
208     if (bufsize == \-1)          /* 値を決定できなかった */
209         bufsize = 16384;        /* 十分大きな値にすべき */
210
211     buf = malloc(bufsize);
212     if (buf == NULL) {
213         perror("malloc");
214         exit(EXIT_FAILURE);
215     }
216
217     s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
218     if (result == NULL) {
219         if (s == 0)
220             printf("Not found\en");
221         else {
222             errno = s;
223             perror("getpwnam_r");
224         }
225         exit(EXIT_FAILURE);
226     }
227
228     printf("Name: %s; UID: %ld\en", pwd.pw_gecos, (long) pwd.pw_uid);
229     exit(EXIT_SUCCESS);
230 }
231 .fi
232 .SH 関連項目
233 \fBendpwent\fP(3), \fBfgetpwent\fP(3), \fBgetgrnam\fP(3), \fBgetpw\fP(3),
234 \fBgetpwent\fP(3), \fBgetspnam\fP(3), \fBputpwent\fP(3), \fBsetpwent\fP(3), \fBpasswd\fP(5)
235 .SH この文書について
236 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
237 である。プロジェクトの説明とバグ報告に関する情報は
238 http://www.kernel.org/doc/man\-pages/ に書かれている。