]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getpwnam.3
futex.2, netlink.3, stdin.3, wavelan.4, netlink.7: srcfix
[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
MK
4.\"
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
c13182ef 13.\"
fea681da
MK
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
c13182ef 21.\"
fea681da
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
24.\"
25.\" References consulted:
26.\" Linux libc source code
6b2fc294 27.\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
fea681da
MK
28.\" 386BSD man pages
29.\"
30.\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
31.\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
32.\" Modified 2003-11-15 by aeb
c6ec3685 33.\" 2008-11-07, mtk, Added an example program for getpwnam_r().
fea681da 34.\"
f6dc3611 35.TH GETPWNAM 3 2012-04-23 "GNU" "Linux Programmer's Manual"
fea681da
MK
36.SH NAME
37getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
38.SH SYNOPSIS
39.nf
40.B #include <sys/types.h>
41.B #include <pwd.h>
42.sp
43.BI "struct passwd *getpwnam(const char *" name );
44.sp
45.BI "struct passwd *getpwuid(uid_t " uid );
46.sp
3adc12e9 47.BI "int getpwnam_r(const char *" name ", struct passwd *" pwd ,
fea681da 48.br
3adc12e9 49.BI " char *" buf ", size_t " buflen ", struct passwd **" result );
fea681da 50.sp
3adc12e9 51.BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwd ,
fea681da 52.br
3adc12e9 53.BI " char *" buf ", size_t " buflen ", struct passwd **" result );
fea681da 54.fi
cc4615cc
MK
55.sp
56.in -4n
57Feature Test Macro Requirements for glibc (see
58.BR feature_test_macros (7)):
59.in
60.sp
61.ad l
62.BR getpwnam_r (),
63.BR getpwuid_r ():
f496f649 64.RS 4
0f200f07
MK
65_POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
66_SVID_SOURCE || _POSIX_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
MK
87The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
88.sp
bd191423 89.in +4n
fea681da
MK
90.nf
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
MK
99};
100.fi
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
MK
129The call
130
131 sysconf(_SC_GETPW_R_SIZE_MAX)
132
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.
156.LP
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).)
fea681da 165.LP
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
189A signal was caught.
190.TP
191.B EIO
192I/O error.
193.TP
194.B EMFILE
2f0af33b
MK
195The maximum number
196.RB ( OPEN_MAX )
197of files was open already in the calling process.
fea681da
MK
198.TP
199.B ENFILE
200The maximum number of files was open already in the system.
201.TP
202.B ENOMEM
f2738b39 203.\" not in POSIX
b2133414
MK
204Insufficient memory to allocate
205.I passwd
206structure.
fea681da
MK
207.\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
208.TP
209.B ERANGE
210Insufficient buffer space supplied.
211.SH FILES
212.TP
213.I /etc/passwd
f2738b39 214local password database file
47297adb 215.SH CONFORMING TO
44a2c328 216SVr4, 4.3BSD, POSIX.1-2001.
958680cd
MK
217The
218.I pw_gecos
219field is not specified in POSIX, but is present on most implementations.
fea681da 220.SH NOTES
68e1685c 221The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
6b2fc294 222It does not call "not found" an error, and hence does not specify what value
fea681da 223.I errno
c13182ef
MK
224might have in this situation.
225But that makes it impossible to recognize
226errors.
227One might argue that according to POSIX
fea681da 228.I errno
c13182ef
MK
229should be left unchanged if an entry is not found.
230Experiments on various
008f1ecc 231UNIX-like systems show that lots of different values occur in this
fea681da
MK
232situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
233.\" more precisely:
234.\" AIX 5.1 - gives ESRCH
235.\" OSF1 4.0g - gives EWOULDBLOCK
db85058b
MK
236.\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT
237.\" glibc since version 2.7 - give 0
fea681da
MK
238.\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
239.\" SunOS 5.8 - gives EBADF
240.\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
6b2fc294 241
c13182ef
MK
242The
243.I pw_dir
244field contains the name of the initial working directory of the user.
2f0af33b
MK
245Login programs use the value of this field to initialize the
246.B HOME
247environment variable for the login shell.
6b2fc294 248An application that wants to determine its user's home directory
2f0af33b
MK
249should inspect the value of
250.B HOME
251(rather than the value
94e9d9fe 252.IR getpwuid(getuid())\->pw_dir )
6b2fc294
MK
253since this allows the user to modify their notion of
254"the home directory" during a login session.
255To determine the (initial) home directory of another user,
c13182ef 256it is necessary to use
94e9d9fe 257.I getpwnam("username")\->pw_dir
6b2fc294 258or similar.
c6ec3685
MK
259.SH EXAMPLE
260The program below demonstrates the use of
261.BR getpwnam_r ()
262to find the full username and user ID for the username
263supplied as a command-line argument.
264
265.nf
266#include <pwd.h>
267#include <stdio.h>
268#include <stdlib.h>
269#include <unistd.h>
270#include <errno.h>
271
272int
273main(int argc, char *argv[])
274{
275 struct passwd pwd;
276 struct passwd *result;
277 char *buf;
278 size_t bufsize;
279 int s;
280
281 if (argc != 2) {
282 fprintf(stderr, "Usage: %s username\\n", argv[0]);
283 exit(EXIT_FAILURE);
284 }
285
286 bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
287 if (bufsize == \-1) /* Value was indeterminate */
288 bufsize = 16384; /* Should be more than enough */
289
290 buf = malloc(bufsize);
291 if (buf == NULL) {
292 perror("malloc");
293 exit(EXIT_FAILURE);
294 }
295
296 s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
297 if (result == NULL) {
298 if (s == 0)
299 printf("Not found\\n");
300 else {
301 errno = s;
302 perror("getpwnam_r");
303 }
304 exit(EXIT_FAILURE);
305 }
306
42afc407 307 printf("Name: %s; UID: %ld\\n", pwd.pw_gecos, (long) pwd.pw_uid);
c6ec3685
MK
308 exit(EXIT_SUCCESS);
309}
310.fi
47297adb 311.SH SEE ALSO
fea681da
MK
312.BR endpwent (3),
313.BR fgetpwent (3),
314.BR getgrnam (3),
315.BR getpw (3),
316.BR getpwent (3),
1ed50462 317.BR getspnam (3),
fea681da
MK
318.BR putpwent (3),
319.BR setpwent (3),
320.BR passwd (5)