]>
Commit | Line | Data |
---|---|---|
fea681da MK |
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 GETGRENT 3 2003-11-15 "GNU" "Linux Programmer's Manual" | |
24 | .SH NAME | |
25 | getgrent_r, fgetgrent_r \- get group file entry reentrantly | |
26 | .SH SYNOPSIS | |
27 | .nf | |
28 | .B "#define _GNU_SOURCE" | |
29 | .br | |
30 | .B #include <grp.h> | |
31 | .sp | |
32 | .BI "int getgrent_r(struct group *" gbuf ", char *" buf , | |
33 | .br | |
34 | .BI " size_t " buflen ", struct group **" gbufp ); | |
35 | .sp | |
36 | .BI "int fgetgrent_r(FILE *" fp ", struct group *" gbuf ", char *" buf , | |
37 | .br | |
38 | .BI " size_t " buflen ", struct group **" gbufp ); | |
39 | .SH DESCRIPTION | |
40 | The functions | |
63aa9df0 | 41 | .BR getgrent_r () |
fea681da | 42 | and |
63aa9df0 | 43 | .BR fgetgrent_r () |
fea681da MK |
44 | are the reentrant versions of |
45 | .BR getgrent (3) | |
46 | and | |
47 | .BR fgetgrent (3). | |
48 | The former reads the next group entry from the stream initialized by | |
49 | .BR setgrent (3). | |
50 | The latter reads the next group entry from the stream | |
51 | .I fp | |
52 | given as parameter. | |
53 | .PP | |
54 | The \fIgroup\fP structure is defined in | |
55 | .I <grp.h> | |
56 | as follows: | |
57 | .sp | |
58 | .RS | |
59 | .nf | |
60 | struct group { | |
cf0a9ace MK |
61 | char *gr_name; /* group name */ |
62 | char *gr_passwd; /* group password */ | |
63 | gid_t gr_gid; /* group ID */ | |
64 | char **gr_mem; /* group members */ | |
fea681da MK |
65 | }; |
66 | .ta | |
67 | .fi | |
68 | .RE | |
69 | .sp | |
70 | The non-reentrant functions return a pointer to static storage, | |
71 | where this static storage contains further pointers to group | |
72 | name, password and members. | |
73 | The reentrant functions described here return all of that in | |
c13182ef MK |
74 | caller-provided buffers. |
75 | First of all there is the buffer | |
fea681da | 76 | .I gbuf |
c13182ef MK |
77 | that can hold a struct group. |
78 | And next the buffer | |
fea681da MK |
79 | .I buf |
80 | of size | |
81 | .I buflen | |
82 | that can hold additional strings. | |
83 | The result of these functions, the struct group read from the stream, | |
84 | is stored in the provided buffer | |
85 | .RI * gbuf , | |
86 | and a pointer to this struct group is returned in | |
87 | .RI * gbufp . | |
88 | .SH "RETURN VALUE" | |
89 | On success, these functions return 0 and | |
90 | .RI * gbufp | |
91 | is a pointer to the struct group. | |
92 | On error, these functions return an error value and | |
93 | .RI * gbufp | |
94 | is NULL. | |
95 | .SH ERRORS | |
96 | .TP | |
97 | .B ENOENT | |
98 | No more entries. | |
99 | .TP | |
100 | .B ERANGE | |
c13182ef MK |
101 | Insufficient buffer space supplied. |
102 | Try again with larger buffer. | |
fea681da MK |
103 | .SH EXAMPLE |
104 | .nf | |
105 | #define _GNU_SOURCE | |
106 | #include <grp.h> | |
107 | #include <stdio.h> | |
108 | #define BUFLEN 4096 | |
109 | ||
c13182ef MK |
110 | int |
111 | main(void) | |
cf0a9ace MK |
112 | { |
113 | struct group grp, *grpp; | |
114 | char buf[BUFLEN]; | |
115 | int i; | |
fea681da | 116 | |
cf0a9ace MK |
117 | setgrent(); |
118 | while (1) { | |
119 | i = getgrent_r(&grp, buf, BUFLEN, &grpp); | |
120 | if (i) | |
121 | break; | |
122 | printf("%s (%d):", grpp->gr_name, grpp->gr_gid); | |
123 | for (i = 0; ; i++) { | |
124 | if (grpp->gr_mem[i] == NULL) | |
125 | break; | |
126 | printf(" %s", grpp->gr_mem[i]); | |
127 | } | |
128 | printf("\en"); | |
129 | } | |
130 | endgrent(); | |
131 | return 0; | |
fea681da MK |
132 | } |
133 | .fi | |
134 | .\" perhaps add error checking - should use strerror_r | |
135 | .\" #include <errno.h> | |
136 | .\" #include <stdlib.h> | |
137 | .\" if (i) { | |
138 | .\" if (i == ENOENT) | |
139 | .\" break; | |
140 | .\" printf("getgrent_r: %s", strerror(i)); | |
141 | .\" exit(1); | |
142 | .\" } | |
143 | .SH "CONFORMING TO" | |
144 | These functions are GNU extensions, done in a style resembling | |
145 | the POSIX version of functions like | |
146 | .BR getpwnam_r (3). | |
147 | Other systems use prototype | |
148 | .sp | |
149 | .nf | |
150 | .in +4 | |
151 | struct group * | |
152 | getgrent_r(struct group *grp, char *buf, int buflen); | |
153 | .in | |
154 | .fi | |
155 | .sp | |
156 | or, better, | |
157 | .sp | |
158 | .nf | |
159 | .in +4 | |
160 | int | |
161 | getgrent_r(struct group *grp, char *buf, int buflen, | |
162 | FILE **gr_fp); | |
163 | .in | |
164 | .fi | |
fea681da MK |
165 | .SH NOTES |
166 | The function | |
63aa9df0 | 167 | .BR getgrent_r () |
fea681da MK |
168 | is not really reentrant since it shares the reading position |
169 | in the stream with all other threads. | |
170 | .SH "SEE ALSO" | |
171 | .BR fgetgrent (3), | |
172 | .BR getgrent (3), | |
173 | .BR getgrgid (3), | |
174 | .BR getgrnam (3), | |
c9c3b6f3 | 175 | .BR putgrent (3), |
0a90178c MK |
176 | .BR group (5), |
177 | .BR feature_test_macros (7) |