]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getpwnam.3
getauxval.3: wfix
[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.\"
460495ca 37.TH GETPWNAM 3 2015-08-08 "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
MK
67_POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
68_SVID_SOURCE || _POSIX_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
191A signal was caught.
192.TP
193.B EIO
194I/O error.
195.TP
196.B EMFILE
2f0af33b
MK
197The maximum number
198.RB ( OPEN_MAX )
199of files was open already in the calling process.
fea681da
MK
200.TP
201.B ENFILE
202The maximum number of files was open already in the system.
203.TP
204.B ENOMEM
f2738b39 205.\" not in POSIX
b2133414
MK
206Insufficient memory to allocate
207.I passwd
208structure.
fea681da
MK
209.\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
210.TP
211.B ERANGE
212Insufficient buffer space supplied.
213.SH FILES
214.TP
215.I /etc/passwd
f2738b39 216local password database file
404197c7 217.SH ATTRIBUTES
2973863a
MK
218For an explanation of the terms used in this section, see
219.BR attributes (7).
220.TS
221allbox;
693e6cf5 222lb lb lb
2973863a
MK
223l l l.
224Interface Attribute Value
225T{
693e6cf5
MS
226.BR getpwnam ()
227T} Thread safety MT-Unsafe race:pwnam locale
228T{
404197c7 229.BR getpwuid ()
693e6cf5 230T} Thread safety MT-Unsafe race:pwuid locale
2973863a
MK
231T{
232.BR getpwnam_r (),
693e6cf5 233.br
404197c7 234.BR getpwuid_r ()
693e6cf5 235T} Thread safety MT-Safe locale
2973863a 236.TE
47297adb 237.SH CONFORMING TO
9720f05e 238POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
958680cd
MK
239The
240.I pw_gecos
241field is not specified in POSIX, but is present on most implementations.
fea681da 242.SH NOTES
68e1685c 243The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
6b2fc294 244It does not call "not found" an error, and hence does not specify what value
fea681da 245.I errno
c13182ef
MK
246might have in this situation.
247But that makes it impossible to recognize
248errors.
249One might argue that according to POSIX
fea681da 250.I errno
c13182ef
MK
251should be left unchanged if an entry is not found.
252Experiments on various
008f1ecc 253UNIX-like systems show that lots of different values occur in this
a797afac 254situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others.
fea681da
MK
255.\" more precisely:
256.\" AIX 5.1 - gives ESRCH
257.\" OSF1 4.0g - gives EWOULDBLOCK
db85058b
MK
258.\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
259.\" glibc since version 2.7 - give 0
fea681da
MK
260.\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
261.\" SunOS 5.8 - gives EBADF
262.\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
6b2fc294 263
c13182ef
MK
264The
265.I pw_dir
266field contains the name of the initial working directory of the user.
2f0af33b
MK
267Login programs use the value of this field to initialize the
268.B HOME
269environment variable for the login shell.
6b2fc294 270An application that wants to determine its user's home directory
2f0af33b
MK
271should inspect the value of
272.B HOME
273(rather than the value
94e9d9fe 274.IR getpwuid(getuid())\->pw_dir )
6b2fc294
MK
275since this allows the user to modify their notion of
276"the home directory" during a login session.
277To determine the (initial) home directory of another user,
c13182ef 278it is necessary to use
94e9d9fe 279.I getpwnam("username")\->pw_dir
6b2fc294 280or similar.
c6ec3685
MK
281.SH EXAMPLE
282The program below demonstrates the use of
283.BR getpwnam_r ()
284to find the full username and user ID for the username
285supplied as a command-line argument.
286
287.nf
288#include <pwd.h>
289#include <stdio.h>
290#include <stdlib.h>
291#include <unistd.h>
292#include <errno.h>
293
294int
295main(int argc, char *argv[])
296{
297 struct passwd pwd;
298 struct passwd *result;
299 char *buf;
300 size_t bufsize;
301 int s;
302
303 if (argc != 2) {
304 fprintf(stderr, "Usage: %s username\\n", argv[0]);
305 exit(EXIT_FAILURE);
306 }
307
308 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
309 if (bufsize == \-1) /* Value was indeterminate */
310 bufsize = 16384; /* Should be more than enough */
311
312 buf = malloc(bufsize);
313 if (buf == NULL) {
314 perror("malloc");
315 exit(EXIT_FAILURE);
316 }
317
318 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
319 if (result == NULL) {
320 if (s == 0)
321 printf("Not found\\n");
322 else {
323 errno = s;
324 perror("getpwnam_r");
325 }
326 exit(EXIT_FAILURE);
327 }
328
42afc407 329 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);
c6ec3685
MK
330 exit(EXIT_SUCCESS);
331}
332.fi
47297adb 333.SH SEE ALSO
fea681da
MK
334.BR endpwent (3),
335.BR fgetpwent (3),
336.BR getgrnam (3),
337.BR getpw (3),
338.BR getpwent (3),
1ed50462 339.BR getspnam (3),
fea681da
MK
340.BR putpwent (3),
341.BR setpwent (3),
342.BR passwd (5)