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
208 Interface Attribute Value
216 MT-Unsafe race:pwnam locale
225 MT-Unsafe race:pwuid locale
241 field is not specified in POSIX, but is present on most implementations.
245 POSIX.1-2001, SVr4, 4.3BSD.
247 The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
248 It does not call "not found" an error, and hence does not specify what value
250 might have in this situation.
251 But that makes it impossible to recognize
253 One might argue that according to POSIX
255 should be left unchanged if an entry is not found.
256 Experiments on various
257 UNIX-like systems show that lots of different values occur in this
258 situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others.
260 .\" AIX 5.1 - gives ESRCH
261 .\" OSF1 4.0g - gives EWOULDBLOCK
262 .\" libc, glibc up to glibc 2.6, Irix 6.5 - give ENOENT
263 .\" since glibc 2.7 - give 0
264 .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
265 .\" SunOS 5.8 - gives EBADF
266 .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
270 field contains the name of the initial working directory of the user.
271 Login programs use the value of this field to initialize the
273 environment variable for the login shell.
274 An application that wants to determine its user's home directory
275 should inspect the value of
277 (rather than the value
278 .IR getpwuid(getuid())\->pw_dir )
279 since this allows the user to modify their notion of
280 "the home directory" during a login session.
281 To determine the (initial) home directory of another user,
282 it is necessary to use
283 .I getpwnam("username")\->pw_dir
286 The program below demonstrates the use of
288 to find the full username and user ID for the username
289 supplied as a command-line argument.
291 .\" SRC BEGIN (getpwnam.c)
301 main(int argc, char *argv[])
304 struct passwd *result;
310 fprintf(stderr, "Usage: %s username\en", argv[0]);
314 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
315 if (bufsize == \-1) /* Value was indeterminate */
316 bufsize = 16384; /* Should be more than enough */
318 buf = malloc(bufsize);
324 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
325 if (result == NULL) {
327 printf("Not found\en");
330 perror("getpwnam_r");
335 printf("Name: %s; UID: %jd\en", pwd.pw_gecos,
336 (intmax_t) pwd.pw_uid);