]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getpwent_r.3
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[thirdparty/man-pages.git] / 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 2003-11-15 "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 "#define _GNU_SOURCE"
29 .br
30 .B #include <pwd.h>
31 .sp
32 .BI "int getpwent_r(struct passwd *" pwbuf ", char *" buf ,
33 .br
34 .BI " size_t " buflen ", struct passwd **" pwbufp );
35 .sp
36 .BI "int fgetpwent_r(FILE *" fp ", struct passwd *" pwbuf ", char *" buf ,
37 .br
38 .BI " size_t " buflen ", struct passwd **" pwbufp );
39 .SH DESCRIPTION
40 The functions
41 .BR getpwent_r ()
42 and
43 .BR fgetpwent_r ()
44 are the reentrant versions of
45 .BR getpwent (3)
46 and
47 .BR fgetpwent (3).
48 The former reads the next passwd entry from the stream initialized by
49 .BR setpwent (3).
50 The latter reads the next passwd entry from the stream
51 .I fp
52 given as parameter.
53 .PP
54 The \fIpasswd\fP structure is defined in
55 .I <pwd.h>
56 as follows:
57 .sp
58 .RS
59 .nf
60 struct passwd {
61 char *pw_name; /* user name */
62 char *pw_passwd; /* user password */
63 uid_t pw_uid; /* user ID */
64 gid_t pw_gid; /* group ID */
65 char *pw_gecos; /* real name */
66 char *pw_dir; /* home directory */
67 char *pw_shell; /* shell program */
68 };
69 .fi
70 .RE
71 .sp
72 The non-reentrant functions return a pointer to static storage,
73 where this static storage contains further pointers to user
74 name, password, gecos field, home directory and shell.
75 The reentrant functions described here return all of that in
76 caller-provided buffers.
77 First of all there is the buffer
78 .I pwbuf
79 that can hold a struct passwd.
80 And next the buffer
81 .I buf
82 of size
83 .I buflen
84 that can hold additional strings.
85 The result of these functions, the struct passwd read from the stream,
86 is stored in the provided buffer
87 .RI * pwbuf ,
88 and a pointer to this struct passwd is returned in
89 .RI * pwbufp .
90 .SH "RETURN VALUE"
91 On success, these functions return 0 and
92 .RI * pwbufp
93 is a pointer to the struct passwd.
94 On error, these functions return an error value and
95 .RI * pwbufp
96 is NULL.
97 .SH ERRORS
98 .TP
99 .B ENOENT
100 No more entries.
101 .TP
102 .B ERANGE
103 Insufficient buffer space supplied.
104 Try again with larger buffer.
105 .SH EXAMPLE
106 .nf
107 #define _GNU_SOURCE
108 #include <pwd.h>
109 #include <stdio.h>
110 #define BUFLEN 4096
111
112 int
113 main(void)
114 {
115 struct passwd pw, *pwp;
116 char buf[BUFLEN];
117 int i;
118
119 setpwent();
120 while (1) {
121 i = getpwent_r(&pw, buf, BUFLEN, &pwp);
122 if (i)
123 break;
124 printf("%s (%d)\etHOME %s\etSHELL %s\en", pwp->pw_name,
125 pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
126 }
127 endpwent();
128 return 0;
129 }
130 .fi
131 .\" perhaps add error checking - should use strerror_r
132 .\" #include <errno.h>
133 .\" #include <stdlib.h>
134 .\" if (i) {
135 .\" if (i == ENOENT)
136 .\" break;
137 .\" printf("getpwent_r: %s", strerror(i));
138 .\" exit(1);
139 .\" }
140 .SH "CONFORMING TO"
141 These functions are GNU extensions, done in a style resembling
142 the POSIX version of functions like
143 .BR getpwnam_r (3).
144 Other systems use prototype
145 .sp
146 .nf
147 .in +4
148 struct passwd *
149 getpwent_r(struct passwd *pwd, char *buf, int buflen);
150 .in
151 .fi
152 .sp
153 or, better,
154 .sp
155 .nf
156 .in +4
157 int
158 getpwent_r(struct passwd *pwd, char *buf, int buflen,
159 FILE **pw_fp);
160 .in
161 .fi
162 .SH NOTES
163 The function
164 .BR getpwent_r ()
165 is not really reentrant since it shares the reading position
166 in the stream with all other threads.
167 .SH "SEE ALSO"
168 .BR fgetpwent (3),
169 .BR getpw (3),
170 .BR getpwent (3),
171 .BR getpwnam (3),
172 .BR getpwuid (3),
173 .BR putpwent (3),
174 .BR passwd (5),
175 .BR feature_test_macros (7)