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 2019-03-06 "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 ,
50 .BI " char *" buf ", size_t " buflen ", struct passwd **" result );
52 .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)):
66 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_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 ... "
189 A signal was caught; see
196 The per-process limit on the number of open file descriptors has been reached.
199 The system-wide limit on the total number of open files has been reached.
203 Insufficient memory to allocate
206 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
209 Insufficient buffer space supplied.
213 local password database file
215 For an explanation of the terms used in this section, see
221 Interface Attribute Value
224 T} Thread safety MT-Unsafe race:pwnam locale
227 T} Thread safety MT-Unsafe race:pwuid locale
232 T} Thread safety MT-Safe locale
235 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
238 field is not specified in POSIX, but is present on most implementations.
240 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
241 It does not call "not found" an error, and hence does not specify what value
243 might have in this situation.
244 But that makes it impossible to recognize
246 One might argue that according to POSIX
248 should be left unchanged if an entry is not found.
249 Experiments on various
250 UNIX-like systems show that lots of different values occur in this
251 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others.
253 .\" AIX 5.1 - gives ESRCH
254 .\" OSF1 4.0g - gives EWOULDBLOCK
255 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
256 .\" glibc since version 2.7 - give 0
257 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
258 .\" SunOS 5.8 - gives EBADF
259 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
263 field contains the name of the initial working directory of the user.
264 Login programs use the value of this field to initialize the
266 environment variable for the login shell.
267 An application that wants to determine its user's home directory
268 should inspect the value of
270 (rather than the value
271 .IR getpwuid(getuid())\->pw_dir )
272 since this allows the user to modify their notion of
273 "the home directory" during a login session.
274 To determine the (initial) home directory of another user,
275 it is necessary to use
276 .I getpwnam("username")\->pw_dir
279 The program below demonstrates the use of
281 to find the full username and user ID for the username
282 supplied as a command-line argument.
292 main(int argc, char *argv[])
295 struct passwd *result;
301 fprintf(stderr, "Usage: %s username\en", argv[0]);
305 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
306 if (bufsize == \-1) /* Value was indeterminate */
307 bufsize = 16384; /* Should be more than enough */
309 buf = malloc(bufsize);
315 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
316 if (result == NULL) {
318 printf("Not found\en");
321 perror("getpwnam_r");
326 printf("Name: %s; UID: %ld\en", pwd.pw_gecos, (long) pwd.pw_uid);