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