.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk .\" .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" References consulted: .\" Linux libc source code .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu) .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de) .\" Modified 2003-11-15 by aeb .\" 2008-11-07, mtk, Added an example program for getpwnam_r(). .\" .\" Japanese Version Copyright (c) 1997 HIROFUMI Nishizuka .\" all rights reserved. .\" Translated 1997-12-18, HIROFUMI Nishizuka .\" Updated & Modified 2004-01-06, Yuichi SATO .\" Updated & Modified 2005-09-06, Akihiro MOTOKI .\" Updated & Modified 2005-10-08, Akihiro MOTOKI .\" Updated 2008-12-24, Akihiro MOTOKI, LDP v3.14 .\" .TH GETPWNAM 3 2009-03-30 "GNU" "Linux Programmer's Manual" .SH 名前 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- パスワードファイルのエントリの取得 .SH 書式 .nf .B #include .B #include .sp .BI "struct passwd *getpwnam(const char *" name ); .sp .BI "struct passwd *getpwuid(uid_t " uid ); .sp .BI "int getpwnam_r(const char *" name ", struct passwd *" pwd , .br .BI " char *" buf ", size_t " buflen ", struct passwd **" result ); .sp .BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd , .br .BI " char *" buf ", size_t " buflen ", struct passwd **" result ); .fi .sp .in -4n glibc 向けの機能検査マクロの要件 .RB ( feature_test_macros (7) 参照): .in .sp .ad l .BR getpwnam_r (), .BR getpwuid_r (): .RS 4 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE .RE .ad b .SH 説明 .BR getpwnam () 関数は、ユーザ名 .I name にマッチするパスワード・データベースのエントリを 要素毎に分解し、各要素を格納した構造体へのポインタを返す (パスワード・データベースの例: ローカルのパスワードファイル .IR /etc/passwd , NIS, LDAP)。 .PP .BR getpwuid () 関数は、ユーザ ID .I uid にマッチするパスワード・データベースのエントリを 要素毎に分解し、各要素を格納した構造体へのポインタを返す。 .PP .BR getpwnam_r () と .BR getpwuid_r () 関数は (上記の関数と) 同じ情報を取得するが、 取得した .I passwd 構造体を .I pwd が指す領域に格納する。 この .I passwd 構造体には文字列へのポインタが含まれ、 これらの文字列はサイズ .I buflen のバッファ .I buf に格納される。 成功した場合 .I *result には結果へのポインタが格納される。 エントリが見つからなかった場合やエラーが発生した場合には .I *result には NULL が入る。 .PP \fIpasswd\fP 構造体は、\fI\fP で以下のように定義されている: .sp .in +4n .nf struct passwd { char *pw_name; /* ユーザ名 */ char *pw_passwd; /* ユーザのパスワード */ uid_t pw_uid; /* ユーザ ID */ gid_t pw_gid; /* グループ ID */ char *pw_gecos; /* 実名 */ char *pw_dir; /* ホームディレクトリ */ char *pw_shell; /* シェルプログラム */ }; .fi .in .PP .I buf に最大必要なサイズは、 .BR sysconf (3) に引き数 .B _SC_GETPW_R_SIZE_MAX を指定して実行することで分かる。 .SH 返り値 .BR getpwnam () と .BR getpwuid () 関数は、 .I passwd 構造体へのポインタを返す。 一致するエントリが見つからなかった場合や、エラーが発生した場合は NULL を返す。 エラーが起こった場合、 .I errno が適切に設定される。 呼び出しの後で .I errno をチェックしたい場合は、 呼び出しの前に (この値を) 0 に設定しておくべきである。 .LP 返り値は静的な領域を指しており、その後の .BR getpwent (3), .BR getpwnam (), .BR getpwuid () の呼び出しで上書きされるかもしれない。 (返されたポインタを .BR free (3) に渡さないこと。) .LP 成功すると、 .BR getpwnam_r () と .BR getpwuid_r () は 0 を返し、 .I *result に .I pwd を設定する。 マッチするパスワード・エントリが見つからなかった場合には、 0 を返し、 .I *result に NULL を設定する。 エラーの場合、エラー番号を返し、 .I *result に NULL を設定する。 .SH エラー .TP .BR 0 " または " ENOENT " または " ESRCH " または " EBADF " または " EPERM " または ... " 指定された .I name または .I uid が見つからなかった。 .TP .B EINTR シグナルがキャッチされた。 .TP .B EIO I/O エラー。 .TP .B EMFILE 呼び出したプロセスにおいて、 既に最大数 (OPEN_MAX) のファイルがオープンされている。 .TP .B ENFILE システム上で既に最大数のファイルがオープンされている。 .TP .B ENOMEM .\" not in POSIX .\" POSIX にはない。 .I passwd 構造体を割り当てるためのメモリが不十分。 .\" この構造体は静的であり、0 回または 1 回割り当てられる。 .\" メモリリークは無い。(libc45) .TP .B ERANGE 与えられたバッファ空間が不十分である。 .SH ファイル .TP .I /etc/passwd ローカルのパスワード・データベースファイル .SH 準拠 SVr4, 4.3BSD, POSIX.1-2001. .SH 注意 上記の「返り値」以下の記述は POSIX.1-2001 に拠る。 この標準は「(エントリが) 見つからないこと」をエラーとしていないので、 そのような場合に .I errno がどのような値になるかを定めていない。 そのため、エラーを認識することは不可能である。 POSIX に準拠して、エントリが見つからない場合は .I errno を変更しないようにすべきである、と主張する人もいるかもしれない。 様々な UNIX 系のシステムで試してみると、そのような場合には 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM といった様々な値が返される。 他の値が返されるかもしれない。 .\" より正確には: .\" AIX 5.1 は ESRCH を返す。 .\" OSF1 4.0g は EWOULDBLOCK を返す。 .\" libc, glibc (バージョン 2.6 まで), Irix 6.5 は ENOENT を返す。 .\" glibc (バージョン 2.7 以降) は 0 を返す。 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 は EPERM を返す。 .\" SunOS 5.8 は EBADF を返す。 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 は 0 を返す。 フィールド .I pw_dir には、ユーザの作業ディレクトリ名の初期値が格納される。 ログインプロセスは、このフィールドの値を使って、 ログインシェルの .B HOME 環境変数を初期化する。 アプリケーションが、ユーザのホーム・ディレクトリを決定する場合には、 .RI ( getpwuid(getuid())\->pw_dir の値ではなく) .B HOME の値を検査するようにすべきである。 なぜなら、このようにすることで、ユーザがログイン・セッション中で 「ホーム・ディレクトリ」の意味を変更できるようになるからである。 別のユーザのホーム・ディレクトリ (の初期値) を知るには .I getpwnam("username")\->pw_dir か同様の方法を使う必要がある。 .SH 例 以下のプログラムは .BR getpwnam_r () の使用例を示したもので、コマンドライン引き数で渡されたユーザ名に対する 完全なユーザ名とユーザ ID を探すものである。 .nf #include #include #include #include #include int main(int argc, char *argv[]) { struct passwd pwd; struct passwd *result; char *buf; size_t bufsize; int s; if (argc != 2) { fprintf(stderr, "Usage: %s username\\n", argv[0]); exit(EXIT_FAILURE); } bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); if (bufsize == \-1) /* 値を決定できなかった */ bufsize = 16384; /* 十分大きな値にすべき */ buf = malloc(bufsize); if (buf == NULL) { perror("malloc"); exit(EXIT_FAILURE); } s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result); if (result == NULL) { if (s == 0) printf("Not found\\n"); else { errno = s; perror("getpwnam_r"); } exit(EXIT_FAILURE); } printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid); exit(EXIT_SUCCESS); } .fi .SH 関連項目 .BR endpwent (3), .BR fgetpwent (3), .BR getgrnam (3), .BR getpw (3), .BR getpwent (3), .BR getspnam (3), .BR putpwent (3), .BR setpwent (3), .BR passwd (5)