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 2012-04-23 "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
131 sysconf(_SC_GETPW_R_SIZE_MAX)
133 returns either \-1, without changing
135 or an initial suggested size for
137 (If this size is too small,
140 in which case the caller can retry with a larger buffer.)
146 functions return a pointer to a
148 structure, or NULL if the matching entry is not found or
152 is set appropriately.
153 If one wants to check
155 after the call, it should be set to zero before the call.
157 The return value may point to a static area, and may be overwritten
158 by subsequent calls to
163 (Do not pass the returned pointer to
174 If no matching password record was found,
175 these functions return 0 and store NULL in
177 In case of error, an error number is returned, and NULL is stored in
181 .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
197 of files was open already in the calling process.
200 The maximum number of files was open already in the system.
204 Insufficient memory to allocate
207 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
210 Insufficient buffer space supplied.
214 local password database file
216 SVr4, 4.3BSD, POSIX.1-2001.
219 field is not specified in POSIX, but is present on most implementations.
221 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
222 It does not call "not found" an error, and hence does not specify what value
224 might have in this situation.
225 But that makes it impossible to recognize
227 One might argue that according to POSIX
229 should be left unchanged if an entry is not found.
230 Experiments on various
231 UNIX-like systems show that lots of different values occur in this
232 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
234 .\" AIX 5.1 - gives ESRCH
235 .\" OSF1 4.0g - gives EWOULDBLOCK
236 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
237 .\" glibc since version 2.7 - give 0
238 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
239 .\" SunOS 5.8 - gives EBADF
240 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
244 field contains the name of the initial working directory of the user.
245 Login programs use the value of this field to initialize the
247 environment variable for the login shell.
248 An application that wants to determine its user's home directory
249 should inspect the value of
251 (rather than the value
252 .IR getpwuid(getuid())\->pw_dir )
253 since this allows the user to modify their notion of
254 "the home directory" during a login session.
255 To determine the (initial) home directory of another user,
256 it is necessary to use
257 .I getpwnam("username")\->pw_dir
260 The program below demonstrates the use of
262 to find the full username and user ID for the username
263 supplied as a command-line argument.
273 main(int argc, char *argv[])
276 struct passwd *result;
282 fprintf(stderr, "Usage: %s username\\n", argv[0]);
286 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
287 if (bufsize == \-1) /* Value was indeterminate */
288 bufsize = 16384; /* Should be more than enough */
290 buf = malloc(bufsize);
296 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
297 if (result == NULL) {
299 printf("Not found\\n");
302 perror("getpwnam_r");
307 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);