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>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
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().
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
43 .TH GETPWNAM 3 2009-03-30 "GNU" "Linux Programmer's Manual"
46 .\"O getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
47 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- パスワードファイルのエントリの取得
51 .B #include <sys/types.h>
54 .BI "struct passwd *getpwnam(const char *" name );
56 .BI "struct passwd *getpwuid(uid_t " uid );
58 .BI "int getpwnam_r(const char *" name ", struct passwd *" pwd ,
60 .BI " char *" buf ", size_t " buflen ", struct passwd **" result );
62 .BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd ,
64 .BI " char *" buf ", size_t " buflen ", struct passwd **" result );
68 .\"O Feature Test Macro Requirements for glibc (see
69 .\"O .BR feature_test_macros (7)):
71 .RB ( feature_test_macros (7)
79 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
80 _SVID_SOURCE || _POSIX_SOURCE
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 ,
92 .\"O that matches the username
97 にマッチするパスワード・データベースのエントリを
98 要素毎に分解し、各要素を格納した構造体へのポインタを返す
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
113 にマッチするパスワード・データベースのエントリを
114 要素毎に分解し、各要素を格納した構造体へのポインタを返す。
117 .\"O .BR getpwnam_r ()
119 .\"O .BR getpwuid_r ()
120 .\"O functions obtain the same information, but store the retrieved
122 .\"O structure in the space pointed to by
127 関数は (上記の関数と) 同じ情報を取得するが、
135 .\"O structure contains pointers to strings, and these strings
136 .\"O are stored in the buffer
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
154 エントリが見つからなかった場合やエラーが発生した場合には
158 .\"O The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
159 \fIpasswd\fP 構造体は、\fI<pwd.h>\fP で以下のように定義されている:
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; /* シェルプログラム */
182 .\"O The maximum needed size for
184 .\"O can be found using
186 .\"O with the argument
187 .\"O .BR _SC_GETPW_R_SIZE_MAX .
192 .B _SC_GETPW_R_SIZE_MAX
194 .\"O .SH "RETURN VALUE"
200 .\"O functions return a pointer to a
202 .\"O structure, or NULL if the matching entry is not found or
203 .\"O an error occurs.
210 一致するエントリが見つからなかった場合や、エラーが発生した場合は NULL を返す。
211 .\"O If an error occurs,
213 .\"O is set appropriately.
214 .\"O If one wants to check
216 .\"O after the call, it should be set to zero before the call.
223 呼び出しの前に (この値を) 0 に設定しておくべきである。
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 (),
230 .\"O .BR getpwuid ().
231 .\"O (Do not pass the returned pointer to
243 .\"O .BR getpwnam_r ()
245 .\"O .BR getpwuid_r ()
246 .\"O return zero, and set
250 .\"O If no matching password record was found,
251 .\"O these functions return 0 and store NULL in
253 .\"O In case of error, an error number is returned, and NULL is stored in
264 マッチするパスワード・エントリが見つからなかった場合には、
274 .\"O .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
275 .BR 0 " または " ENOENT " または " ESRCH " または " EBADF " または " EPERM " または ... "
288 .\"O A signal was caught.
296 .\"O The maximum number
297 .\"O .RB ( OPEN_MAX )
298 .\"O of files was open already in the calling process.
300 既に最大数 (OPEN_MAX) のファイルがオープンされている。
303 .\"O The maximum number of files was open already in the system.
304 システム上で既に最大数のファイルがオープンされている。
308 .\"O Insufficient memory to allocate
311 .\"O .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
315 .\" この構造体は静的であり、0 回または 1 回割り当てられる。
316 .\" メモリリークは無い。(libc45)
319 .\"O Insufficient buffer space supplied.
325 .\"O local password database file
326 ローカルのパスワード・データベースファイル
327 .\"O .SH "CONFORMING TO"
329 SVr4, 4.3BSD, POSIX.1-2001.
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
336 .\"O might have in this situation.
337 .\"O But that makes it impossible to recognize
339 .\"O One might argue that according to POSIX
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 この標準は「(エントリが) 見つからないこと」をエラーとしていないので、
349 そのため、エラーを認識することは不可能である。
350 POSIX に準拠して、エントリが見つからない場合は
352 を変更しないようにすべきである、と主張する人もいるかもしれない。
353 様々な UNIX 系のシステムで試してみると、そのような場合には
354 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM といった様々な値が返される。
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
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 を返す。
375 .\"O field contains the name of the initial working directory of the user.
378 には、ユーザの作業ディレクトリ名の初期値が格納される。
379 .\"O Login programs use the value of this field to initialize the
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
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
393 ログインプロセスは、このフィールドの値を使って、
397 アプリケーションが、ユーザのホーム・ディレクトリを決定する場合には、
398 .RI ( getpwuid(getuid())\->pw_dir
402 なぜなら、このようにすることで、ユーザがログイン・セッション中で
403 「ホーム・ディレクトリ」の意味を変更できるようになるからである。
404 別のユーザのホーム・ディレクトリ (の初期値) を知るには
405 .I getpwnam("username")\->pw_dir
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.
415 の使用例を示したもので、コマンドライン引き数で渡されたユーザ名に対する
416 完全なユーザ名とユーザ ID を探すものである。
426 main(int argc, char *argv[])
429 struct passwd *result;
435 fprintf(stderr, "Usage: %s username\\n", argv[0]);
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; /* 十分大きな値にすべき */
445 buf = malloc(bufsize);
451 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
452 if (result == NULL) {
454 printf("Not found\\n");
457 perror("getpwnam_r");
462 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);