]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getgrent_r.3
adjtimex.2, prctl.2, semget.2, signalfd.2, umount.2, syscall.2, dlopen.3, getgrent_r...
[thirdparty/man-pages.git] / man3 / getgrent_r.3
CommitLineData
fea681da
MK
1.\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl)
2.\"
1dd72f9c 3.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
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
c715f741
MK
20.\" License along with this manual; if not, see
21.\" <http://www.gnu.org/licenses/>.
6a8d8745 22.\" %%%LICENSE_END
fea681da 23.\"
4b8c67d9 24.TH GETGRENT_R 3 2017-09-15 "GNU" "Linux Programmer's Manual"
fea681da
MK
25.SH NAME
26getgrent_r, fgetgrent_r \- get group file entry reentrantly
27.SH SYNOPSIS
28.nf
fea681da 29.B #include <grp.h>
68e4db0a 30.PP
fea681da 31.BI "int getgrent_r(struct group *" gbuf ", char *" buf ,
fea681da 32.BI " size_t " buflen ", struct group **" gbufp );
68e4db0a 33.PP
e164e983 34.BI "int fgetgrent_r(FILE *" stream ", struct group *" gbuf ", char *" buf ,
fea681da 35.BI " size_t " buflen ", struct group **" gbufp );
cc4615cc 36.fi
68e4db0a 37.PP
cc4615cc
MK
38.in -4n
39Feature Test Macro Requirements for glibc (see
40.BR feature_test_macros (7)):
41.in
68e4db0a 42.PP
cc4615cc
MK
43.BR getgrent_r ():
44_GNU_SOURCE
45.\" FIXME . The FTM requirements seem inconsistent here. File a glibc bug?
46.br
47.BR fgetgrent_r ():
51c612fb
MK
48 Since glibc 2.19:
49 _DEFAULT_SOURCE
50 Glibc 2.19 and earlier:
51 _SVID_SOURCE
fea681da
MK
52.SH DESCRIPTION
53The functions
63aa9df0 54.BR getgrent_r ()
fea681da 55and
63aa9df0 56.BR fgetgrent_r ()
fea681da
MK
57are the reentrant versions of
58.BR getgrent (3)
59and
60.BR fgetgrent (3).
61The former reads the next group entry from the stream initialized by
62.BR setgrent (3).
43905c28 63The latter reads the next group entry from
e164e983 64.IR stream .
fea681da
MK
65.PP
66The \fIgroup\fP structure is defined in
67.I <grp.h>
68as follows:
51f5698d 69.PP
bd191423 70.in +4n
b8302363 71.EX
fea681da 72struct group {
5e333b1b
MK
73 char *gr_name; /* group name */
74 char *gr_passwd; /* group password */
75 gid_t gr_gid; /* group ID */
b7800b44
MK
76 char **gr_mem; /* NULL-terminated array of pointers
77 to names of group members */
fea681da 78};
b8302363 79.EE
bd191423 80.in
b39f3e05
MK
81.PP
82For more information about the fields of this structure, see
83.BR group (5).
84.PP
54d75d6c 85The nonreentrant functions return a pointer to static storage,
fea681da
MK
86where this static storage contains further pointers to group
87name, password and members.
88The reentrant functions described here return all of that in
c13182ef
MK
89caller-provided buffers.
90First of all there is the buffer
fea681da 91.I gbuf
0c2ec4f1 92that can hold a \fIstruct group\fP.
c13182ef 93And next the buffer
fea681da
MK
94.I buf
95of size
96.I buflen
97that can hold additional strings.
0c2ec4f1 98The result of these functions, the \fIstruct group\fP read from the stream,
fea681da 99is stored in the provided buffer
bd9b2a9c 100.IR *gbuf ,
0c2ec4f1 101and a pointer to this \fIstruct group\fP is returned in
bd9b2a9c 102.IR *gbufp .
47297adb 103.SH RETURN VALUE
fea681da 104On success, these functions return 0 and
bd9b2a9c 105.I *gbufp
0c2ec4f1 106is a pointer to the \fIstruct group\fP.
fea681da 107On error, these functions return an error value and
bd9b2a9c 108.I *gbufp
fea681da
MK
109is NULL.
110.SH ERRORS
111.TP
112.B ENOENT
113No more entries.
114.TP
115.B ERANGE
c13182ef
MK
116Insufficient buffer space supplied.
117Try again with larger buffer.
f8c46262
ZL
118.SH ATTRIBUTES
119For an explanation of the terms used in this section, see
120.BR attributes (7).
121.TS
122allbox;
123lb lb lbw27
124l l l.
125Interface Attribute Value
126T{
127.BR getgrent_r ()
128T} Thread safety MT-Unsafe race:grent locale
129T{
130.BR fgetgrent_r ()
131T} Thread safety MT-Safe
132.TE
847e0d88 133.sp 1
f8c46262
ZL
134In the above table,
135.I grent
136in
137.I race:grent
138signifies that if any of the functions
e3a78ee9
MK
139.BR setgrent (3),
140.BR getgrent (3),
141.BR endgrent (3),
f8c46262
ZL
142or
143.BR getgrent_r ()
144are used in parallel in different threads of a program,
145then data races could occur.
47297adb 146.SH CONFORMING TO
2b2581ee
MK
147These functions are GNU extensions, done in a style resembling
148the POSIX version of functions like
149.BR getpwnam_r (3).
b33e5e80 150Other systems use the prototype
51f5698d 151.PP
088a639b 152.in +4n
b8302363 153.EX
3f282501
MK
154struct group *getgrent_r(struct group *grp, char *buf,
155 int buflen);
b8302363 156.EE
bdd915e2 157.in
51f5698d 158.PP
2b2581ee 159or, better,
51f5698d 160.PP
088a639b 161.in +4n
b8302363 162.EX
3f282501
MK
163int getgrent_r(struct group *grp, char *buf, int buflen,
164 FILE **gr_fp);
b8302363 165.EE
bdd915e2 166.in
2b2581ee
MK
167.SH NOTES
168The function
169.BR getgrent_r ()
170is not really reentrant since it shares the reading position
171in the stream with all other threads.
fea681da 172.SH EXAMPLE
bdd915e2 173.EX
fea681da
MK
174#define _GNU_SOURCE
175#include <grp.h>
176#include <stdio.h>
af9c7ff2 177#include <stdlib.h>
fea681da
MK
178#define BUFLEN 4096
179
c13182ef
MK
180int
181main(void)
cf0a9ace
MK
182{
183 struct group grp, *grpp;
184 char buf[BUFLEN];
185 int i;
fea681da 186
cf0a9ace
MK
187 setgrent();
188 while (1) {
189 i = getgrent_r(&grp, buf, BUFLEN, &grpp);
190 if (i)
191 break;
29059a65 192 printf("%s (%d):", grpp\->gr_name, grpp\->gr_gid);
cf0a9ace 193 for (i = 0; ; i++) {
29059a65 194 if (grpp\->gr_mem[i] == NULL)
cf0a9ace 195 break;
29059a65 196 printf(" %s", grpp\->gr_mem[i]);
cf0a9ace 197 }
31a6818e 198 printf("\en");
cf0a9ace
MK
199 }
200 endgrent();
5bc8c34c 201 exit(EXIT_SUCCESS);
fea681da 202}
bdd915e2 203.EE
fea681da
MK
204.\" perhaps add error checking - should use strerror_r
205.\" #include <errno.h>
206.\" #include <stdlib.h>
207.\" if (i) {
208.\" if (i == ENOENT)
209.\" break;
210.\" printf("getgrent_r: %s", strerror(i));
4c44ffe5 211.\" exit(EXIT_FAILURE);
fea681da 212.\" }
47297adb 213.SH SEE ALSO
fea681da
MK
214.BR fgetgrent (3),
215.BR getgrent (3),
216.BR getgrgid (3),
217.BR getgrnam (3),
c9c3b6f3 218.BR putgrent (3),
cc4615cc 219.BR group (5)