Debian Codegraph @

upstream/debian-unstable/man3/getpwent_r.3

raw
1 '\" t
2 .\" Copyright 2003, Andries E. Brouwer <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\"
7 .TH getpwent_r 3 2026-02-14 "Linux man-pages 6.18"
8 .SH NAME
9 getpwent_r, fgetpwent_r \- get passwd file entry reentrantly
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .B #include <pwd.h>
16 .P
17 .BR "int getpwent_r(" "size_t size;"
18 .BI " struct passwd *restrict " pwbuf ,
19 .BI " char " buf "[restrict " size "], size_t " size ,
20 .BI " struct passwd **restrict " pwbufp );
21 .BR "int fgetpwent_r(" "size_t size;"
22 .BI " FILE *restrict " stream ", struct passwd *restrict " pwbuf ,
23 .BI " char " buf "[restrict " size "], size_t " size ,
24 .BI " struct passwd **restrict " pwbufp );
25 .fi
26 .P
27 .RS -4
28 Feature Test Macro Requirements for glibc (see
29 .BR feature_test_macros (7)):
30 .RE
31 .P
32 .BR getpwent_r (),
33 .nf
34 Since glibc 2.19:
35 _DEFAULT_SOURCE
36 glibc 2.19 and earlier:
37 _BSD_SOURCE || _SVID_SOURCE
38 .fi
39 .P
40 .BR fgetpwent_r ():
41 .nf
42 Since glibc 2.19:
43 _DEFAULT_SOURCE
44 glibc 2.19 and earlier:
45 _SVID_SOURCE
46 .fi
47 .SH DESCRIPTION
48 The functions
49 .BR getpwent_r ()
50 and
51 .BR fgetpwent_r ()
52 are the reentrant versions of
53 .BR getpwent (3)
54 and
55 .BR fgetpwent (3).
56 The former reads the next passwd entry from the stream initialized by
57 .BR setpwent (3).
58 The latter reads the next passwd entry from
59 .IR stream .
60 .P
61 The
62 .I passwd
63 structure is defined in
64 .I <pwd.h>
65 as follows:
66 .P
67 .in +4n
68 .EX
69 struct passwd {
70 char *pw_name; /* username */
71 char *pw_passwd; /* user password */
72 uid_t pw_uid; /* user ID */
73 gid_t pw_gid; /* group ID */
74 char *pw_gecos; /* user information */
75 char *pw_dir; /* home directory */
76 char *pw_shell; /* shell program */
77 };
78 .EE
79 .in
80 .P
81 For more information about the fields of this structure, see
82 .BR passwd (5).
83 .P
84 The nonreentrant functions return a pointer to static storage,
85 where this static storage contains further pointers to user
86 name, password, gecos field, home directory and shell.
87 The reentrant functions described here return all of that in
88 caller-provided buffers.
89 First of all there is the buffer
90 .I pwbuf
91 that can hold a
92 .IR "struct\ passwd" .
93 And next the buffer
94 .I buf
95 of size
96 .I size
97 that can hold additional strings.
98 The result of these functions, the
99 .I struct\ passwd
100 read from the stream,
101 is stored in the provided buffer
102 .IR *pwbuf ,
103 and a pointer to this
104 .I struct\ passwd
105 is returned in
106 .IR *pwbufp .
107 .SH RETURN VALUE
108 On success, these functions return 0 and
109 .I *pwbufp
110 is a pointer to the
111 .IR "struct\ passwd" .
112 On error, these functions return an error value and
113 .I *pwbufp
114 is NULL.
115 .SH ERRORS
116 .TP
117 .B ENOENT
118 No more entries.
119 .TP
120 .B ERANGE
121 Insufficient buffer space supplied.
122 Try again with larger buffer.
123 .SH ATTRIBUTES
124 For an explanation of the terms used in this section, see
125 .BR attributes (7).
126 .TS
127 allbox;
128 lb lb lbx
129 l l l.
130 Interface Attribute Value
131 T{
132 .na
133 .nh
134 .BR getpwent_r ()
135 T} Thread safety T{
136 .na
137 .nh
138 MT-Unsafe race:pwent locale
139 T}
140 T{
141 .na
142 .nh
143 .BR fgetpwent_r ()
144 T} Thread safety MT-Safe
145 .TE
146 .P
147 In the above table,
148 .I pwent
149 in
150 .I race:pwent
151 signifies that if any of the functions
152 .BR setpwent (),
153 .BR getpwent (),
154 .BR endpwent (),
155 or
156 .BR getpwent_r ()
157 are used in parallel in different threads of a program,
158 then data races could occur.
159 .SH VERSIONS
160 Other systems use the prototype
161 .P
162 .in +4n
163 .EX
164 struct passwd *
165 getpwent_r(struct passwd *pwd, char buf[.size], int size);
166 .EE
167 .in
168 .P
169 or, better,
170 .P
171 .in +4n
172 .EX
173 int
174 getpwent_r(struct passwd *pwd, char buf[.size], int size,
175 FILE **pw_fp);
176 .EE
177 .in
178 .SH STANDARDS
179 None.
180 .SH HISTORY
181 These functions are done in a style resembling
182 the POSIX version of functions like
183 .BR getpwnam_r (3).
184 .SH NOTES
185 The function
186 .BR getpwent_r ()
187 is not really reentrant since it shares the reading position
188 in the stream with all other threads.
189 .SH EXAMPLES
190 .\" SRC BEGIN (getpwent_r.c)
191 .EX
192 #define _GNU_SOURCE
193 #include <pwd.h>
194 #include <stdint.h>
195 #include <stdio.h>
196 #include <stdlib.h>
197 \&
198 #define BUFLEN 4096
199 \&
200 int
201 main(void)
202 {
203 struct passwd pw;
204 struct passwd *pwp;
205 char buf[BUFLEN];
206 int i;
207 \&
208 setpwent();
209 for (;;) {
210 i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
211 if (i)
212 break;
213 printf("%s (%jd)\[rs]tHOME %s\[rs]tSHELL %s\[rs]n", pwp\->pw_name,
214 (intmax_t) pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
215 }
216 endpwent();
217 exit(EXIT_SUCCESS);
218 }
219 .EE
220 .\" perhaps add error checking - should use strerror_r
221 .\" #include <errno.h>
222 .\" #include <stdlib.h>
223 .\" if (i) {
224 .\" if (i == ENOENT)
225 .\" break;
226 .\" printf("getpwent_r: %s", strerror(i));
227 .\" exit(EXIT_SUCCESS);
228 .\" }
229 .\" SRC END
230 .SH SEE ALSO
231 .BR fgetpwent (3),
232 .BR getpw (3),
233 .BR getpwent (3),
234 .BR getpwnam (3),
235 .BR getpwuid (3),
236 .BR putpwent (3),
237 .BR passwd (5)