OSDN Git Service

e4c957b8f3c12a383917627d550b35e9cda47950
[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 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein.  The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\"
25 .\" References consulted:
26 .\"     Linux libc source code
27 .\"     Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
28 .\"     386BSD man pages
29 .\"
30 .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
31 .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
32 .\" Modified 2003-11-15 by aeb
33 .\" 2008-11-07, mtk, Added an example program for getpwnam_r().
34 .\"
35 .\" Japanese Version Copyright (c) 1997 HIROFUMI Nishizuka
36 .\"     all rights reserved.
37 .\" Translated 1997-12-18, HIROFUMI Nishizuka <nishi@rpts.cl.nec.co.jp>
38 .\" Updated & Modified 2004-01-06, Yuichi SATO <ysato444@yahoo.co.jp>
39 .\" Updated & Modified 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
40 .\" Updated & Modified 2005-10-08, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
41 .\" Updated 2008-12-24, Akihiro MOTOKI, LDP v3.14
42 .\"
43 .TH GETPWNAM 3  2009-03-30 "GNU" "Linux Programmer's Manual"
44 .\"O .SH NAME
45 .SH 名前
46 .\"O getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
47 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- パスワードファイルのエントリの取得
48 .\"O .SH SYNOPSIS
49 .SH 書式
50 .nf
51 .B #include <sys/types.h>
52 .B #include <pwd.h>
53 .sp
54 .BI "struct passwd *getpwnam(const char *" name );
55 .sp
56 .BI "struct passwd *getpwuid(uid_t " uid );
57 .sp
58 .BI "int getpwnam_r(const char *" name ", struct passwd *" pwd ,
59 .br
60 .BI "            char *" buf ", size_t " buflen ", struct passwd **" result );
61 .sp
62 .BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd ,
63 .br
64 .BI "            char *" buf ", size_t " buflen ", struct passwd **" result );
65 .fi
66 .sp
67 .in -4n
68 .\"O Feature Test Macro Requirements for glibc (see
69 .\"O .BR feature_test_macros (7)):
70 glibc 向けの機能検査マクロの要件
71 .RB ( feature_test_macros (7)
72 参照):
73 .in
74 .sp
75 .ad l
76 .BR getpwnam_r (),
77 .BR getpwuid_r ():
78 .RS 4
79 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
80 _SVID_SOURCE || _POSIX_SOURCE
81 .RE
82 .ad b
83 .\"O .SH DESCRIPTION
84 .SH 説明
85 .\"O The
86 .\"O .BR getpwnam ()
87 .\"O function returns a pointer to a structure containing
88 .\"O the broken-out fields of the record in the password database
89 .\"O (e.g., the local password file
90 .\"O .IR /etc/passwd ,
91 .\"O NIS, and LDAP)
92 .\"O that matches the username
93 .\"O .IR name .
94 .BR getpwnam ()
95 関数は、ユーザ名
96 .I name
97 にマッチするパスワード・データベースのエントリを
98 要素毎に分解し、各要素を格納した構造体へのポインタを返す
99 (パスワード・データベースの例:
100 ローカルのパスワードファイル
101 .IR /etc/passwd ,
102 NIS, LDAP)。
103 .PP
104 .\"O The
105 .\"O .BR getpwuid ()
106 .\"O function returns a pointer to a structure containing
107 .\"O the broken-out fields of the record in the password database
108 .\"O that matches the user ID
109 .\"O .IR uid .
110 .BR getpwuid ()
111 関数は、ユーザ ID
112 .I uid
113 にマッチするパスワード・データベースのエントリを
114 要素毎に分解し、各要素を格納した構造体へのポインタを返す。
115 .PP
116 .\"O The
117 .\"O .BR getpwnam_r ()
118 .\"O and
119 .\"O .BR getpwuid_r ()
120 .\"O functions obtain the same information, but store the retrieved
121 .\"O .I passwd
122 .\"O structure in the space pointed to by
123 .\"O .IR pwd .
124 .BR getpwnam_r ()
125
126 .BR getpwuid_r ()
127 関数は (上記の関数と) 同じ情報を取得するが、
128 取得した
129 .I passwd
130 構造体を
131 .I pwd
132 が指す領域に格納する。
133 .\"O This
134 .\"O .I passwd
135 .\"O structure contains pointers to strings, and these strings
136 .\"O are stored in the buffer
137 .\"O .I buf
138 .\"O of size
139 .\"O .IR buflen .
140 この
141 .I passwd
142 構造体には文字列へのポインタが含まれ、
143 これらの文字列はサイズ
144 .I buflen
145 のバッファ
146 .I buf
147 に格納される。
148 .\"O A pointer to the result (in case of success) or NULL (in case no entry
149 .\"O was found or an error occurred) is stored in
150 .\"O .IR *result .
151 成功した場合
152 .I *result
153 には結果へのポインタが格納される。
154 エントリが見つからなかった場合やエラーが発生した場合には
155 .I *result
156 には NULL が入る。
157 .PP
158 .\"O The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
159 \fIpasswd\fP 構造体は、\fI<pwd.h>\fP で以下のように定義されている:
160 .sp
161 .in +4n
162 .nf
163 struct passwd {
164 .\"O     char   *pw_name;       /* username */
165 .\"O     char   *pw_passwd;     /* user password */
166 .\"O     uid_t   pw_uid;        /* user ID */
167 .\"O     gid_t   pw_gid;        /* group ID */
168 .\"O     char   *pw_gecos;      /* real name */
169 .\"O     char   *pw_dir;        /* home directory */
170 .\"O     char   *pw_shell;      /* shell program */
171     char   *pw_name;       /* ユーザ名 */
172     char   *pw_passwd;     /* ユーザのパスワード */
173     uid_t   pw_uid;        /* ユーザ ID */
174     gid_t   pw_gid;        /* グループ ID */
175     char   *pw_gecos;      /* 実名 */
176     char   *pw_dir;        /* ホームディレクトリ */
177     char   *pw_shell;      /* シェルプログラム */
178 };
179 .fi
180 .in
181 .PP
182 .\"O The maximum needed size for
183 .\"O .I buf
184 .\"O can be found using
185 .\"O .BR sysconf (3)
186 .\"O with the argument
187 .\"O .BR _SC_GETPW_R_SIZE_MAX .
188 .I buf
189 に最大必要なサイズは、
190 .BR sysconf (3)
191 に引き数
192 .B _SC_GETPW_R_SIZE_MAX
193 を指定して実行することで分かる。
194 .\"O .SH "RETURN VALUE"
195 .SH 返り値
196 .\"O The
197 .\"O .BR getpwnam ()
198 .\"O and
199 .\"O .BR getpwuid ()
200 .\"O functions return a pointer to a
201 .\"O .I passwd
202 .\"O structure, or NULL if the matching entry is not found or
203 .\"O an error occurs.
204 .BR getpwnam ()
205
206 .BR getpwuid ()
207 関数は、
208 .I passwd
209 構造体へのポインタを返す。
210 一致するエントリが見つからなかった場合や、エラーが発生した場合は NULL を返す。
211 .\"O If an error occurs,
212 .\"O .I errno
213 .\"O is set appropriately.
214 .\"O If one wants to check
215 .\"O .I errno
216 .\"O after the call, it should be set to zero before the call.
217 エラーが起こった場合、
218 .I errno
219 が適切に設定される。
220 呼び出しの後で
221 .I errno
222 をチェックしたい場合は、
223 呼び出しの前に (この値を) 0 に設定しておくべきである。
224 .LP
225 .\"O The return value may point to a static area, and may be overwritten
226 .\"O by subsequent calls to
227 .\"O .BR getpwent (3),
228 .\"O .BR getpwnam (),
229 .\"O or
230 .\"O .BR getpwuid ().
231 .\"O (Do not pass the returned pointer to
232 .\"O .BR free (3).)
233 返り値は静的な領域を指しており、その後の
234 .BR getpwent (3),
235 .BR getpwnam (),
236 .BR getpwuid ()
237 の呼び出しで上書きされるかもしれない。
238 (返されたポインタを
239 .BR free (3)
240 に渡さないこと。)
241 .LP
242 .\"O On success,
243 .\"O .BR getpwnam_r ()
244 .\"O and
245 .\"O .BR getpwuid_r ()
246 .\"O return zero, and set
247 .\"O .IR *result
248 .\"O to
249 .\"O .IR pwd .
250 .\"O If no matching password record was found,
251 .\"O these functions return 0 and store NULL in
252 .\"O .IR *result .
253 .\"O In case of error, an error number is returned, and NULL is stored in
254 .\"O .IR *result .
255 成功すると、
256 .BR getpwnam_r ()
257
258 .BR getpwuid_r ()
259 は 0 を返し、
260 .I *result
261
262 .I pwd
263 を設定する。
264 マッチするパスワード・エントリが見つからなかった場合には、
265 0 を返し、
266 .I *result
267 に NULL を設定する。
268 エラーの場合、エラー番号を返し、
269 .I *result
270 に NULL を設定する。
271 .\"O .SH ERRORS
272 .SH エラー
273 .TP
274 .\"O .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
275 .BR 0 " または " ENOENT " または " ESRCH " または " EBADF " または " EPERM " または ... "
276 .\"O The given
277 .\"O .I name
278 .\"O or
279 .\"O .I uid
280 .\"O was not found.
281 指定された
282 .I name
283 または
284 .I uid
285 が見つからなかった。
286 .TP
287 .B EINTR
288 .\"O A signal was caught.
289 シグナルがキャッチされた。
290 .TP
291 .B EIO
292 .\"O I/O error.
293 I/O エラー。
294 .TP
295 .B EMFILE
296 .\"O The maximum number
297 .\"O .RB ( OPEN_MAX )
298 .\"O of files was open already in the calling process.
299 呼び出したプロセスにおいて、
300 既に最大数 (OPEN_MAX) のファイルがオープンされている。
301 .TP
302 .B ENFILE
303 .\"O The maximum number of files was open already in the system.
304 システム上で既に最大数のファイルがオープンされている。
305 .TP
306 .B ENOMEM
307 .\" not in POSIX
308 .\"O Insufficient memory to allocate
309 .\"O .I passwd
310 .\"O structure.
311 .\"O .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
312 .\" POSIX にはない。
313 .I passwd
314 構造体を割り当てるためのメモリが不十分。
315 .\" この構造体は静的であり、0 回または 1 回割り当てられる。
316 .\" メモリリークは無い。(libc45)
317 .TP
318 .B ERANGE
319 .\"O Insufficient buffer space supplied.
320 与えられたバッファ空間が不十分である。
321 .\"O .SH FILES
322 .SH ファイル
323 .TP
324 .I /etc/passwd
325 .\"O local password database file
326 ローカルのパスワード・データベースファイル
327 .\"O .SH "CONFORMING TO"
328 .SH 準拠
329 SVr4, 4.3BSD, POSIX.1-2001.
330 .\"O .SH NOTES
331 .SH 注意
332 .\"O The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
333 上記の「返り値」以下の記述は POSIX.1-2001 に拠る。
334 .\"O It does not call "not found" an error, and hence does not specify what value
335 .\"O .I errno
336 .\"O might have in this situation.
337 .\"O But that makes it impossible to recognize
338 .\"O errors.
339 .\"O One might argue that according to POSIX
340 .\"O .I errno
341 .\"O should be left unchanged if an entry is not found.
342 .\"O Experiments on various
343 .\"O UNIX-like systems show that lots of different values occur in this
344 .\"O situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
345 この標準は「(エントリが) 見つからないこと」をエラーとしていないので、
346 そのような場合に
347 .I errno
348 がどのような値になるかを定めていない。
349 そのため、エラーを認識することは不可能である。
350 POSIX に準拠して、エントリが見つからない場合は
351 .I errno
352 を変更しないようにすべきである、と主張する人もいるかもしれない。
353 様々な UNIX 系のシステムで試してみると、そのような場合には
354 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM といった様々な値が返される。
355 他の値が返されるかもしれない。
356 .\"O .\" more precisely:
357 .\"O .\" AIX 5.1 - gives ESRCH
358 .\"O .\" OSF1 4.0g - gives EWOULDBLOCK
359 .\"O .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
360 .\"O .\" glibc since version 2.7 - give 0
361 .\"O .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
362 .\"O .\" SunOS 5.8 - gives EBADF
363 .\"O .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
364 .\" より正確には:
365 .\" AIX 5.1 は ESRCH を返す。
366 .\" OSF1 4.0g は EWOULDBLOCK を返す。
367 .\" libc, glibc (バージョン 2.6 まで), Irix 6.5 は ENOENT を返す。
368 .\" glibc (バージョン 2.7 以降) は 0 を返す。
369 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 は EPERM を返す。
370 .\" SunOS 5.8 は EBADF を返す。
371 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 は 0 を返す。
372
373 .\"O The
374 .\"O .I pw_dir
375 .\"O field contains the name of the initial working directory of the user.
376 フィールド
377 .I pw_dir
378 には、ユーザの作業ディレクトリ名の初期値が格納される。
379 .\"O Login programs use the value of this field to initialize the
380 .\"O .B HOME
381 .\"O environment variable for the login shell.
382 .\"O An application that wants to determine its user's home directory
383 .\"O should inspect the value of
384 .\"O .B HOME
385 .\"O (rather than the value
386 .\"O .IR getpwuid(getuid())\->pw_dir )
387 .\"O since this allows the user to modify their notion of
388 .\"O "the home directory" during a login session.
389 .\"O To determine the (initial) home directory of another user,
390 .\"O it is necessary to use
391 .\"O .I getpwnam("username")\->pw_dir
392 .\"O or similar.
393 ログインプロセスは、このフィールドの値を使って、
394 ログインシェルの
395 .B HOME
396 環境変数を初期化する。
397 アプリケーションが、ユーザのホーム・ディレクトリを決定する場合には、
398 .RI ( getpwuid(getuid())\->pw_dir
399 の値ではなく)
400 .B HOME
401 の値を検査するようにすべきである。
402 なぜなら、このようにすることで、ユーザがログイン・セッション中で
403 「ホーム・ディレクトリ」の意味を変更できるようになるからである。
404 別のユーザのホーム・ディレクトリ (の初期値) を知るには
405 .I getpwnam("username")\->pw_dir
406 か同様の方法を使う必要がある。
407 .\"O .SH EXAMPLE
408 .SH 例
409 .\"O The program below demonstrates the use of
410 .\"O .BR getpwnam_r ()
411 .\"O to find the full username and user ID for the username
412 .\"O supplied as a command-line argument.
413 以下のプログラムは
414 .BR getpwnam_r ()
415 の使用例を示したもので、コマンドライン引き数で渡されたユーザ名に対する
416 完全なユーザ名とユーザ ID を探すものである。
417
418 .nf
419 #include <pwd.h>
420 #include <stdio.h>
421 #include <stdlib.h>
422 #include <unistd.h>
423 #include <errno.h>
424
425 int
426 main(int argc, char *argv[])
427 {
428     struct passwd pwd;
429     struct passwd *result;
430     char *buf;
431     size_t bufsize;
432     int s;
433
434     if (argc != 2) {
435         fprintf(stderr, "Usage: %s username\\n", argv[0]);
436         exit(EXIT_FAILURE);
437     }
438
439     bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
440 .\"O     if (bufsize == \-1)          /* Value was indeterminate */
441 .\"O         bufsize = 16384;        /* Should be more than enough */
442     if (bufsize == \-1)          /* 値を決定できなかった */
443         bufsize = 16384;        /* 十分大きな値にすべき */
444
445     buf = malloc(bufsize);
446     if (buf == NULL) {
447         perror("malloc");
448         exit(EXIT_FAILURE);
449     }
450
451     s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
452     if (result == NULL) {
453         if (s == 0)
454             printf("Not found\\n");
455         else {
456             errno = s;
457             perror("getpwnam_r");
458         }
459         exit(EXIT_FAILURE);
460     }
461
462     printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);
463     exit(EXIT_SUCCESS);
464 }
465 .fi
466 .\"O .SH "SEE ALSO"
467 .SH 関連項目
468 .BR endpwent (3),
469 .BR fgetpwent (3),
470 .BR getgrnam (3),
471 .BR getpw (3),
472 .BR getpwent (3),
473 .BR getspnam (3),
474 .BR putpwent (3),
475 .BR setpwent (3),
476 .BR passwd (5)