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 .TH GETPWNAM 3 2010-10-21 "GNU" "Linux Programmer's Manual"
37 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
40 .B #include <sys/types.h>
43 .BI "struct passwd *getpwnam(const char *" name );
45 .BI "struct passwd *getpwuid(uid_t " uid );
47 .BI "int getpwnam_r(const char *" name ", struct passwd *" pwd ,
49 .BI " char *" buf ", size_t " buflen ", struct passwd **" result );
51 .BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd ,
53 .BI " char *" buf ", size_t " buflen ", struct passwd **" result );
57 Feature Test Macro Requirements for glibc (see
58 .BR feature_test_macros (7)):
65 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
66 _SVID_SOURCE || _POSIX_SOURCE
72 function returns a pointer to a structure containing
73 the broken-out fields of the record in the password database
74 (e.g., the local password file
77 that matches the username
82 function returns a pointer to a structure containing
83 the broken-out fields of the record in the password database
84 that matches the user ID
87 The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
92 char *pw_name; /* username */
93 char *pw_passwd; /* user password */
94 uid_t pw_uid; /* user ID */
95 gid_t pw_gid; /* group ID */
96 char *pw_gecos; /* user information */
97 char *pw_dir; /* home directory */
98 char *pw_shell; /* shell program */
105 for more information about these fields.
111 functions obtain the same information as
115 but store the retrieved
117 structure in the space pointed to by
119 The string fields pointed to by the members of the
121 structure are stored in the buffer
125 A pointer to the result (in case of success) or NULL (in case no entry
126 was found or an error occurred) is stored in
129 The maximum size needed for
134 .BR _SC_GETPW_R_SIZE_MAX .
140 functions return a pointer to a
142 structure, or NULL if the matching entry is not found or
146 is set appropriately.
147 If one wants to check
149 after the call, it should be set to zero before the call.
151 The return value may point to a static area, and may be overwritten
152 by subsequent calls to
157 (Do not pass the returned pointer to
168 If no matching password record was found,
169 these functions return 0 and store NULL in
171 In case of error, an error number is returned, and NULL is stored in
175 .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
191 of files was open already in the calling process.
194 The maximum number of files was open already in the system.
198 Insufficient memory to allocate
201 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
204 Insufficient buffer space supplied.
208 local password database file
210 SVr4, 4.3BSD, POSIX.1-2001.
213 field is not specified in POSIX, but is present on most implementations.
215 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
216 It does not call "not found" an error, and hence does not specify what value
218 might have in this situation.
219 But that makes it impossible to recognize
221 One might argue that according to POSIX
223 should be left unchanged if an entry is not found.
224 Experiments on various
225 UNIX-like systems show that lots of different values occur in this
226 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
228 .\" AIX 5.1 - gives ESRCH
229 .\" OSF1 4.0g - gives EWOULDBLOCK
230 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
231 .\" glibc since version 2.7 - give 0
232 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
233 .\" SunOS 5.8 - gives EBADF
234 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
238 field contains the name of the initial working directory of the user.
239 Login programs use the value of this field to initialize the
241 environment variable for the login shell.
242 An application that wants to determine its user's home directory
243 should inspect the value of
245 (rather than the value
246 .IR getpwuid(getuid())\->pw_dir )
247 since this allows the user to modify their notion of
248 "the home directory" during a login session.
249 To determine the (initial) home directory of another user,
250 it is necessary to use
251 .I getpwnam("username")\->pw_dir
254 The program below demonstrates the use of
256 to find the full username and user ID for the username
257 supplied as a command-line argument.
267 main(int argc, char *argv[])
270 struct passwd *result;
276 fprintf(stderr, "Usage: %s username\\n", argv[0]);
280 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
281 if (bufsize == \-1) /* Value was indeterminate */
282 bufsize = 16384; /* Should be more than enough */
284 buf = malloc(bufsize);
290 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
291 if (result == NULL) {
293 printf("Not found\\n");
296 perror("getpwnam_r");
301 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);