OSDN Git Service

8bca0a01df48c1f341af1e5367ecdbcd413bc207
[linuxjm/LDP_man-pages.git] / release / 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 .\"*******************************************************************
36 .\"
37 .\" This file was generated with po4a. Translate the source file.
38 .\"
39 .\"*******************************************************************
40 .TH GETPWNAM 3 2012\-04\-23 GNU "Linux Programmer's Manual"
41 .SH 名前
42 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- パスワードファイルのエントリの取得
43 .SH 書式
44 .nf
45 \fB#include <sys/types.h>\fP
46 \fB#include <pwd.h>\fP
47 .sp
48 \fBstruct passwd *getpwnam(const char *\fP\fIname\fP\fB);\fP
49 .sp
50 \fBstruct passwd *getpwuid(uid_t \fP\fIuid\fP\fB);\fP
51 .sp
52 \fBint getpwnam_r(const char *\fP\fIname\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
53 .br
54 \fB            char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
55 .sp
56 \fBint getpwuid_r(uid_t \fP\fIuid\fP\fB, struct passwd *\fP\fIpwd\fP\fB,\fP
57 .br
58 \fB            char *\fP\fIbuf\fP\fB, size_t \fP\fIbuflen\fP\fB, struct passwd **\fP\fIresult\fP\fB);\fP
59 .fi
60 .sp
61 .in -4n
62 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
63 .in
64 .sp
65 .ad l
66 \fBgetpwnam_r\fP(), \fBgetpwuid_r\fP():
67 .RS 4
68 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE
69 || _POSIX_SOURCE
70 .RE
71 .ad b
72 .SH 説明
73 \fBgetpwnam\fP()  関数は、ユーザ名 \fIname\fP にマッチするパスワード・データベースのエントリを
74 要素毎に分解し、各要素を格納した構造体へのポインタを返す (パスワード・データベースの例: ローカルのパスワードファイル \fI/etc/passwd\fP,
75 NIS, LDAP)。
76 .PP
77 \fBgetpwuid\fP()  関数は、ユーザ ID \fIuid\fP にマッチするパスワード・データベースのエントリを
78 要素毎に分解し、各要素を格納した構造体へのポインタを返す。
79 .PP
80 \fIpasswd\fP 構造体は、\fI<pwd.h>\fP で以下のように定義されている:
81 .sp
82 .in +4n
83 .nf
84 struct passwd {
85     char   *pw_name;       /* ユーザ名 */
86     char   *pw_passwd;     /* ユーザのパスワード */
87     uid_t   pw_uid;        /* ユーザ ID */
88     gid_t   pw_gid;        /* グループ ID */
89     char   *pw_gecos;      /* ユーザ情報 */
90     char   *pw_dir;        /* ホームディレクトリ */
91     char   *pw_shell;      /* シェルプログラム */
92 };
93 .fi
94 .in
95 .PP
96 これらのフィールドの詳しい情報については \fBpasswd\fP(5) を参照のこと。
97 .PP
98 \fBgetpwnam_r\fP() と \fBgetpwuid_r\fP() 関数は、それぞれ\fBgetpwnam\fP() と
99 \fBgetpwuid\fP() と同じ情報を取得するが、取得した \fIpasswd\fP 構造体を
100 \fIpwd\fP が指す領域に格納する。\fIpasswd\fP 構造体のメンバーが指す文字列は、
101 サイズ \fIbuflen\fP のバッファ \fIbuf\fP に格納される。成功した場合
102 \fI*result\fP には結果へのポインタが格納される。エントリが見つからなかった
103 場合やエラーが発生した場合には \fI*result\fP には NULL が入る。
104 .PP
105 呼び出し
106
107     sysconf(_SC_GETPW_R_SIZE_MAX)
108
109 は、 \fIerrno\fP を変更せずに \-1 を返すか、 \fIbuf\fP の初期サイズの推奨値を
110 返す。(このサイズが小さすぎる場合、呼び出しは \fBERANGE\fP で失敗し、この
111 場合には呼び出し側はバッファを大きくしてから再度呼び出すことができる。)
112 .SH 返り値
113 \fBgetpwnam\fP()  と \fBgetpwuid\fP()  関数は、 \fIpasswd\fP 構造体へのポインタを返す。
114 一致するエントリが見つからなかった場合や、エラーが発生した場合は NULL を返す。 エラーが起こった場合、 \fIerrno\fP が適切に設定される。
115 呼び出しの後で \fIerrno\fP をチェックしたい場合は、 呼び出しの前に (この値を) 0 に設定しておくべきである。
116 .LP
117 返り値は静的な領域を指しており、その後の \fBgetpwent\fP(3), \fBgetpwnam\fP(), \fBgetpwuid\fP()
118 の呼び出しで上書きされるかもしれない。 (返されたポインタを \fBfree\fP(3)  に渡さないこと。)
119 .LP
120 成功すると、 \fBgetpwnam_r\fP()  と \fBgetpwuid_r\fP()  は 0 を返し、 \fI*result\fP に \fIpwd\fP
121 を設定する。 マッチするパスワード・エントリが見つからなかった場合には、 0 を返し、 \fI*result\fP に NULL を設定する。
122 エラーの場合、エラー番号を返し、 \fI*result\fP に NULL を設定する。
123 .SH エラー
124 .TP 
125 \fB0\fP または \fBENOENT\fP または \fBESRCH\fP または \fBEBADF\fP または \fBEPERM\fP または ... 
126 指定された \fIname\fP または \fIuid\fP が見つからなかった。
127 .TP 
128 \fBEINTR\fP
129 シグナルが捕捉された。
130 .TP 
131 \fBEIO\fP
132 I/O エラー。
133 .TP 
134 \fBEMFILE\fP
135 呼び出し元プロセスがオープンしているファイル数が すでに上限 (\fBOPEN_MAX\fP)  であった。
136 .TP 
137 \fBENFILE\fP
138 システムでオープンされているファイル数がすでに上限であった。
139 .TP 
140 \fBENOMEM\fP
141 .\" not in POSIX
142 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
143 \fIpasswd\fP 構造体に割り当てるメモリが十分なかった。
144 .TP 
145 \fBERANGE\fP
146 与えられたバッファ空間が不十分である。
147 .SH ファイル
148 .TP 
149 \fI/etc/passwd\fP
150 ローカルのパスワード・データベースファイル
151 .SH 準拠
152 SVr4, 4.3BSD, POSIX.1\-2001.  \fIpw_gecos\fP フィールドは POSIX では規定されていないが、
153 ほとんどの実装に存在する。
154 .SH 注意
155 .\" more precisely:
156 .\" AIX 5.1 - gives ESRCH
157 .\" OSF1 4.0g - gives EWOULDBLOCK
158 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
159 .\" glibc since version 2.7 - give 0
160 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
161 .\" SunOS 5.8 - gives EBADF
162 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
163 上記の「返り値」以下の記述は POSIX.1\-2001 に拠る。 この標準は「(エントリが) 見つからないこと」をエラーとしていないので、
164 そのような場合に \fIerrno\fP がどのような値になるかを定めていない。 そのため、エラーを認識することは不可能である。 POSIX
165 に準拠して、エントリが見つからない場合は \fIerrno\fP を変更しないようにすべきである、と主張する人もいるかもしれない。 様々な UNIX
166 系のシステムで試してみると、そのような場合には 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM
167 といった様々な値が返される。 他の値が返されるかもしれない。
168
169 フィールド \fIpw_dir\fP には、ユーザの作業ディレクトリ名の初期値が格納される。 ログインプロセスは、このフィールドの値を使って、
170 ログインシェルの \fBHOME\fP 環境変数を初期化する。 アプリケーションが、ユーザのホーム・ディレクトリを決定する場合には、
171 (\fIgetpwuid(getuid())\->pw_dir\fP の値ではなく)  \fBHOME\fP の値を検査するようにすべきである。
172 なぜなら、このようにすることで、ユーザがログイン・セッション中で 「ホーム・ディレクトリ」の意味を変更できるようになるからである。
173 別のユーザのホーム・ディレクトリ (の初期値) を知るには \fIgetpwnam("username")\->pw_dir\fP
174 か同様の方法を使う必要がある。
175 .SH 例
176 以下のプログラムは \fBgetpwnam_r\fP()  の使用例を示したもので、コマンドライン引き数で渡されたユーザ名に対する 完全なユーザ名とユーザ
177 ID を探すものである。
178
179 .nf
180 #include <pwd.h>
181 #include <stdio.h>
182 #include <stdlib.h>
183 #include <unistd.h>
184 #include <errno.h>
185
186 int
187 main(int argc, char *argv[])
188 {
189     struct passwd pwd;
190     struct passwd *result;
191     char *buf;
192     size_t bufsize;
193     int s;
194
195     if (argc != 2) {
196         fprintf(stderr, "Usage: %s username\en", argv[0]);
197         exit(EXIT_FAILURE);
198     }
199
200     bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
201     if (bufsize == \-1)          /* 値を決定できなかった */
202         bufsize = 16384;        /* 十分大きな値にすべき */
203
204     buf = malloc(bufsize);
205     if (buf == NULL) {
206         perror("malloc");
207         exit(EXIT_FAILURE);
208     }
209
210     s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
211     if (result == NULL) {
212         if (s == 0)
213             printf("Not found\en");
214         else {
215             errno = s;
216             perror("getpwnam_r");
217         }
218         exit(EXIT_FAILURE);
219     }
220
221     printf("Name: %s; UID: %ld\en", pwd.pw_gecos, (long) pwd.pw_uid);
222     exit(EXIT_SUCCESS);
223 }
224 .fi
225 .SH 関連項目
226 \fBendpwent\fP(3), \fBfgetpwent\fP(3), \fBgetgrnam\fP(3), \fBgetpw\fP(3),
227 \fBgetpwent\fP(3), \fBgetspnam\fP(3), \fBputpwent\fP(3), \fBsetpwent\fP(3), \fBpasswd\fP(5)
228 .SH この文書について
229 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.40 の一部
230 である。プロジェクトの説明とバグ報告に関する情報は
231 http://www.kernel.org/doc/man\-pages/ に書かれている。