OSDN Git Service

777580a5fa3d63c8bca0f478857e56b52243d2db
[linuxjm/LDP_man-pages.git] / original / 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 2010-10-21 "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 .sp
31 .BI "int getpwent_r(struct passwd *" pwbuf ", char *" buf ,
32 .br
33 .BI "               size_t " buflen ", struct passwd **" pwbufp );
34 .sp
35 .BI "int fgetpwent_r(FILE *" fp ", struct passwd *" pwbuf ", char *" buf ,
36 .br
37 .BI "                size_t " buflen ", struct passwd **" pwbufp );
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 (),
46 _BSD_SOURCE || _SVID_SOURCE
47 .br
48 .BR fgetpwent_r ():
49 _SVID_SOURCE
50 .SH DESCRIPTION
51 The functions
52 .BR getpwent_r ()
53 and
54 .BR fgetpwent_r ()
55 are the reentrant versions of
56 .BR getpwent (3)
57 and
58 .BR fgetpwent (3).
59 The former reads the next passwd entry from the stream initialized by
60 .BR setpwent (3).
61 The latter reads the next passwd entry from the stream
62 .IR fp .
63 .PP
64 The \fIpasswd\fP structure is defined in
65 .I <pwd.h>
66 as follows:
67 .sp
68 .in +4n
69 .nf
70 struct passwd {
71     char    *pw_name;      /* username */
72     char    *pw_passwd;    /* user password */
73     uid_t    pw_uid;       /* user ID */
74     gid_t    pw_gid;       /* group ID */
75     char    *pw_gecos;     /* user information */
76     char    *pw_dir;       /* home directory */
77     char    *pw_shell;     /* shell program */
78 };
79 .fi
80 .in
81 .PP
82 For more information about the fields of this structure, see
83 .BR passwd (5).
84
85 The nonreentrant functions return a pointer to static storage,
86 where this static storage contains further pointers to user
87 name, password, gecos field, home directory and shell.
88 The reentrant functions described here return all of that in
89 caller-provided buffers.
90 First of all there is the buffer
91 .I pwbuf
92 that can hold a \fIstruct passwd\fP.
93 And next the buffer
94 .I buf
95 of size
96 .I buflen
97 that can hold additional strings.
98 The result of these functions, the \fIstruct passwd\fP read from the stream,
99 is stored in the provided buffer
100 .IR *pwbuf ,
101 and a pointer to this \fIstruct passwd\fP is returned in
102 .IR *pwbufp .
103 .SH RETURN VALUE
104 On success, these functions return 0 and
105 .I *pwbufp
106 is a pointer to the \fIstruct passwd\fP.
107 On error, these functions return an error value and
108 .I *pwbufp
109 is NULL.
110 .SH ERRORS
111 .TP
112 .B ENOENT
113 No more entries.
114 .TP
115 .B ERANGE
116 Insufficient buffer space supplied.
117 Try again with larger buffer.
118 .SH CONFORMING TO
119 These functions are GNU extensions, done in a style resembling
120 the POSIX version of functions like
121 .BR getpwnam_r (3).
122 Other systems use prototype
123 .sp
124 .nf
125 .in +4n
126 struct passwd *
127 getpwent_r(struct passwd *pwd, char *buf, int buflen);
128 .in
129 .fi
130 .sp
131 or, better,
132 .sp
133 .nf
134 .in +4n
135 int
136 getpwent_r(struct passwd *pwd, char *buf, int buflen,
137            FILE **pw_fp);
138 .in
139 .fi
140 .SH NOTES
141 The function
142 .BR getpwent_r ()
143 is not really reentrant since it shares the reading position
144 in the stream with all other threads.
145 .SH EXAMPLE
146 .nf
147 #define _GNU_SOURCE
148 #include <pwd.h>
149 #include <stdio.h>
150 #define BUFLEN 4096
151
152 int
153 main(void)
154 {
155     struct passwd pw, *pwp;
156     char buf[BUFLEN];
157     int i;
158
159     setpwent();
160     while (1) {
161         i = getpwent_r(&pw, buf, BUFLEN, &pwp);
162         if (i)
163             break;
164         printf("%s (%d)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
165                pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
166     }
167     endpwent();
168     exit(EXIT_SUCCESS);
169 }
170 .fi
171 .\" perhaps add error checking - should use strerror_r
172 .\" #include <errno.h>
173 .\" #include <stdlib.h>
174 .\"         if (i) {
175 .\"               if (i == ENOENT)
176 .\"                     break;
177 .\"               printf("getpwent_r: %s", strerror(i));
178 .\"               exit(EXIT_SUCCESS);
179 .\"         }
180 .SH SEE ALSO
181 .BR fgetpwent (3),
182 .BR getpw (3),
183 .BR getpwent (3),
184 .BR getpwnam (3),
185 .BR getpwuid (3),
186 .BR putpwent (3),
187 .BR passwd (5)
188 .SH COLOPHON
189 This page is part of release 3.67 of the Linux
190 .I man-pages
191 project.
192 A description of the project,
193 information about reporting bugs,
194 and the latest version of this page,
195 can be found at
196 \%http://www.kernel.org/doc/man\-pages/.