]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getpwnam.3
getcpu.2, sendfile.2, cmsg.3, rtnetlink.3, arp.7, ddp.7, fifo.7, icmp.7, ip.7, ipv6...
[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.\"
4b72fb64 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.\"
f6dc3611 37.TH GETPWNAM 3 2012-04-23 "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
3adc12e9 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
3adc12e9 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
47297adb 217.SH CONFORMING TO
44a2c328 218SVr4, 4.3BSD, POSIX.1-2001.
958680cd
MK
219The
220.I pw_gecos
221field is not specified in POSIX, but is present on most implementations.
fea681da 222.SH NOTES
68e1685c 223The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
6b2fc294 224It does not call "not found" an error, and hence does not specify what value
fea681da 225.I errno
c13182ef
MK
226might have in this situation.
227But that makes it impossible to recognize
228errors.
229One might argue that according to POSIX
fea681da 230.I errno
c13182ef
MK
231should be left unchanged if an entry is not found.
232Experiments on various
008f1ecc 233UNIX-like systems show that lots of different values occur in this
fea681da
MK
234situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
235.\" more precisely:
236.\" AIX 5.1 - gives ESRCH
237.\" OSF1 4.0g - gives EWOULDBLOCK
db85058b
MK
238.\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
239.\" glibc since version 2.7 - give 0
fea681da
MK
240.\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
241.\" SunOS 5.8 - gives EBADF
242.\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
6b2fc294 243
c13182ef
MK
244The
245.I pw_dir
246field contains the name of the initial working directory of the user.
2f0af33b
MK
247Login programs use the value of this field to initialize the
248.B HOME
249environment variable for the login shell.
6b2fc294 250An application that wants to determine its user's home directory
2f0af33b
MK
251should inspect the value of
252.B HOME
253(rather than the value
94e9d9fe 254.IR getpwuid(getuid())\->pw_dir )
6b2fc294
MK
255since this allows the user to modify their notion of
256"the home directory" during a login session.
257To determine the (initial) home directory of another user,
c13182ef 258it is necessary to use
94e9d9fe 259.I getpwnam("username")\->pw_dir
6b2fc294 260or similar.
c6ec3685
MK
261.SH EXAMPLE
262The program below demonstrates the use of
263.BR getpwnam_r ()
264to find the full username and user ID for the username
265supplied as a command-line argument.
266
267.nf
268#include <pwd.h>
269#include <stdio.h>
270#include <stdlib.h>
271#include <unistd.h>
272#include <errno.h>
273
274int
275main(int argc, char *argv[])
276{
277 struct passwd pwd;
278 struct passwd *result;
279 char *buf;
280 size_t bufsize;
281 int s;
282
283 if (argc != 2) {
284 fprintf(stderr, "Usage: %s username\\n", argv[0]);
285 exit(EXIT_FAILURE);
286 }
287
288 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
289 if (bufsize == \-1) /* Value was indeterminate */
290 bufsize = 16384; /* Should be more than enough */
291
292 buf = malloc(bufsize);
293 if (buf == NULL) {
294 perror("malloc");
295 exit(EXIT_FAILURE);
296 }
297
298 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
299 if (result == NULL) {
300 if (s == 0)
301 printf("Not found\\n");
302 else {
303 errno = s;
304 perror("getpwnam_r");
305 }
306 exit(EXIT_FAILURE);
307 }
308
42afc407 309 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);
c6ec3685
MK
310 exit(EXIT_SUCCESS);
311}
312.fi
47297adb 313.SH SEE ALSO
fea681da
MK
314.BR endpwent (3),
315.BR fgetpwent (3),
316.BR getgrnam (3),
317.BR getpw (3),
318.BR getpwent (3),
1ed50462 319.BR getspnam (3),
fea681da
MK
320.BR putpwent (3),
321.BR setpwent (3),
322.BR passwd (5)