OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / original / man3 / getpwent_r.3
1 .\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl)
2 .\"
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
7 .\"
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
12 .\"
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 .\" GNU General Public License for more details.
17 .\"
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, write to the Free
20 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
21 .\" USA.
22 .\"
23 .TH GETPWENT_R 3 2007-07-26 "GNU" "Linux Programmer's Manual"
24 .SH NAME
25 getpwent_r, fgetpwent_r \- get passwd file entry reentrantly
26 .SH SYNOPSIS
27 .nf
28 .B #include <pwd.h>
29 .sp
30 .BI "int getpwent_r(struct passwd *" pwbuf ", char *" buf ,
31 .br
32 .BI "               size_t " buflen ", struct passwd **" pwbufp );
33 .sp
34 .BI "int fgetpwent_r(FILE *" fp ", struct passwd *" pwbuf ", char *" buf ,
35 .br
36 .BI "                size_t " buflen ", struct passwd **" pwbufp );
37 .fi
38 .sp
39 .in -4n
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .in
43 .sp
44 .BR getpwent_r (),
45 _BSD_SOURCE || _SVID_SOURCE
46 .br
47 .BR fgetpwent_r ():
48 _SVID_SOURCE
49 .SH DESCRIPTION
50 The functions
51 .BR getpwent_r ()
52 and
53 .BR fgetpwent_r ()
54 are the reentrant versions of
55 .BR getpwent (3)
56 and
57 .BR fgetpwent (3).
58 The former reads the next passwd entry from the stream initialized by
59 .BR setpwent (3).
60 The latter reads the next passwd entry from the stream
61 .IR fp .
62 .PP
63 The \fIpasswd\fP structure is defined in
64 .I <pwd.h>
65 as follows:
66 .sp
67 .in +4n
68 .nf
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;     /* real name */
75     char    *pw_dir;       /* home directory */
76     char    *pw_shell;     /* shell program */
77 };
78 .fi
79 .in
80 .sp
81 The nonreentrant functions return a pointer to static storage,
82 where this static storage contains further pointers to user
83 name, password, gecos field, home directory and shell.
84 The reentrant functions described here return all of that in
85 caller-provided buffers.
86 First of all there is the buffer
87 .I pwbuf
88 that can hold a \fIstruct passwd\fP.
89 And next the buffer
90 .I buf
91 of size
92 .I buflen
93 that can hold additional strings.
94 The result of these functions, the \fIstruct passwd\fP read from the stream,
95 is stored in the provided buffer
96 .IR *pwbuf ,
97 and a pointer to this \fIstruct passwd\fP is returned in
98 .IR *pwbufp .
99 .SH "RETURN VALUE"
100 On success, these functions return 0 and
101 .I *pwbufp
102 is a pointer to the \fIstruct passwd\fP.
103 On error, these functions return an error value and
104 .I *pwbufp
105 is NULL.
106 .SH ERRORS
107 .TP
108 .B ENOENT
109 No more entries.
110 .TP
111 .B ERANGE
112 Insufficient buffer space supplied.
113 Try again with larger buffer.
114 .SH "CONFORMING TO"
115 These functions are GNU extensions, done in a style resembling
116 the POSIX version of functions like
117 .BR getpwnam_r (3).
118 Other systems use prototype
119 .sp
120 .nf
121 .in +4n
122 struct passwd *
123 getpwent_r(struct passwd *pwd, char *buf, int buflen);
124 .in
125 .fi
126 .sp
127 or, better,
128 .sp
129 .nf
130 .in +4n
131 int
132 getpwent_r(struct passwd *pwd, char *buf, int buflen,
133            FILE **pw_fp);
134 .in
135 .fi
136 .SH NOTES
137 The function
138 .BR getpwent_r ()
139 is not really reentrant since it shares the reading position
140 in the stream with all other threads.
141 .SH EXAMPLE
142 .nf
143 #define _GNU_SOURCE
144 #include <pwd.h>
145 #include <stdio.h>
146 #define BUFLEN 4096
147
148 int
149 main(void)
150 {
151     struct passwd pw, *pwp;
152     char buf[BUFLEN];
153     int i;
154
155     setpwent();
156     while (1) {
157         i = getpwent_r(&pw, buf, BUFLEN, &pwp);
158         if (i)
159             break;
160         printf("%s (%d)\etHOME %s\etSHELL %s\en", pwp\->pw_name,
161                pwp\->pw_uid, pwp\->pw_dir, pwp\->pw_shell);
162     }
163     endpwent();
164     exit(EXIT_SUCCESS);
165 }
166 .fi
167 .\" perhaps add error checking - should use strerror_r
168 .\" #include <errno.h>
169 .\" #include <stdlib.h>
170 .\"         if (i) {
171 .\"               if (i == ENOENT)
172 .\"                     break;
173 .\"               printf("getpwent_r: %s", strerror(i));
174 .\"               exit(EXIT_SUCCESS);
175 .\"         }
176 .SH "SEE ALSO"
177 .BR fgetpwent (3),
178 .BR getpw (3),
179 .BR getpwent (3),
180 .BR getpwnam (3),
181 .BR getpwuid (3),
182 .BR putpwent (3),
183 .BR passwd (5)