2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
4 .\" <mtk.manpages@gmail.com>
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
8 .\" References consulted:
9 .\" Linux libc source code
10 .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
13 .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
14 .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
15 .\" Modified 2003-11-15 by aeb
16 .\" 2008-11-07, mtk, Added an example program for getpwnam_r().
18 .TH getpwnam 3 (date) "Linux man-pages (unreleased)"
20 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
23 .RI ( libc ", " \-lc )
26 .B #include <sys/types.h>
29 .BI "struct passwd *getpwnam(const char *" name );
30 .BI "struct passwd *getpwuid(uid_t " uid );
32 .BI "int getpwnam_r(const char *restrict " name ", \
33 struct passwd *restrict " pwd ,
34 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
35 .BI " struct passwd **restrict " result );
36 .BI "int getpwuid_r(uid_t " uid ", struct passwd *restrict " pwd ,
37 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
38 .BI " struct passwd **restrict " result );
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
50 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
55 function returns a pointer to a structure containing
56 the broken-out fields of the record in the password database
57 (e.g., the local password file
60 that matches the username
65 function returns a pointer to a structure containing
66 the broken-out fields of the record in the password database
67 that matches the user ID
70 The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
75 char *pw_name; /* username */
76 char *pw_passwd; /* user password */
77 uid_t pw_uid; /* user ID */
78 gid_t pw_gid; /* group ID */
79 char *pw_gecos; /* user information */
80 char *pw_dir; /* home directory */
81 char *pw_shell; /* shell program */
88 for more information about these fields.
94 functions obtain the same information as
98 but store the retrieved
100 structure in the space pointed to by
102 The string fields pointed to by the members of the
104 structure are stored in the buffer
108 A pointer to the result (in case of success) or NULL (in case no entry
109 was found or an error occurred) is stored in
116 sysconf(_SC_GETPW_R_SIZE_MAX)
120 returns either \-1, without changing
122 or an initial suggested size for
124 (If this size is too small,
127 in which case the caller can retry with a larger buffer.)
133 functions return a pointer to a
135 structure, or NULL if the matching entry is not found or
139 is set to indicate the error.
140 If one wants to check
142 after the call, it should be set to zero before the call.
144 The return value may point to a static area, and may be overwritten
145 by subsequent calls to
150 (Do not pass the returned pointer to
161 If no matching password record was found,
162 these functions return 0 and store NULL in
164 In case of error, an error number is returned, and NULL is stored in
168 .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ..."
176 A signal was caught; see
183 The per-process limit on the number of open file descriptors has been reached.
186 The system-wide limit on the total number of open files has been reached.
190 Insufficient memory to allocate
193 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
196 Insufficient buffer space supplied.
200 local password database file
202 For an explanation of the terms used in this section, see
210 Interface Attribute Value
214 MT-Unsafe race:pwnam locale
219 MT-Unsafe race:pwuid locale
232 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
235 field is not specified in POSIX, but is present on most implementations.
237 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
238 It does not call "not found" an error, and hence does not specify what value
240 might have in this situation.
241 But that makes it impossible to recognize
243 One might argue that according to POSIX
245 should be left unchanged if an entry is not found.
246 Experiments on various
247 UNIX-like systems show that lots of different values occur in this
248 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others.
250 .\" AIX 5.1 - gives ESRCH
251 .\" OSF1 4.0g - gives EWOULDBLOCK
252 .\" libc, glibc up to glibc 2.6, Irix 6.5 - give ENOENT
253 .\" since glibc 2.7 - give 0
254 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
255 .\" SunOS 5.8 - gives EBADF
256 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
260 field contains the name of the initial working directory of the user.
261 Login programs use the value of this field to initialize the
263 environment variable for the login shell.
264 An application that wants to determine its user's home directory
265 should inspect the value of
267 (rather than the value
268 .IR getpwuid(getuid())\->pw_dir )
269 since this allows the user to modify their notion of
270 "the home directory" during a login session.
271 To determine the (initial) home directory of another user,
272 it is necessary to use
273 .I getpwnam("username")\->pw_dir
276 The program below demonstrates the use of
278 to find the full username and user ID for the username
279 supplied as a command-line argument.
281 .\" SRC BEGIN (getpwnam.c)
291 main(int argc, char *argv[])
294 struct passwd *result;
300 fprintf(stderr, "Usage: %s username\en", argv[0]);
304 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
305 if (bufsize == \-1) /* Value was indeterminate */
306 bufsize = 16384; /* Should be more than enough */
308 buf = malloc(bufsize);
314 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
315 if (result == NULL) {
317 printf("Not found\en");
320 perror("getpwnam_r");
325 printf("Name: %s; UID: %jd\en", pwd.pw_gecos,
326 (intmax_t) pwd.pw_uid);