]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getpwnam.3
fuse.4: ffix
[thirdparty/man-pages.git] / man3 / getpwnam.3
CommitLineData
fea681da 1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
c6ec3685
MK
2.\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk
3.\" <mtk.manpages@gmail.com>
fea681da 4.\"
93015253 5.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
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.
9.\"
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.
c13182ef 14.\"
fea681da
MK
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
21.\" professionally.
c13182ef 22.\"
fea681da
MK
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 25.\" %%%LICENSE_END
fea681da
MK
26.\"
27.\" References consulted:
28.\" Linux libc source code
6b2fc294 29.\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
fea681da
MK
30.\" 386BSD man pages
31.\"
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
c6ec3685 35.\" 2008-11-07, mtk, Added an example program for getpwnam_r().
fea681da 36.\"
4b8c67d9 37.TH GETPWNAM 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da
MK
38.SH NAME
39getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
40.SH SYNOPSIS
41.nf
42.B #include <sys/types.h>
43.B #include <pwd.h>
68e4db0a 44.PP
fea681da 45.BI "struct passwd *getpwnam(const char *" name );
68e4db0a 46.PP
fea681da 47.BI "struct passwd *getpwuid(uid_t " uid );
68e4db0a 48.PP
3adc12e9 49.BI "int getpwnam_r(const char *" name ", struct passwd *" pwd ,
ae577310 50.BI " char *" buf ", size_t " buflen ", struct passwd **" result );
68e4db0a 51.PP
3adc12e9 52.BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd ,
ae577310 53.BI " char *" buf ", size_t " buflen ", struct passwd **" result );
fea681da 54.fi
68e4db0a 55.PP
cc4615cc
MK
56.in -4n
57Feature Test Macro Requirements for glibc (see
58.BR feature_test_macros (7)):
59.in
68e4db0a 60.PP
cc4615cc
MK
61.ad l
62.BR getpwnam_r (),
63.BR getpwuid_r ():
f496f649 64.RS 4
9db90c7c
MK
65_POSIX_C_SOURCE
66 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
f496f649 67.RE
cc4615cc 68.ad b
fea681da
MK
69.SH DESCRIPTION
70The
63aa9df0 71.BR getpwnam ()
fea681da 72function returns a pointer to a structure containing
f936cf26 73the broken-out fields of the record in the password database
c13182ef 74(e.g., the local password file
f2738b39
MK
75.IR /etc/passwd ,
76NIS, and LDAP)
18701562 77that matches the username
fea681da
MK
78.IR name .
79.PP
80The
63aa9df0 81.BR getpwuid ()
fea681da 82function returns a pointer to a structure containing
f2738b39 83the broken-out fields of the record in the password database
f936cf26 84that matches the user ID
fea681da
MK
85.IR uid .
86.PP
fea681da 87The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
51f5698d 88.PP
bd191423 89.in +4n
b8302363 90.EX
fea681da 91struct passwd {
18701562 92 char *pw_name; /* username */
f2738b39 93 char *pw_passwd; /* user password */
34c97781
MK
94 uid_t pw_uid; /* user ID */
95 gid_t pw_gid; /* group ID */
958680cd 96 char *pw_gecos; /* user information */
f2738b39
MK
97 char *pw_dir; /* home directory */
98 char *pw_shell; /* shell program */
fea681da 99};
b8302363 100.EE
bd191423 101.in
fea681da 102.PP
011520b8
MK
103See
104.BR passwd (5)
105for more information about these fields.
106.PP
107The
108.BR getpwnam_r ()
109and
110.BR getpwuid_r ()
111functions obtain the same information as
112.BR getpwnam ()
113and
114.BR getpwuid (),
115but store the retrieved
116.I passwd
117structure in the space pointed to by
118.IR pwd .
119The string fields pointed to by the members of the
120.I passwd
121structure are stored in the buffer
122.I buf
123of size
124.IR buflen .
125A pointer to the result (in case of success) or NULL (in case no entry
126was found or an error occurred) is stored in
127.IR *result .
128.PP
f6dc3611 129The call
847e0d88 130.PP
f6dc3611 131 sysconf(_SC_GETPW_R_SIZE_MAX)
847e0d88 132.PP
f6dc3611
MK
133returns either \-1, without changing
134.IR errno ,
135or an initial suggested size for
136.IR buf .
137(If this size is too small,
138the call fails with
139.BR ERANGE ,
140in which case the caller can retry with a larger buffer.)
47297adb 141.SH RETURN VALUE
60a90ecd
MK
142The
143.BR getpwnam ()
144and
145.BR getpwuid ()
146functions return a pointer to a
f2738b39
MK
147.I passwd
148structure, or NULL if the matching entry is not found or
149an error occurs.
150If an error occurs,
fea681da 151.I errno
f2738b39
MK
152is set appropriately.
153If one wants to check
fea681da
MK
154.I errno
155after the call, it should be set to zero before the call.
dd3568a1 156.PP
57c1b002 157The return value may point to a static area, and may be overwritten
fea681da 158by subsequent calls to
3a72373c 159.BR getpwent (3),
63aa9df0 160.BR getpwnam (),
fea681da 161or
63aa9df0 162.BR getpwuid ().
48e4cfa0
MK
163(Do not pass the returned pointer to
164.BR free (3).)
dd3568a1 165.PP
bbab485d 166On success,
60a90ecd
MK
167.BR getpwnam_r ()
168and
169.BR getpwuid_r ()
bbab485d 170return zero, and set
3adc12e9 171.IR *result
bbab485d 172to
3adc12e9 173.IR pwd .
bbab485d
MK
174If no matching password record was found,
175these functions return 0 and store NULL in
3adc12e9 176.IR *result .
bbab485d 177In case of error, an error number is returned, and NULL is stored in
3adc12e9 178.IR *result .
fea681da
MK
179.SH ERRORS
180.TP
181.BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
182The given
183.I name
184or
185.I uid
186was not found.
187.TP
188.B EINTR
bb14af39
MK
189A signal was caught; see
190.BR signal (7).
fea681da
MK
191.TP
192.B EIO
193I/O error.
194.TP
195.B EMFILE
26c32fab 196The per-process limit on the number of open file descriptors has been reached.
fea681da
MK
197.TP
198.B ENFILE
e258766b 199The system-wide limit on the total number of open files has been reached.
fea681da
MK
200.TP
201.B ENOMEM
f2738b39 202.\" not in POSIX
b2133414
MK
203Insufficient memory to allocate
204.I passwd
205structure.
fea681da
MK
206.\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
207.TP
208.B ERANGE
209Insufficient buffer space supplied.
210.SH FILES
211.TP
212.I /etc/passwd
f2738b39 213local password database file
404197c7 214.SH ATTRIBUTES
2973863a
MK
215For an explanation of the terms used in this section, see
216.BR attributes (7).
217.TS
218allbox;
693e6cf5 219lb lb lb
2973863a
MK
220l l l.
221Interface Attribute Value
222T{
693e6cf5
MS
223.BR getpwnam ()
224T} Thread safety MT-Unsafe race:pwnam locale
225T{
404197c7 226.BR getpwuid ()
693e6cf5 227T} Thread safety MT-Unsafe race:pwuid locale
2973863a
MK
228T{
229.BR getpwnam_r (),
693e6cf5 230.br
404197c7 231.BR getpwuid_r ()
693e6cf5 232T} Thread safety MT-Safe locale
2973863a 233.TE
47297adb 234.SH CONFORMING TO
9720f05e 235POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
958680cd
MK
236The
237.I pw_gecos
238field is not specified in POSIX, but is present on most implementations.
fea681da 239.SH NOTES
68e1685c 240The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
6b2fc294 241It does not call "not found" an error, and hence does not specify what value
fea681da 242.I errno
c13182ef
MK
243might have in this situation.
244But that makes it impossible to recognize
245errors.
246One might argue that according to POSIX
fea681da 247.I errno
c13182ef
MK
248should be left unchanged if an entry is not found.
249Experiments on various
008f1ecc 250UNIX-like systems show that lots of different values occur in this
a797afac 251situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others.
fea681da
MK
252.\" more precisely:
253.\" AIX 5.1 - gives ESRCH
254.\" OSF1 4.0g - gives EWOULDBLOCK
db85058b
MK
255.\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
256.\" glibc since version 2.7 - give 0
fea681da
MK
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
847e0d88 260.PP
c13182ef
MK
261The
262.I pw_dir
263field contains the name of the initial working directory of the user.
2f0af33b
MK
264Login programs use the value of this field to initialize the
265.B HOME
266environment variable for the login shell.
6b2fc294 267An application that wants to determine its user's home directory
2f0af33b
MK
268should inspect the value of
269.B HOME
270(rather than the value
94e9d9fe 271.IR getpwuid(getuid())\->pw_dir )
6b2fc294
MK
272since this allows the user to modify their notion of
273"the home directory" during a login session.
274To determine the (initial) home directory of another user,
c13182ef 275it is necessary to use
94e9d9fe 276.I getpwnam("username")\->pw_dir
6b2fc294 277or similar.
c6ec3685
MK
278.SH EXAMPLE
279The program below demonstrates the use of
280.BR getpwnam_r ()
281to find the full username and user ID for the username
282supplied as a command-line argument.
847e0d88 283.PP
207050fa 284.EX
c6ec3685
MK
285#include <pwd.h>
286#include <stdio.h>
287#include <stdlib.h>
288#include <unistd.h>
289#include <errno.h>
290
291int
292main(int argc, char *argv[])
293{
294 struct passwd pwd;
295 struct passwd *result;
296 char *buf;
297 size_t bufsize;
298 int s;
299
300 if (argc != 2) {
301 fprintf(stderr, "Usage: %s username\\n", argv[0]);
302 exit(EXIT_FAILURE);
303 }
304
305 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
306 if (bufsize == \-1) /* Value was indeterminate */
307 bufsize = 16384; /* Should be more than enough */
308
309 buf = malloc(bufsize);
310 if (buf == NULL) {
311 perror("malloc");
312 exit(EXIT_FAILURE);
313 }
314
315 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
316 if (result == NULL) {
317 if (s == 0)
318 printf("Not found\\n");
319 else {
320 errno = s;
321 perror("getpwnam_r");
322 }
323 exit(EXIT_FAILURE);
324 }
325
42afc407 326 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);
c6ec3685
MK
327 exit(EXIT_SUCCESS);
328}
207050fa 329.EE
47297adb 330.SH SEE ALSO
fea681da
MK
331.BR endpwent (3),
332.BR fgetpwent (3),
333.BR getgrnam (3),
334.BR getpw (3),
335.BR getpwent (3),
1ed50462 336.BR getspnam (3),
fea681da
MK
337.BR putpwent (3),
338.BR setpwent (3),
339.BR passwd (5)