OSDN Git Service

08c8e4a9edd2fb593c7fe9ba7dc720e9c83bc601
[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 2015-01-22 "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 *" stream ", 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
63 .IR stream .
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;         /* NULL-terminated array of pointers
76                                to names of group members */
77 };
78 .fi
79 .in
80 .PP
81 For more information about the fields of this structure, see
82 .BR group (5).
83 .PP
84 The nonreentrant functions return a pointer to static storage,
85 where this static storage contains further pointers to group
86 name, password and members.
87 The reentrant functions described here return all of that in
88 caller-provided buffers.
89 First of all there is the buffer
90 .I gbuf
91 that can hold a \fIstruct group\fP.
92 And next the buffer
93 .I buf
94 of size
95 .I buflen
96 that can hold additional strings.
97 The result of these functions, the \fIstruct group\fP read from the stream,
98 is stored in the provided buffer
99 .IR *gbuf ,
100 and a pointer to this \fIstruct group\fP is returned in
101 .IR *gbufp .
102 .SH RETURN VALUE
103 On success, these functions return 0 and
104 .I *gbufp
105 is a pointer to the \fIstruct group\fP.
106 On error, these functions return an error value and
107 .I *gbufp
108 is NULL.
109 .SH ERRORS
110 .TP
111 .B ENOENT
112 No more entries.
113 .TP
114 .B ERANGE
115 Insufficient buffer space supplied.
116 Try again with larger buffer.
117 .SH CONFORMING TO
118 These functions are GNU extensions, done in a style resembling
119 the POSIX version of functions like
120 .BR getpwnam_r (3).
121 Other systems use the prototype
122 .sp
123 .nf
124 .in +4n
125 struct group *getgrent_r(struct group *grp, char *buf,
126                          int buflen);
127 .in
128 .fi
129 .sp
130 or, better,
131 .sp
132 .nf
133 .in +4n
134 int getgrent_r(struct group *grp, char *buf, int buflen,
135                FILE **gr_fp);
136 .in
137 .fi
138 .SH NOTES
139 The function
140 .BR getgrent_r ()
141 is not really reentrant since it shares the reading position
142 in the stream with all other threads.
143 .SH EXAMPLE
144 .nf
145 #define _GNU_SOURCE
146 #include <grp.h>
147 #include <stdio.h>
148 #include <stdlib.h>
149 #define BUFLEN 4096
150
151 int
152 main(void)
153 {
154     struct group grp, *grpp;
155     char buf[BUFLEN];
156     int i;
157
158     setgrent();
159     while (1) {
160         i = getgrent_r(&grp, buf, BUFLEN, &grpp);
161         if (i)
162             break;
163         printf("%s (%d):", grpp\->gr_name, grpp\->gr_gid);
164         for (i = 0; ; i++) {
165             if (grpp\->gr_mem[i] == NULL)
166                 break;
167             printf(" %s", grpp\->gr_mem[i]);
168         }
169         printf("\en");
170     }
171     endgrent();
172     exit(EXIT_SUCCESS);
173 }
174 .fi
175 .\" perhaps add error checking - should use strerror_r
176 .\" #include <errno.h>
177 .\" #include <stdlib.h>
178 .\"         if (i) {
179 .\"               if (i == ENOENT)
180 .\"                     break;
181 .\"               printf("getgrent_r: %s", strerror(i));
182 .\"               exit(EXIT_FAILURE);
183 .\"         }
184 .SH SEE ALSO
185 .BR fgetgrent (3),
186 .BR getgrent (3),
187 .BR getgrgid (3),
188 .BR getgrnam (3),
189 .BR putgrent (3),
190 .BR group (5)
191 .SH COLOPHON
192 This page is part of release 3.78 of the Linux
193 .I man-pages
194 project.
195 A description of the project,
196 information about reporting bugs,
197 and the latest version of this page,
198 can be found at
199 \%http://www.kernel.org/doc/man\-pages/.