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)
78 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
79 _SVID_SOURCE || _POSIX_SOURCE
85 .\"O function returns a pointer to a structure containing
86 .\"O the broken-out fields of the record in the password database
87 .\"O (e.g., the local password file
88 .\"O .IR /etc/passwd ,
90 .\"O that matches the username
95 ¤Ë¥Þ¥Ã¥Á¤¹¤ë¥Ñ¥¹¥ï¡¼¥É¡¦¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î¥¨¥ó¥È¥ê¤ò
96 Í×ÁÇËè¤Ëʬ²ò¤·¡¢³ÆÍ×ÁǤò³ÊǼ¤·¤¿¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹
97 (¥Ñ¥¹¥ï¡¼¥É¡¦¥Ç¡¼¥¿¥Ù¡¼¥¹¤ÎÎã:
98 ¥í¡¼¥«¥ë¤Î¥Ñ¥¹¥ï¡¼¥É¥Õ¥¡¥¤¥ë
104 .\"O function returns a pointer to a structure containing
105 .\"O the broken-out fields of the record in the password database
106 .\"O that matches the user ID
111 ¤Ë¥Þ¥Ã¥Á¤¹¤ë¥Ñ¥¹¥ï¡¼¥É¡¦¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î¥¨¥ó¥È¥ê¤ò
112 Í×ÁÇËè¤Ëʬ²ò¤·¡¢³ÆÍ×ÁǤò³ÊǼ¤·¤¿¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹¡£
115 .\"O .BR getpwnam_r ()
117 .\"O .BR getpwuid_r ()
118 .\"O functions obtain the same information, but store the retrieved
120 .\"O structure in the space pointed to by
125 ´Ø¿ô¤Ï (¾åµ¤Î´Ø¿ô¤È) Ʊ¤¸¾ðÊó¤ò¼èÆÀ¤¹¤ë¤¬¡¢
130 ¤¬»Ø¤¹Îΰè¤Ë³ÊǼ¤¹¤ë¡£
133 .\"O structure contains pointers to strings, and these strings
134 .\"O are stored in the buffer
140 ¹½Â¤ÂΤˤÏʸ»úÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬´Þ¤Þ¤ì¡¢
141 ¤³¤ì¤é¤Îʸ»úÎó¤Ï¥µ¥¤¥º
146 .\"O A pointer to the result (in case of success) or NULL (in case no entry
147 .\"O was found or an error occurred) is stored in
151 ¤Ë¤Ï·ë²Ì¤Ø¤Î¥Ý¥¤¥ó¥¿¤¬³ÊǼ¤µ¤ì¤ë¡£
152 ¥¨¥ó¥È¥ê¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤ä¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ë¤Ï
156 .\"O The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
157 \fIpasswd\fP ¹½Â¤ÂΤϡ¢\fI<pwd.h>\fP ¤Ç°Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
162 .\"O char *pw_name; /* username */
163 .\"O char *pw_passwd; /* user password */
164 .\"O uid_t pw_uid; /* user ID */
165 .\"O gid_t pw_gid; /* group ID */
166 .\"O char *pw_gecos; /* real name */
167 .\"O char *pw_dir; /* home directory */
168 .\"O char *pw_shell; /* shell program */
169 char *pw_name; /* ¥æ¡¼¥¶Ì¾ */
170 char *pw_passwd; /* ¥æ¡¼¥¶¤Î¥Ñ¥¹¥ï¡¼¥É */
171 uid_t pw_uid; /* ¥æ¡¼¥¶ ID */
172 gid_t pw_gid; /* ¥°¥ë¡¼¥× ID */
173 char *pw_gecos; /* ¼Â̾ */
174 char *pw_dir; /* ¥Û¡¼¥à¥Ç¥£¥ì¥¯¥È¥ê */
175 char *pw_shell; /* ¥·¥§¥ë¥×¥í¥°¥é¥à */
180 .\"O The maximum needed size for
182 .\"O can be found using
184 .\"O with the argument
185 .\"O .BR _SC_GETPW_R_SIZE_MAX .
187 ¤ËºÇÂçɬÍפʥµ¥¤¥º¤Ï¡¢
190 .B _SC_GETPW_R_SIZE_MAX
191 ¤ò»ØÄꤷ¤Æ¼Â¹Ô¤¹¤ë¤³¤È¤Çʬ¤«¤ë¡£
192 .\"O .SH "RETURN VALUE"
198 .\"O functions return a pointer to a
200 .\"O structure, or NULL if the matching entry is not found or
201 .\"O an error occurs.
207 ¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤òÊÖ¤¹¡£
208 °ìÃפ¹¤ë¥¨¥ó¥È¥ê¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤ä¡¢¥¨¥é¡¼¤¬È¯À¸¤·¤¿¾ì¹ç¤Ï NULL ¤òÊÖ¤¹¡£
209 .\"O If an error occurs,
211 .\"O is set appropriately.
212 .\"O If one wants to check
214 .\"O after the call, it should be set to zero before the call.
215 ¥¨¥é¡¼¤¬µ¯¤³¤Ã¤¿¾ì¹ç¡¢
220 ¤ò¥Á¥§¥Ã¥¯¤·¤¿¤¤¾ì¹ç¤Ï¡¢
221 ¸Æ¤Ó½Ð¤·¤ÎÁ°¤Ë (¤³¤ÎÃͤò) 0 ¤ËÀßÄꤷ¤Æ¤ª¤¯¤Ù¤¤Ç¤¢¤ë¡£
223 .\"O The return value may point to a static area, and may be overwritten
224 .\"O by subsequent calls to
225 .\"O .BR getpwent (3),
226 .\"O .BR getpwnam (),
228 .\"O .BR getpwuid ().
229 .\"O (Do not pass the returned pointer to
231 ÊÖ¤êÃͤÏÀÅŪ¤ÊÎΰè¤ò»Ø¤·¤Æ¤ª¤ê¡¢¤½¤Î¸å¤Î
235 ¤Î¸Æ¤Ó½Ð¤·¤Ç¾å½ñ¤¤µ¤ì¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
241 .\"O .BR getpwnam_r ()
243 .\"O .BR getpwuid_r ()
244 .\"O return zero, and set
248 .\"O If no matching password record was found,
249 .\"O these functions return 0 and store NULL in
251 .\"O In case of error, an error number is returned, and NULL is stored in
262 ¥Þ¥Ã¥Á¤¹¤ë¥Ñ¥¹¥ï¡¼¥É¡¦¥¨¥ó¥È¥ê¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¤Ï¡¢
266 ¥¨¥é¡¼¤Î¾ì¹ç¡¢¥¨¥é¡¼ÈÖ¹æ¤òÊÖ¤·¡¢
272 .\"O .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
273 .BR 0 " ¤Þ¤¿¤Ï " ENOENT " ¤Þ¤¿¤Ï " ESRCH " ¤Þ¤¿¤Ï " EBADF " ¤Þ¤¿¤Ï " EPERM " ¤Þ¤¿¤Ï ... "
286 .\"O A signal was caught.
287 ¥·¥°¥Ê¥ë¤¬¥¥ã¥Ã¥Á¤µ¤ì¤¿¡£
294 .\"O The maximum number
295 .\"O .RB ( OPEN_MAX )
296 .\"O of files was open already in the calling process.
297 ¸Æ¤Ó½Ð¤·¤¿¥×¥í¥»¥¹¤Ë¤ª¤¤¤Æ¡¢
298 ´û¤ËºÇÂç¿ô (OPEN_MAX) ¤Î¥Õ¥¡¥¤¥ë¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¡£
301 .\"O The maximum number of files was open already in the system.
302 ¥·¥¹¥Æ¥à¾å¤Ç´û¤ËºÇÂç¿ô¤Î¥Õ¥¡¥¤¥ë¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¡£
306 .\"O Insufficient memory to allocate
309 .\"O .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
312 ¹½Â¤ÂΤò³ä¤êÅö¤Æ¤ë¤¿¤á¤Î¥á¥â¥ê¤¬ÉÔ½½Ê¬¡£
313 .\" ¤³¤Î¹½Â¤ÂΤÏÀÅŪ¤Ç¤¢¤ê¡¢0 ²ó¤Þ¤¿¤Ï 1 ²ó³ä¤êÅö¤Æ¤é¤ì¤ë¡£
314 .\" ¥á¥â¥ê¥ê¡¼¥¯¤Ï̵¤¤¡£(libc45)
317 .\"O Insufficient buffer space supplied.
318 Í¿¤¨¤é¤ì¤¿¥Ð¥Ã¥Õ¥¡¶õ´Ö¤¬ÉÔ½½Ê¬¤Ç¤¢¤ë¡£
323 .\"O local password database file
324 ¥í¡¼¥«¥ë¤Î¥Ñ¥¹¥ï¡¼¥É¡¦¥Ç¡¼¥¿¥Ù¡¼¥¹¥Õ¥¡¥¤¥ë
325 .\"O .SH "CONFORMING TO"
327 SVr4, 4.3BSD, POSIX.1-2001.
330 .\"O The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
331 ¾åµ¤Î¡ÖÊÖ¤êÃ͡װʲ¼¤Îµ½Ò¤Ï POSIX.1-2001 ¤Ëµò¤ë¡£
332 .\"O It does not call "not found" an error, and hence does not specify what value
334 .\"O might have in this situation.
335 .\"O But that makes it impossible to recognize
337 .\"O One might argue that according to POSIX
339 .\"O should be left unchanged if an entry is not found.
340 .\"O Experiments on various
341 .\"O Unix-like systems show that lots of different values occur in this
342 .\"O situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
343 ¤³¤Îɸ½à¤Ï¡Ö(¥¨¥ó¥È¥ê¤¬) ¸«¤Ä¤«¤é¤Ê¤¤¤³¤È¡×¤ò¥¨¥é¡¼¤È¤·¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢
346 ¤¬¤É¤Î¤è¤¦¤ÊÃͤˤʤ뤫¤òÄê¤á¤Æ¤¤¤Ê¤¤¡£
347 ¤½¤Î¤¿¤á¡¢¥¨¥é¡¼¤òǧ¼±¤¹¤ë¤³¤È¤ÏÉÔ²Äǽ¤Ç¤¢¤ë¡£
348 POSIX ¤Ë½àµò¤·¤Æ¡¢¥¨¥ó¥È¥ê¤¬¸«¤Ä¤«¤é¤Ê¤¤¾ì¹ç¤Ï
350 ¤òÊѹ¹¤·¤Ê¤¤¤è¤¦¤Ë¤¹¤Ù¤¤Ç¤¢¤ë¡¢¤È¼çÄ¥¤¹¤ë¿Í¤â¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
351 ÍÍ¡¹¤Ê Unix ·Ï¤Î¥·¥¹¥Æ¥à¤Ç»î¤·¤Æ¤ß¤ë¤È¡¢¤½¤Î¤è¤¦¤Ê¾ì¹ç¤Ë¤Ï
352 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM ¤È¤¤¤Ã¤¿ÍÍ¡¹¤ÊÃͤ¬ÊÖ¤µ¤ì¤ë¡£
353 ¾¤ÎÃͤ¬ÊÖ¤µ¤ì¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
354 .\"O .\" more precisely:
355 .\"O .\" AIX 5.1 - gives ESRCH
356 .\"O .\" OSF1 4.0g - gives EWOULDBLOCK
357 .\"O .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
358 .\"O .\" glibc since version 2.7 - give 0
359 .\"O .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
360 .\"O .\" SunOS 5.8 - gives EBADF
361 .\"O .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
363 .\" AIX 5.1 ¤Ï ESRCH ¤òÊÖ¤¹¡£
364 .\" OSF1 4.0g ¤Ï EWOULDBLOCK ¤òÊÖ¤¹¡£
365 .\" libc, glibc (¥Ð¡¼¥¸¥ç¥ó 2.6 ¤Þ¤Ç), Irix 6.5 ¤Ï ENOENT ¤òÊÖ¤¹¡£
366 .\" glibc (¥Ð¡¼¥¸¥ç¥ó 2.7 °Ê¹ß) ¤Ï 0 ¤òÊÖ¤¹¡£
367 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 ¤Ï EPERM ¤òÊÖ¤¹¡£
368 .\" SunOS 5.8 ¤Ï EBADF ¤òÊÖ¤¹¡£
369 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 ¤Ï 0 ¤òÊÖ¤¹¡£
373 .\"O field contains the name of the initial working directory of the user.
376 ¤Ë¤Ï¡¢¥æ¡¼¥¶¤Îºî¶È¥Ç¥£¥ì¥¯¥È¥ê̾¤Î½é´üÃͤ¬³ÊǼ¤µ¤ì¤ë¡£
377 .\"O Login programs use the value of this field to initialize the
379 .\"O environment variable for the login shell.
380 .\"O An application that wants to determine its user's home directory
381 .\"O should inspect the value of
383 .\"O (rather than the value
384 .\"O .IR getpwuid(getuid())\->pw_dir )
385 .\"O since this allows the user to modify their notion of
386 .\"O "the home directory" during a login session.
387 .\"O To determine the (initial) home directory of another user,
388 .\"O it is necessary to use
389 .\"O .I getpwnam("username")\->pw_dir
391 ¥í¥°¥¤¥ó¥×¥í¥»¥¹¤Ï¡¢¤³¤Î¥Õ¥£¡¼¥ë¥É¤ÎÃͤò»È¤Ã¤Æ¡¢
394 ´Ä¶ÊÑ¿ô¤ò½é´ü²½¤¹¤ë¡£
395 ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤¬¡¢¥æ¡¼¥¶¤Î¥Û¡¼¥à¡¦¥Ç¥£¥ì¥¯¥È¥ê¤ò·èÄꤹ¤ë¾ì¹ç¤Ë¤Ï¡¢
396 .RI ( getpwuid(getuid())\->pw_dir
399 ¤ÎÃͤò¸¡ºº¤¹¤ë¤è¤¦¤Ë¤¹¤Ù¤¤Ç¤¢¤ë¡£
400 ¤Ê¤¼¤Ê¤é¡¢¤³¤Î¤è¤¦¤Ë¤¹¤ë¤³¤È¤Ç¡¢¥æ¡¼¥¶¤¬¥í¥°¥¤¥ó¡¦¥»¥Ã¥·¥ç¥óÃæ¤Ç
401 ¡Ö¥Û¡¼¥à¡¦¥Ç¥£¥ì¥¯¥È¥ê¡×¤Î°ÕÌ£¤òÊѹ¹¤Ç¤¤ë¤è¤¦¤Ë¤Ê¤ë¤«¤é¤Ç¤¢¤ë¡£
402 Ê̤Υ桼¥¶¤Î¥Û¡¼¥à¡¦¥Ç¥£¥ì¥¯¥È¥ê (¤Î½é´üÃÍ) ¤òÃΤë¤Ë¤Ï
403 .I getpwnam("username")\->pw_dir
404 ¤«Æ±ÍͤÎÊýË¡¤ò»È¤¦É¬Íפ¬¤¢¤ë¡£
407 .\"O The program below demonstrates the use of
408 .\"O .BR getpwnam_r ()
409 .\"O to find the full username and user ID for the username
410 .\"O supplied as a command-line argument.
413 ¤Î»ÈÍÑÎã¤ò¼¨¤·¤¿¤â¤Î¤Ç¡¢¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤¿ô¤ÇÅϤµ¤ì¤¿¥æ¡¼¥¶Ì¾¤ËÂФ¹¤ë
414 ´°Á´¤Ê¥æ¡¼¥¶Ì¾¤È¥æ¡¼¥¶ ID ¤òõ¤¹¤â¤Î¤Ç¤¢¤ë¡£
424 main(int argc, char *argv[])
427 struct passwd *result;
433 fprintf(stderr, "Usage: %s username\\n", argv[0]);
437 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
438 .\"O if (bufsize == \-1) /* Value was indeterminate */
439 .\"O bufsize = 16384; /* Should be more than enough */
440 if (bufsize == \-1) /* Ãͤò·èÄê¤Ç¤¤Ê¤«¤Ã¤¿ */
441 bufsize = 16384; /* ½½Ê¬Â礤ÊÃͤˤ¹¤Ù¤ */
443 buf = malloc(bufsize);
449 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
450 if (result == NULL) {
452 printf("Not found\\n");
455 perror("getpwnam_r");
460 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);