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 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\" References consulted:
8 .\" Linux libc source code
9 .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
12 .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
13 .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
14 .\" Modified 2003-11-15 by aeb
15 .\" 2008-11-07, mtk, Added an example program for getpwnam_r().
17 .TH GETPWNAM 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
19 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
22 .RI ( libc ", " \-lc )
25 .B #include <sys/types.h>
28 .BI "struct passwd *getpwnam(const char *" name );
29 .BI "struct passwd *getpwuid(uid_t " uid );
31 .BI "int getpwnam_r(const char *restrict " name \
32 ", struct passwd *restrict " pwd ,
33 .BI " char *restrict " buf ", size_t " buflen ,
34 .BI " struct passwd **restrict " result );
35 .BI "int getpwuid_r(uid_t " uid ", struct passwd *restrict " pwd ,
36 .BI " char *restrict " buf ", size_t " buflen ,
37 .BI " struct passwd **restrict " result );
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
49 || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
54 function returns a pointer to a structure containing
55 the broken-out fields of the record in the password database
56 (e.g., the local password file
59 that matches the username
64 function returns a pointer to a structure containing
65 the broken-out fields of the record in the password database
66 that matches the user ID
69 The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
74 char *pw_name; /* username */
75 char *pw_passwd; /* user password */
76 uid_t pw_uid; /* user ID */
77 gid_t pw_gid; /* group ID */
78 char *pw_gecos; /* user information */
79 char *pw_dir; /* home directory */
80 char *pw_shell; /* shell program */
87 for more information about these fields.
93 functions obtain the same information as
97 but store the retrieved
99 structure in the space pointed to by
101 The string fields pointed to by the members of the
103 structure are stored in the buffer
107 A pointer to the result (in case of success) or NULL (in case no entry
108 was found or an error occurred) is stored in
115 sysconf(_SC_GETPW_R_SIZE_MAX)
119 returns either \-1, without changing
121 or an initial suggested size for
123 (If this size is too small,
126 in which case the caller can retry with a larger buffer.)
132 functions return a pointer to a
134 structure, or NULL if the matching entry is not found or
138 is set to indicate the error.
139 If one wants to check
141 after the call, it should be set to zero before the call.
143 The return value may point to a static area, and may be overwritten
144 by subsequent calls to
149 (Do not pass the returned pointer to
160 If no matching password record was found,
161 these functions return 0 and store NULL in
163 In case of error, an error number is returned, and NULL is stored in
167 .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ..."
175 A signal was caught; see
182 The per-process limit on the number of open file descriptors has been reached.
185 The system-wide limit on the total number of open files has been reached.
189 Insufficient memory to allocate
192 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
195 Insufficient buffer space supplied.
199 local password database file
201 For an explanation of the terms used in this section, see
209 Interface Attribute Value
213 MT-Unsafe race:pwnam locale
218 MT-Unsafe race:pwuid locale
231 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
234 field is not specified in POSIX, but is present on most implementations.
236 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
237 It does not call "not found" an error, and hence does not specify what value
239 might have in this situation.
240 But that makes it impossible to recognize
242 One might argue that according to POSIX
244 should be left unchanged if an entry is not found.
245 Experiments on various
246 UNIX-like systems show that lots of different values occur in this
247 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others.
249 .\" AIX 5.1 - gives ESRCH
250 .\" OSF1 4.0g - gives EWOULDBLOCK
251 .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
252 .\" glibc since version 2.7 - give 0
253 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
254 .\" SunOS 5.8 - gives EBADF
255 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
259 field contains the name of the initial working directory of the user.
260 Login programs use the value of this field to initialize the
262 environment variable for the login shell.
263 An application that wants to determine its user's home directory
264 should inspect the value of
266 (rather than the value
267 .IR getpwuid(getuid())\->pw_dir )
268 since this allows the user to modify their notion of
269 "the home directory" during a login session.
270 To determine the (initial) home directory of another user,
271 it is necessary to use
272 .I getpwnam("username")\->pw_dir
275 The program below demonstrates the use of
277 to find the full username and user ID for the username
278 supplied as a command-line argument.
289 main(int argc, char *argv[])
292 struct passwd *result;
298 fprintf(stderr, "Usage: %s username\en", argv[0]);
302 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
303 if (bufsize == \-1) /* Value was indeterminate */
304 bufsize = 16384; /* Should be more than enough */
306 buf = malloc(bufsize);
312 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
313 if (result == NULL) {
315 printf("Not found\en");
318 perror("getpwnam_r");
323 printf("Name: %s; UID: %jd\en", pwd.pw_gecos,
324 (intmax_t) pwd.pw_uid);