]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getpwent_r.3
ld.so.8: srcfix
[thirdparty/man-pages.git] / man3 / getpwent_r.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl)
2.\"
1dd72f9c 3.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
4.\" This is free documentation; you can redistribute it and/or
5.\" modify it under the terms of the GNU General Public License as
6.\" published by the Free Software Foundation; either version 2 of
7.\" the License, or (at your option) any later version.
8.\"
9.\" The GNU General Public License's references to "object code"
10.\" and "executables" are to be interpreted as the output of any
11.\" document formatting or typesetting system, including
12.\" intermediate and printed output.
13.\"
14.\" This manual is distributed in the hope that it will be useful,
15.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
16.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17.\" GNU General Public License for more details.
18.\"
19.\" You should have received a copy of the GNU General Public
c715f741
MK
20.\" License along with this manual; if not, see
21.\" <http://www.gnu.org/licenses/>.
6a8d8745 22.\" %%%LICENSE_END
fea681da 23.\"
4b8c67d9 24.TH GETPWENT_R 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da
MK
25.SH NAME
26getpwent_r, fgetpwent_r \- get passwd file entry reentrantly
27.SH SYNOPSIS
28.nf
fea681da 29.B #include <pwd.h>
68e4db0a 30.PP
fea681da 31.BI "int getpwent_r(struct passwd *" pwbuf ", char *" buf ,
fea681da 32.BI " size_t " buflen ", struct passwd **" pwbufp );
68e4db0a 33.PP
495604c3 34.BI "int fgetpwent_r(FILE *" stream ", struct passwd *" pwbuf ", char *" buf ,
fea681da 35.BI " size_t " buflen ", struct passwd **" pwbufp );
cc4615cc 36.fi
68e4db0a 37.PP
cc4615cc
MK
38.in -4n
39Feature Test Macro Requirements for glibc (see
40.BR feature_test_macros (7)):
41.in
68e4db0a 42.PP
cc4615cc 43.BR getpwent_r (),
51c612fb
MK
44 Since glibc 2.19:
45 _DEFAULT_SOURCE
46 Glibc 2.19 and earlier:
47 _BSD_SOURCE || _SVID_SOURCE
cc4615cc
MK
48.br
49.BR fgetpwent_r ():
51c612fb
MK
50 Since glibc 2.19:
51 _DEFAULT_SOURCE
52 Glibc 2.19 and earlier:
53 _SVID_SOURCE
fea681da
MK
54.SH DESCRIPTION
55The functions
63aa9df0 56.BR getpwent_r ()
fea681da 57and
63aa9df0 58.BR fgetpwent_r ()
fea681da
MK
59are the reentrant versions of
60.BR getpwent (3)
61and
62.BR fgetpwent (3).
63The former reads the next passwd entry from the stream initialized by
64.BR setpwent (3).
ef3d4efe 65The latter reads the next passwd entry from
495604c3 66.IR stream .
fea681da
MK
67.PP
68The \fIpasswd\fP structure is defined in
69.I <pwd.h>
70as follows:
51f5698d 71.PP
bd191423 72.in +4n
b8302363 73.EX
fea681da 74struct passwd {
18701562 75 char *pw_name; /* username */
cf0a9ace
MK
76 char *pw_passwd; /* user password */
77 uid_t pw_uid; /* user ID */
78 gid_t pw_gid; /* group ID */
97724acd 79 char *pw_gecos; /* user information */
cf0a9ace
MK
80 char *pw_dir; /* home directory */
81 char *pw_shell; /* shell program */
fea681da 82};
b8302363 83.EE
bd191423 84.in
7f12f117
MK
85.PP
86For more information about the fields of this structure, see
87.BR passwd (5).
847e0d88 88.PP
54d75d6c 89The nonreentrant functions return a pointer to static storage,
fea681da
MK
90where this static storage contains further pointers to user
91name, password, gecos field, home directory and shell.
92The reentrant functions described here return all of that in
c13182ef
MK
93caller-provided buffers.
94First of all there is the buffer
fea681da 95.I pwbuf
0c2ec4f1 96that can hold a \fIstruct passwd\fP.
c13182ef 97And next the buffer
fea681da
MK
98.I buf
99of size
100.I buflen
101that can hold additional strings.
0c2ec4f1 102The result of these functions, the \fIstruct passwd\fP read from the stream,
fea681da 103is stored in the provided buffer
bd9b2a9c 104.IR *pwbuf ,
0c2ec4f1 105and a pointer to this \fIstruct passwd\fP is returned in
bd9b2a9c 106.IR *pwbufp .
47297adb 107.SH RETURN VALUE
fea681da 108On success, these functions return 0 and
bd9b2a9c 109.I *pwbufp
0c2ec4f1 110is a pointer to the \fIstruct passwd\fP.
fea681da 111On error, these functions return an error value and
bd9b2a9c 112.I *pwbufp
fea681da
MK
113is NULL.
114.SH ERRORS
115.TP
116.B ENOENT
117No more entries.
118.TP
119.B ERANGE
c13182ef
MK
120Insufficient buffer space supplied.
121Try again with larger buffer.
98a731cd
ZL
122.SH ATTRIBUTES
123For an explanation of the terms used in this section, see
124.BR attributes (7).
125.TS
126allbox;
127lb lb lbw27
128l l l.
129Interface Attribute Value
130T{
131.BR getpwent_r ()
132T} Thread safety MT-Unsafe race:pwent locale
133T{
134.BR fgetpwent_r ()
135T} Thread safety MT-Safe
136.TE
847e0d88 137.sp 1
98a731cd
ZL
138In the above table,
139.I pwent
140in
141.I race:pwent
142signifies that if any of the functions
143.BR setpwent (),
144.BR getpwent (),
145.BR endpwent (),
146or
147.BR getpwent_r ()
148are used in parallel in different threads of a program,
149then data races could occur.
47297adb 150.SH CONFORMING TO
2b2581ee
MK
151These functions are GNU extensions, done in a style resembling
152the POSIX version of functions like
153.BR getpwnam_r (3).
b5606b27 154Other systems use the prototype
51f5698d 155.PP
088a639b 156.in +4n
b8302363 157.EX
2b2581ee
MK
158struct passwd *
159getpwent_r(struct passwd *pwd, char *buf, int buflen);
b8302363 160.EE
bdd915e2 161.in
51f5698d 162.PP
2b2581ee 163or, better,
51f5698d 164.PP
088a639b 165.in +4n
b8302363 166.EX
2b2581ee
MK
167int
168getpwent_r(struct passwd *pwd, char *buf, int buflen,
169 FILE **pw_fp);
b8302363 170.EE
e646a1ba 171.in
2b2581ee
MK
172.SH NOTES
173The function
174.BR getpwent_r ()
175is not really reentrant since it shares the reading position
176in the stream with all other threads.
fea681da 177.SH EXAMPLE
bdd915e2 178.EX
fea681da
MK
179#define _GNU_SOURCE
180#include <pwd.h>
181#include <stdio.h>
182#define BUFLEN 4096
183
c13182ef
MK
184int
185main(void)
cf0a9ace
MK
186{
187 struct passwd pw, *pwp;
188 char buf[BUFLEN];
189 int i;
fea681da 190
cf0a9ace
MK
191 setpwent();
192 while (1) {
193 i = getpwent_r(&pw, buf, BUFLEN, &pwp);
194 if (i)
195 break;
31a6818e 196 printf("%s (%d)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
29059a65 197 pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
cf0a9ace
MK
198 }
199 endpwent();
5bc8c34c 200 exit(EXIT_SUCCESS);
fea681da 201}
bdd915e2 202.EE
fea681da
MK
203.\" perhaps add error checking - should use strerror_r
204.\" #include <errno.h>
205.\" #include <stdlib.h>
206.\" if (i) {
207.\" if (i == ENOENT)
208.\" break;
209.\" printf("getpwent_r: %s", strerror(i));
4c44ffe5 210.\" exit(EXIT_SUCCESS);
fea681da 211.\" }
47297adb 212.SH SEE ALSO
fea681da
MK
213.BR fgetpwent (3),
214.BR getpw (3),
215.BR getpwent (3),
216.BR getpwnam (3),
217.BR getpwuid (3),
218.BR putpwent (3),
cc4615cc 219.BR passwd (5)