tzfile.5, tzselect.8: sync from tzdb upstream
[man-pages.git] / man3 / getpwnam.3
blob130110552fd38dc3cc6c5524c5edb77cdb965d20
1 '\" t
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>
5 .\"
6 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .\"
8 .\" References consulted:
9 .\"     Linux libc source code
10 .\"     Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
11 .\"     386BSD man pages
12 .\"
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().
17 .\"
18 .TH getpwnam 3 (date) "Linux man-pages (unreleased)"
19 .SH NAME
20 getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
21 .SH LIBRARY
22 Standard C library
23 .RI ( libc ", " \-lc )
24 .SH SYNOPSIS
25 .nf
26 .B #include <sys/types.h>
27 .B #include <pwd.h>
28 .PP
29 .BI "struct passwd *getpwnam(const char *" name );
30 .BI "struct passwd *getpwuid(uid_t " uid );
31 .PP
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 );
39 .fi
40 .PP
41 .RS -4
42 Feature Test Macro Requirements for glibc (see
43 .BR feature_test_macros (7)):
44 .RE
45 .PP
46 .BR getpwnam_r (),
47 .BR getpwuid_r ():
48 .nf
49     _POSIX_C_SOURCE
50         || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
51 .fi
52 .SH DESCRIPTION
53 The
54 .BR getpwnam ()
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
58 .IR /etc/passwd ,
59 NIS, and LDAP)
60 that matches the username
61 .IR name .
62 .PP
63 The
64 .BR getpwuid ()
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
68 .IR uid .
69 .PP
70 The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
71 .PP
72 .in +4n
73 .EX
74 struct passwd {
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 */
83 .EE
84 .in
85 .PP
86 See
87 .BR passwd (5)
88 for more information about these fields.
89 .PP
90 The
91 .BR getpwnam_r ()
92 and
93 .BR getpwuid_r ()
94 functions obtain the same information as
95 .BR getpwnam ()
96 and
97 .BR getpwuid (),
98 but store the retrieved
99 .I passwd
100 structure in the space pointed to by
101 .IR pwd .
102 The string fields pointed to by the members of the
103 .I passwd
104 structure are stored in the buffer
105 .I buf
106 of size
107 .IR buflen .
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
110 .IR *result .
112 The call
114 .in +4n
116 sysconf(_SC_GETPW_R_SIZE_MAX)
120 returns either \-1, without changing
121 .IR errno ,
122 or an initial suggested size for
123 .IR buf .
124 (If this size is too small,
125 the call fails with
126 .BR ERANGE ,
127 in which case the caller can retry with a larger buffer.)
128 .SH RETURN VALUE
130 .BR getpwnam ()
132 .BR getpwuid ()
133 functions return a pointer to a
134 .I passwd
135 structure, or NULL if the matching entry is not found or
136 an error occurs.
137 If an error occurs,
138 .I errno
139 is set to indicate the error.
140 If one wants to check
141 .I errno
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
146 .BR getpwent (3),
147 .BR getpwnam (),
149 .BR getpwuid ().
150 (Do not pass the returned pointer to
151 .BR free (3).)
153 On success,
154 .BR getpwnam_r ()
156 .BR getpwuid_r ()
157 return zero, and set
158 .I *result
160 .IR pwd .
161 If no matching password record was found,
162 these functions return 0 and store NULL in
163 .IR *result .
164 In case of error, an error number is returned, and NULL is stored in
165 .IR *result .
166 .SH ERRORS
168 .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ..."
169 The given
170 .I name
172 .I uid
173 was not found.
175 .B EINTR
176 A signal was caught; see
177 .BR signal (7).
179 .B EIO
180 I/O error.
182 .B EMFILE
183 The per-process limit on the number of open file descriptors has been reached.
185 .B ENFILE
186 The system-wide limit on the total number of open files has been reached.
188 .B ENOMEM
189 .\" not in POSIX
190 Insufficient memory to allocate
191 .I passwd
192 structure.
193 .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
195 .B ERANGE
196 Insufficient buffer space supplied.
197 .SH FILES
199 .I /etc/passwd
200 local password database file
201 .SH ATTRIBUTES
202 For an explanation of the terms used in this section, see
203 .BR attributes (7).
204 .ad l
207 allbox;
208 lb lb lbx
209 l l l.
210 Interface       Attribute       Value
212 .BR getpwnam ()
213 T}      Thread safety   T{
214 MT-Unsafe race:pwnam locale
217 .BR getpwuid ()
218 T}      Thread safety   T{
219 MT-Unsafe race:pwuid locale
222 .BR getpwnam_r (),
223 .BR getpwuid_r ()
224 T}      Thread safety   T{
225 MT-Safe locale
230 .sp 1
231 .SH STANDARDS
232 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
234 .I pw_gecos
235 field is not specified in POSIX, but is present on most implementations.
236 .SH NOTES
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
239 .I errno
240 might have in this situation.
241 But that makes it impossible to recognize
242 errors.
243 One might argue that according to POSIX
244 .I errno
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.
249 .\" more precisely:
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
259 .I pw_dir
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
262 .B HOME
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
266 .B HOME
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
274 or similar.
275 .SH EXAMPLES
276 The program below demonstrates the use of
277 .BR getpwnam_r ()
278 to find the full username and user ID for the username
279 supplied as a command-line argument.
281 .\" SRC BEGIN (getpwnam.c)
283 #include <errno.h>
284 #include <pwd.h>
285 #include <stdint.h>
286 #include <stdio.h>
287 #include <stdlib.h>
288 #include <unistd.h>
291 main(int argc, char *argv[])
293     struct passwd pwd;
294     struct passwd *result;
295     char *buf;
296     long bufsize;
297     int s;
299     if (argc != 2) {
300         fprintf(stderr, "Usage: %s username\en", argv[0]);
301         exit(EXIT_FAILURE);
302     }
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);
309     if (buf == NULL) {
310         perror("malloc");
311         exit(EXIT_FAILURE);
312     }
314     s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
315     if (result == NULL) {
316         if (s == 0)
317             printf("Not found\en");
318         else {
319             errno = s;
320             perror("getpwnam_r");
321         }
322         exit(EXIT_FAILURE);
323     }
325     printf("Name: %s; UID: %jd\en", pwd.pw_gecos,
326            (intmax_t) pwd.pw_uid);
327     exit(EXIT_SUCCESS);
330 .\" SRC END
331 .SH SEE ALSO
332 .BR endpwent (3),
333 .BR fgetpwent (3),
334 .BR getgrnam (3),
335 .BR getpw (3),
336 .BR getpwent (3),
337 .BR getspnam (3),
338 .BR putpwent (3),
339 .BR setpwent (3),
340 .BR passwd (5)