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)):
70 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
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 Í×ÁÇËè¤Ëʬ²ò¤·¡¢³ÆÍ×ÁǤò³ÊǼ¤·¤¿¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹
99 (¥Ñ¥¹¥ï¡¼¥É¡¦¥Ç¡¼¥¿¥Ù¡¼¥¹¤ÎÎã:
100 ¥í¡¼¥«¥ë¤Î¥Ñ¥¹¥ï¡¼¥É¥Õ¥¡¥¤¥ë
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 ´Ø¿ô¤Ï (¾åµ¤Î´Ø¿ô¤È) Ʊ¤¸¾ðÊó¤ò¼èÆÀ¤¹¤ë¤¬¡¢
132 ¤¬»Ø¤¹Îΰè¤Ë³ÊǼ¤¹¤ë¡£
135 .\"O structure contains pointers to strings, and these strings
136 .\"O are stored in the buffer
142 ¹½Â¤ÂΤˤÏʸ»úÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬´Þ¤Þ¤ì¡¢
143 ¤³¤ì¤é¤Îʸ»úÎó¤Ï¥µ¥¤¥º
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
153 ¤Ë¤Ï·ë²Ì¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬³ÊǼ¤µ¤ì¤ë¡£
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 .
189 ¤ËºÇÂçɬÍפʥµ¥¤¥º¤Ï¡¢
192 .B _SC_GETPW_R_SIZE_MAX
193 ¤ò»ØÄꤷ¤Æ¼Â¹Ô¤¹¤ë¤³¤È¤Çʬ¤«¤ë¡£
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.
209 ¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹¡£
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.
217 ¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿¾ì¹ç¡¢
222 ¤ò¥Á¥§¥Ã¥¯¤·¤¿¤¤¾ì¹ç¤Ï¡¢
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
233 ÊÖ¤êÃͤÏÀÅŪ¤ÊÎΰè¤ò»Ø¤·¤Æ¤ª¤ê¡¢¤½¤Î¸å¤Î
237 ¤Î¸Æ¤Ó½Ð¤·¤Ç¾å½ñ¤¤µ¤ì¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
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 ¥Þ¥Ã¥Á¤¹¤ë¥Ñ¥¹¥ï¡¼¥É¡¦¥¨¥ó¥È¥ê¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¡¢
268 ¥¨¥é¡¼¤Î¾ì¹ç¡¢¥¨¥é¡¼ÈÖ¹æ¤òÊÖ¤·¡¢
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.
289 ¥·¥°¥Ê¥ë¤¬¥¥ã¥Ã¥Á¤µ¤ì¤¿¡£
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) ¤Î¥Õ¥¡¥¤¥ë¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¡£
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)
314 ¹½Â¤ÂΤò³ä¤êÅö¤Æ¤ë¤¿¤á¤Î¥á¥â¥ê¤¬ÉÔ½½Ê¬¡£
315 .\" ¤³¤Î¹½Â¤ÂΤÏÀÅŪ¤Ç¤¢¤ê¡¢0 ²ó¤Þ¤¿¤Ï 1 ²ó³ä¤êÅö¤Æ¤é¤ì¤ë¡£
316 .\" ¥á¥â¥ê¥ê¡¼¥¯¤Ï̵¤¤¡£(libc45)
319 .\"O Insufficient buffer space supplied.
320 Í¿¤¨¤é¤ì¤¿¥Ð¥Ã¥Õ¥¡¶õ´Ö¤¬ÉÔ½½Ê¬¤Ç¤¢¤ë¡£
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 ¤³¤Îɸ½à¤Ï¡Ö(¥¨¥ó¥È¥ê¤¬) ¸«¤Ä¤«¤é¤Ê¤¤¤³¤È¡×¤ò¥¨¥é¡¼¤È¤·¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢
348 ¤¬¤É¤Î¤è¤¦¤ÊÃͤˤʤ뤫¤òÄê¤á¤Æ¤¤¤Ê¤¤¡£
349 ¤½¤Î¤¿¤á¡¢¥¨¥é¡¼¤òǧ¼±¤¹¤ë¤³¤È¤ÏÉÔ²Äǽ¤Ç¤¢¤ë¡£
350 POSIX ¤Ë½àµò¤·¤Æ¡¢¥¨¥ó¥È¥ê¤¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ï
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
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 ¥í¥°¥¤¥ó¥×¥í¥»¥¹¤Ï¡¢¤³¤Î¥Õ¥£¡¼¥ë¥É¤ÎÃͤò»È¤Ã¤Æ¡¢
396 ´Ä¶ÊÑ¿ô¤ò½é´ü²½¤¹¤ë¡£
397 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤¬¡¢¥æ¡¼¥¶¤Î¥Û¡¼¥à¡¦¥Ç¥£¥ì¥¯¥È¥ê¤ò·èÄꤹ¤ë¾ì¹ç¤Ë¤Ï¡¢
398 .RI ( getpwuid(getuid())\->pw_dir
401 ¤ÎÃͤò¸¡ºº¤¹¤ë¤è¤¦¤Ë¤¹¤Ù¤¤Ç¤¢¤ë¡£
402 ¤Ê¤¼¤Ê¤é¡¢¤³¤Î¤è¤¦¤Ë¤¹¤ë¤³¤È¤Ç¡¢¥æ¡¼¥¶¤¬¥í¥°¥¤¥ó¡¦¥»¥Ã¥·¥ç¥óÃæ¤Ç
403 ¡Ö¥Û¡¼¥à¡¦¥Ç¥£¥ì¥¯¥È¥ê¡×¤Î°ÕÌ£¤òÊѹ¹¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤ë¤«¤é¤Ç¤¢¤ë¡£
404 Ê̤Υ桼¥¶¤Î¥Û¡¼¥à¡¦¥Ç¥£¥ì¥¯¥È¥ê (¤Î½é´üÃÍ) ¤òÃΤë¤Ë¤Ï
405 .I getpwnam("username")\->pw_dir
406 ¤«Æ±ÍͤÎÊýË¡¤ò»È¤¦É¬Íפ¬¤¢¤ë¡£
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);