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 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .\" References consulted:
28 .\" Linux libc source code
29 .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
32 .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
33 .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
34 .\" Modified 2003-11-15 by aeb
35 .\" 2008-11-07, mtk, Added an example program for getpwnam_r().
37 .TH GETPWNAM 3 2012-04-23 "GNU" "Linux Programmer's Manual"
39 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
42 .B #include <sys/types.h>
45 .BI "struct passwd *getpwnam(const char *" name );
47 .BI "struct passwd *getpwuid(uid_t " uid );
49 .BI "int getpwnam_r(const char *" name ", struct passwd *" pwd ,
51 .BI " char *" buf ", size_t " buflen ", struct passwd **" result );
53 .BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd ,
55 .BI " char *" buf ", size_t " buflen ", struct passwd **" result );
59 Feature Test Macro Requirements for glibc (see
60 .BR feature_test_macros (7)):
67 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
68 _SVID_SOURCE || _POSIX_SOURCE
74 function returns a pointer to a structure containing
75 the broken-out fields of the record in the password database
76 (e.g., the local password file
79 that matches the username
84 function returns a pointer to a structure containing
85 the broken-out fields of the record in the password database
86 that matches the user ID
89 The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
94 char *pw_name; /* username */
95 char *pw_passwd; /* user password */
96 uid_t pw_uid; /* user ID */
97 gid_t pw_gid; /* group ID */
98 char *pw_gecos; /* user information */
99 char *pw_dir; /* home directory */
100 char *pw_shell; /* shell program */
107 for more information about these fields.
113 functions obtain the same information as
117 but store the retrieved
119 structure in the space pointed to by
121 The string fields pointed to by the members of the
123 structure are stored in the buffer
127 A pointer to the result (in case of success) or NULL (in case no entry
128 was found or an error occurred) is stored in
133 sysconf(_SC_GETPW_R_SIZE_MAX)
135 returns either \-1, without changing
137 or an initial suggested size for
139 (If this size is too small,
142 in which case the caller can retry with a larger buffer.)
148 functions return a pointer to a
150 structure, or NULL if the matching entry is not found or
154 is set appropriately.
155 If one wants to check
157 after the call, it should be set to zero before the call.
159 The return value may point to a static area, and may be overwritten
160 by subsequent calls to
165 (Do not pass the returned pointer to
176 If no matching password record was found,
177 these functions return 0 and store NULL in
179 In case of error, an error number is returned, and NULL is stored in
183 .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
199 of files was open already in the calling process.
202 The maximum number of files was open already in the system.
206 Insufficient memory to allocate
209 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
212 Insufficient buffer space supplied.
216 local password database file
218 SVr4, 4.3BSD, POSIX.1-2001.
221 field is not specified in POSIX, but is present on most implementations.
223 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
224 It does not call "not found" an error, and hence does not specify what value
226 might have in this situation.
227 But that makes it impossible to recognize
229 One might argue that according to POSIX
231 should be left unchanged if an entry is not found.
232 Experiments on various
233 UNIX-like systems show that lots of different values occur in this
234 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
236 .\" AIX 5.1 - gives ESRCH
237 .\" OSF1 4.0g - gives EWOULDBLOCK
238 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
239 .\" glibc since version 2.7 - give 0
240 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
241 .\" SunOS 5.8 - gives EBADF
242 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
246 field contains the name of the initial working directory of the user.
247 Login programs use the value of this field to initialize the
249 environment variable for the login shell.
250 An application that wants to determine its user's home directory
251 should inspect the value of
253 (rather than the value
254 .IR getpwuid(getuid())\->pw_dir )
255 since this allows the user to modify their notion of
256 "the home directory" during a login session.
257 To determine the (initial) home directory of another user,
258 it is necessary to use
259 .I getpwnam("username")\->pw_dir
262 The program below demonstrates the use of
264 to find the full username and user ID for the username
265 supplied as a command-line argument.
275 main(int argc, char *argv[])
278 struct passwd *result;
284 fprintf(stderr, "Usage: %s username\\n", argv[0]);
288 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
289 if (bufsize == \-1) /* Value was indeterminate */
290 bufsize = 16384; /* Should be more than enough */
292 buf = malloc(bufsize);
298 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
299 if (result == NULL) {
301 printf("Not found\\n");
304 perror("getpwnam_r");
309 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);