]>
Commit | Line | Data |
---|---|---|
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 | .\" |
97986708 | 24 | .TH GETPWENT_R 3 2016-03-15 "GNU" "Linux Programmer's Manual" |
fea681da MK |
25 | .SH NAME |
26 | getpwent_r, fgetpwent_r \- get passwd file entry reentrantly | |
27 | .SH SYNOPSIS | |
28 | .nf | |
fea681da MK |
29 | .B #include <pwd.h> |
30 | .sp | |
31 | .BI "int getpwent_r(struct passwd *" pwbuf ", char *" buf , | |
32 | .br | |
33 | .BI " size_t " buflen ", struct passwd **" pwbufp ); | |
34 | .sp | |
495604c3 | 35 | .BI "int fgetpwent_r(FILE *" stream ", struct passwd *" pwbuf ", char *" buf , |
fea681da MK |
36 | .br |
37 | .BI " size_t " buflen ", struct passwd **" pwbufp ); | |
cc4615cc MK |
38 | .fi |
39 | .sp | |
40 | .in -4n | |
41 | Feature Test Macro Requirements for glibc (see | |
42 | .BR feature_test_macros (7)): | |
43 | .in | |
44 | .sp | |
45 | .BR getpwent_r (), | |
51c612fb MK |
46 | Since glibc 2.19: |
47 | _DEFAULT_SOURCE | |
48 | Glibc 2.19 and earlier: | |
49 | _BSD_SOURCE || _SVID_SOURCE | |
cc4615cc MK |
50 | .br |
51 | .BR fgetpwent_r (): | |
51c612fb MK |
52 | Since glibc 2.19: |
53 | _DEFAULT_SOURCE | |
54 | Glibc 2.19 and earlier: | |
55 | _SVID_SOURCE | |
fea681da MK |
56 | .SH DESCRIPTION |
57 | The functions | |
63aa9df0 | 58 | .BR getpwent_r () |
fea681da | 59 | and |
63aa9df0 | 60 | .BR fgetpwent_r () |
fea681da MK |
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). | |
ef3d4efe | 67 | The latter reads the next passwd entry from |
495604c3 | 68 | .IR stream . |
fea681da MK |
69 | .PP |
70 | The \fIpasswd\fP structure is defined in | |
71 | .I <pwd.h> | |
72 | as follows: | |
73 | .sp | |
bd191423 | 74 | .in +4n |
fea681da MK |
75 | .nf |
76 | struct passwd { | |
18701562 | 77 | char *pw_name; /* username */ |
cf0a9ace MK |
78 | char *pw_passwd; /* user password */ |
79 | uid_t pw_uid; /* user ID */ | |
80 | gid_t pw_gid; /* group ID */ | |
97724acd | 81 | char *pw_gecos; /* user information */ |
cf0a9ace MK |
82 | char *pw_dir; /* home directory */ |
83 | char *pw_shell; /* shell program */ | |
fea681da MK |
84 | }; |
85 | .fi | |
bd191423 | 86 | .in |
7f12f117 MK |
87 | .PP |
88 | For more information about the fields of this structure, see | |
89 | .BR passwd (5). | |
90 | ||
54d75d6c | 91 | The nonreentrant functions return a pointer to static storage, |
fea681da MK |
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 | |
c13182ef MK |
95 | caller-provided buffers. |
96 | First of all there is the buffer | |
fea681da | 97 | .I pwbuf |
0c2ec4f1 | 98 | that can hold a \fIstruct passwd\fP. |
c13182ef | 99 | And next the buffer |
fea681da MK |
100 | .I buf |
101 | of size | |
102 | .I buflen | |
103 | that can hold additional strings. | |
0c2ec4f1 | 104 | The result of these functions, the \fIstruct passwd\fP read from the stream, |
fea681da | 105 | is stored in the provided buffer |
bd9b2a9c | 106 | .IR *pwbuf , |
0c2ec4f1 | 107 | and a pointer to this \fIstruct passwd\fP is returned in |
bd9b2a9c | 108 | .IR *pwbufp . |
47297adb | 109 | .SH RETURN VALUE |
fea681da | 110 | On success, these functions return 0 and |
bd9b2a9c | 111 | .I *pwbufp |
0c2ec4f1 | 112 | is a pointer to the \fIstruct passwd\fP. |
fea681da | 113 | On error, these functions return an error value and |
bd9b2a9c | 114 | .I *pwbufp |
fea681da MK |
115 | is NULL. |
116 | .SH ERRORS | |
117 | .TP | |
118 | .B ENOENT | |
119 | No more entries. | |
120 | .TP | |
121 | .B ERANGE | |
c13182ef MK |
122 | Insufficient buffer space supplied. |
123 | Try again with larger buffer. | |
98a731cd ZL |
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. | |
47297adb | 152 | .SH CONFORMING TO |
2b2581ee MK |
153 | These functions are GNU extensions, done in a style resembling |
154 | the POSIX version of functions like | |
155 | .BR getpwnam_r (3). | |
b5606b27 | 156 | Other systems use the prototype |
2b2581ee MK |
157 | .sp |
158 | .nf | |
088a639b | 159 | .in +4n |
2b2581ee MK |
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 | |
088a639b | 168 | .in +4n |
2b2581ee MK |
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. | |
fea681da MK |
179 | .SH EXAMPLE |
180 | .nf | |
181 | #define _GNU_SOURCE | |
182 | #include <pwd.h> | |
183 | #include <stdio.h> | |
184 | #define BUFLEN 4096 | |
185 | ||
c13182ef MK |
186 | int |
187 | main(void) | |
cf0a9ace MK |
188 | { |
189 | struct passwd pw, *pwp; | |
190 | char buf[BUFLEN]; | |
191 | int i; | |
fea681da | 192 | |
cf0a9ace MK |
193 | setpwent(); |
194 | while (1) { | |
195 | i = getpwent_r(&pw, buf, BUFLEN, &pwp); | |
196 | if (i) | |
197 | break; | |
31a6818e | 198 | printf("%s (%d)\etHOME %s\etSHELL %s\en", pwp\->pw_name, |
29059a65 | 199 | pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell); |
cf0a9ace MK |
200 | } |
201 | endpwent(); | |
5bc8c34c | 202 | exit(EXIT_SUCCESS); |
fea681da MK |
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)); | |
4c44ffe5 | 212 | .\" exit(EXIT_SUCCESS); |
fea681da | 213 | .\" } |
47297adb | 214 | .SH SEE ALSO |
fea681da MK |
215 | .BR fgetpwent (3), |
216 | .BR getpw (3), | |
217 | .BR getpwent (3), | |
218 | .BR getpwnam (3), | |
219 | .BR getpwuid (3), | |
220 | .BR putpwent (3), | |
cc4615cc | 221 | .BR passwd (5) |