OSDN Git Service

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