]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getpwnam.3
sync
[thirdparty/man-pages.git] / man3 / getpwnam.3
CommitLineData
fea681da
MK
1.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2.\"
3.\" Permission is granted to make and distribute verbatim copies of this
4.\" manual provided the copyright notice and this permission notice are
5.\" preserved on all copies.
6.\"
7.\" Permission is granted to copy and distribute modified versions of this
8.\" manual under the conditions for verbatim copying, provided that the
9.\" entire resulting derived work is distributed under the terms of a
10.\" permission notice identical to this one.
c13182ef 11.\"
fea681da
MK
12.\" Since the Linux kernel and libraries are constantly changing, this
13.\" manual page may be incorrect or out-of-date. The author(s) assume no
14.\" responsibility for errors or omissions, or for damages resulting from
15.\" the use of the information contained herein. The author(s) may not
16.\" have taken the same level of care in the production of this manual,
17.\" which is licensed free of charge, as they might when working
18.\" professionally.
c13182ef 19.\"
fea681da
MK
20.\" Formatted or processed versions of this manual, if unaccompanied by
21.\" the source, must acknowledge the copyright and authors of this work.
22.\"
23.\" References consulted:
24.\" Linux libc source code
6b2fc294 25.\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991)
fea681da
MK
26.\" 386BSD man pages
27.\"
28.\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu)
29.\" Modified 1996-05-27 by Martin Schulze (joey@linux.de)
30.\" Modified 2003-11-15 by aeb
31.\"
32.TH GETPWNAM 3 1996-05-27 "GNU" "Linux Programmer's Manual"
33.SH NAME
34getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry
35.SH SYNOPSIS
36.nf
37.B #include <sys/types.h>
38.B #include <pwd.h>
39.sp
40.BI "struct passwd *getpwnam(const char *" name );
41.sp
42.BI "struct passwd *getpwuid(uid_t " uid );
43.sp
44.BI "int getpwnam_r(const char *" name ", struct passwd *" pwbuf ,
45.br
46.BI " char *" buf ", size_t " buflen ", struct passwd **" pwbufp );
47.sp
48.BI "int getpwuid_r(uid_t " uid ", struct passwd *" pwbuf ,
49.br
50.BI " char *" buf ", size_t " buflen ", struct passwd **" pwbufp );
51.fi
52.SH DESCRIPTION
53The
63aa9df0 54.BR getpwnam ()
fea681da 55function returns a pointer to a structure containing
f936cf26 56the broken-out fields of the record in the password database
c13182ef 57(e.g., the local password file
f2738b39
MK
58.IR /etc/passwd ,
59NIS, and LDAP)
f936cf26 60that matches the user name
fea681da
MK
61.IR name .
62.PP
63The
63aa9df0 64.BR getpwuid ()
fea681da 65function returns a pointer to a structure containing
f2738b39 66the broken-out fields of the record in the password database
f936cf26 67that matches the user ID
fea681da
MK
68.IR uid .
69.PP
70The
63aa9df0 71.BR getpwnam_r ()
fea681da 72and
63aa9df0 73.BR getpwuid_r ()
f2738b39
MK
74functions obtain the same information, but store the retrieved
75.I passwd
76structure in the space pointed to by
fea681da 77.IR pwbuf .
f2738b39
MK
78This
79.I passwd
80structure contains pointers to strings, and these strings
fea681da
MK
81are stored in the buffer
82.I buf
83of size
84.IR buflen .
85A pointer to the result (in case of success) or NULL (in case no entry
86was found or an error occurred) is stored in
87.RI * pwbufp .
88.PP
89The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows:
90.sp
f2738b39 91.RS 0.25i
fea681da
MK
92.nf
93struct passwd {
f2738b39
MK
94 char *pw_name; /* user name */
95 char *pw_passwd; /* user password */
34c97781
MK
96 uid_t pw_uid; /* user ID */
97 gid_t pw_gid; /* group ID */
f2738b39
MK
98 char *pw_gecos; /* real name */
99 char *pw_dir; /* home directory */
100 char *pw_shell; /* shell program */
fea681da
MK
101};
102.fi
103.RE
104.PP
105The maximum needed size for
106.I buf
107can be found using
108.BR sysconf (3)
109with the _SC_GETPW_R_SIZE_MAX parameter.
110.SH "RETURN VALUE"
63aa9df0 111The \fBgetpwnam\fP() and \fBgetpwuid\fP() functions return a pointer to a
f2738b39
MK
112.I passwd
113structure, or NULL if the matching entry is not found or
114an error occurs.
115If an error occurs,
fea681da 116.I errno
f2738b39
MK
117is set appropriately.
118If one wants to check
fea681da
MK
119.I errno
120after the call, it should be set to zero before the call.
121.LP
122The return value may point to static area, and may be overwritten
123by subsequent calls to
63aa9df0
MK
124.BR getpwent (),
125.BR getpwnam (),
fea681da 126or
63aa9df0 127.BR getpwuid ().
fea681da 128.LP
63aa9df0 129The \fBgetpwnam_r\fP() and \fBgetpwuid_r\fP() functions return
c13182ef
MK
130zero on success.
131In case of error, an error number is returned.
fea681da
MK
132.SH ERRORS
133.TP
134.BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ... "
135The given
136.I name
137or
138.I uid
139was not found.
140.TP
141.B EINTR
142A signal was caught.
143.TP
144.B EIO
145I/O error.
146.TP
147.B EMFILE
148The maximum number (OPEN_MAX) of files was open already in the calling process.
149.TP
150.B ENFILE
151The maximum number of files was open already in the system.
152.TP
153.B ENOMEM
f2738b39 154.\" not in POSIX
fea681da
MK
155Insufficient memory to allocate passwd structure.
156.\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45)
157.TP
158.B ERANGE
159Insufficient buffer space supplied.
160.SH FILES
161.TP
162.I /etc/passwd
f2738b39 163local password database file
fea681da
MK
164.fi
165.SH "CONFORMING TO"
68e1685c 166SVr4, 4.3BSD, POSIX.1-2001
fea681da 167.SH NOTES
68e1685c 168The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
6b2fc294 169It does not call "not found" an error, and hence does not specify what value
fea681da 170.I errno
c13182ef
MK
171might have in this situation.
172But that makes it impossible to recognize
173errors.
174One might argue that according to POSIX
fea681da 175.I errno
c13182ef
MK
176should be left unchanged if an entry is not found.
177Experiments on various
6b2fc294 178Unix-like systems show that lots of different values occur in this
fea681da
MK
179situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM and probably others.
180.\" more precisely:
181.\" AIX 5.1 - gives ESRCH
182.\" OSF1 4.0g - gives EWOULDBLOCK
183.\" libc, glibc, Irix 6.5 - give ENOENT
184.\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM
185.\" SunOS 5.8 - gives EBADF
186.\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0
6b2fc294 187
c13182ef
MK
188The
189.I pw_dir
190field contains the name of the initial working directory of the user.
6b2fc294
MK
191Login programs use the value of this field to initialize
192the HOME environment variable for the login shell.
193An application that wants to determine its user's home directory
194should inspect the value of HOME (rather than the value
195.IR getpwuid(getuid())->pw_dir )
196since this allows the user to modify their notion of
197"the home directory" during a login session.
198To determine the (initial) home directory of another user,
c13182ef
MK
199it is necessary to use
200.I getpwnam("username")->pw_dir
6b2fc294 201or similar.
fea681da
MK
202.SH "SEE ALSO"
203.BR endpwent (3),
204.BR fgetpwent (3),
205.BR getgrnam (3),
206.BR getpw (3),
207.BR getpwent (3),
208.BR putpwent (3),
209.BR setpwent (3),
210.BR passwd (5)