ioctl_tty.2: srcfix
[man-pages.git] / man3 / getpwent_r.3
blob3c90a9b7b60d66c74f1df0dfbcd18cec04fb7e06
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 2021-03-22 "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 *restrict " pwbuf ,
32 .BI "               char *restrict " buf ", size_t " buflen ,
33 .BI "               struct passwd **restrict " pwbufp );
34 .BI "int fgetpwent_r(FILE *restrict " stream \
35 ", struct passwd *restrict " pwbuf ,
36 .BI "               char *restrict " buf ", size_t " buflen ,
37 .BI "               struct passwd **restrict " pwbufp );
38 .fi
39 .PP
40 .RS -4
41 Feature Test Macro Requirements for glibc (see
42 .BR feature_test_macros (7)):
43 .RE
44 .PP
45 .BR getpwent_r (),
46 .nf
47     Since glibc 2.19:
48         _DEFAULT_SOURCE
49     Glibc 2.19 and earlier:
50         _BSD_SOURCE || _SVID_SOURCE
51 .fi
52 .PP
53 .BR fgetpwent_r ():
54 .nf
55     Since glibc 2.19:
56         _DEFAULT_SOURCE
57     Glibc 2.19 and earlier:
58         _SVID_SOURCE
59 .fi
60 .SH DESCRIPTION
61 The functions
62 .BR getpwent_r ()
63 and
64 .BR fgetpwent_r ()
65 are the reentrant versions of
66 .BR getpwent (3)
67 and
68 .BR fgetpwent (3).
69 The former reads the next passwd entry from the stream initialized by
70 .BR setpwent (3).
71 The latter reads the next passwd entry from
72 .IR stream .
73 .PP
74 The \fIpasswd\fP structure is defined in
75 .I <pwd.h>
76 as follows:
77 .PP
78 .in +4n
79 .EX
80 struct passwd {
81     char    *pw_name;      /* username */
82     char    *pw_passwd;    /* user password */
83     uid_t    pw_uid;       /* user ID */
84     gid_t    pw_gid;       /* group ID */
85     char    *pw_gecos;     /* user information */
86     char    *pw_dir;       /* home directory */
87     char    *pw_shell;     /* shell program */
89 .EE
90 .in
91 .PP
92 For more information about the fields of this structure, see
93 .BR passwd (5).
94 .PP
95 The nonreentrant functions return a pointer to static storage,
96 where this static storage contains further pointers to user
97 name, password, gecos field, home directory and shell.
98 The reentrant functions described here return all of that in
99 caller-provided buffers.
100 First of all there is the buffer
101 .I pwbuf
102 that can hold a \fIstruct passwd\fP.
103 And next the buffer
104 .I buf
105 of size
106 .I buflen
107 that can hold additional strings.
108 The result of these functions, the \fIstruct passwd\fP read from the stream,
109 is stored in the provided buffer
110 .IR *pwbuf ,
111 and a pointer to this \fIstruct passwd\fP is returned in
112 .IR *pwbufp .
113 .SH RETURN VALUE
114 On success, these functions return 0 and
115 .I *pwbufp
116 is a pointer to the \fIstruct passwd\fP.
117 On error, these functions return an error value and
118 .I *pwbufp
119 is NULL.
120 .SH ERRORS
122 .B ENOENT
123 No more entries.
125 .B ERANGE
126 Insufficient buffer space supplied.
127 Try again with larger buffer.
128 .SH ATTRIBUTES
129 For an explanation of the terms used in this section, see
130 .BR attributes (7).
131 .ad l
134 allbox;
135 lb lb lbx
136 l l l.
137 Interface       Attribute       Value
139 .BR getpwent_r ()
140 T}      Thread safety   T{
141 MT-Unsafe race:pwent locale
144 .BR fgetpwent_r ()
145 T}      Thread safety   MT-Safe
149 .sp 1
150 In the above table,
151 .I pwent
153 .I race:pwent
154 signifies that if any of the functions
155 .BR setpwent (),
156 .BR getpwent (),
157 .BR endpwent (),
159 .BR getpwent_r ()
160 are used in parallel in different threads of a program,
161 then data races could occur.
162 .SH CONFORMING TO
163 These functions are GNU extensions, done in a style resembling
164 the POSIX version of functions like
165 .BR getpwnam_r (3).
166 Other systems use the prototype
168 .in +4n
170 struct passwd *
171 getpwent_r(struct passwd *pwd, char *buf, int buflen);
175 or, better,
177 .in +4n
180 getpwent_r(struct passwd *pwd, char *buf, int buflen,
181            FILE **pw_fp);
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
191 #define _GNU_SOURCE
192 #include <pwd.h>
193 #include <stdio.h>
194 #include <stdint.h>
195 #define BUFLEN 4096
198 main(void)
200     struct passwd pw;
201     struct passwd *pwp;
202     char buf[BUFLEN];
203     int i;
205     setpwent();
206     while (1) {
207         i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
208         if (i)
209             break;
210         printf("%s (%jd)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
211                (intmax_t) pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
212     }
213     endpwent();
214     exit(EXIT_SUCCESS);
217 .\" perhaps add error checking - should use strerror_r
218 .\" #include <errno.h>
219 .\" #include <stdlib.h>
220 .\"         if (i) {
221 .\"               if (i == ENOENT)
222 .\"                     break;
223 .\"               printf("getpwent_r: %s", strerror(i));
224 .\"               exit(EXIT_SUCCESS);
225 .\"         }
226 .SH SEE ALSO
227 .BR fgetpwent (3),
228 .BR getpw (3),
229 .BR getpwent (3),
230 .BR getpwnam (3),
231 .BR getpwuid (3),
232 .BR putpwent (3),
233 .BR passwd (5)