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