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