1GETGRENT_R(3) Linux Programmer's Manual GETGRENT_R(3)
2
3
4
6 getgrent_r, fgetgrent_r - get group file entry reentrantly
7
9 #include <grp.h>
10
11 int getgrent_r(struct group *gbuf, char *buf,
12 size_t buflen, struct group **gbufp);
13
14 int fgetgrent_r(FILE *stream, struct group *gbuf, char *buf,
15 size_t buflen, struct group **gbufp);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 getgrent_r(): _GNU_SOURCE
20 fgetgrent_r():
21 Since glibc 2.19:
22 _DEFAULT_SOURCE
23 Glibc 2.19 and earlier:
24 _SVID_SOURCE
25
27 The functions getgrent_r() and fgetgrent_r() are the reentrant versions
28 of getgrent(3) and fgetgrent(3). The former reads the next group entry
29 from the stream initialized by setgrent(3). The latter reads the next
30 group entry from stream.
31
32 The group structure is defined in <grp.h> as follows:
33
34 struct group {
35 char *gr_name; /* group name */
36 char *gr_passwd; /* group password */
37 gid_t gr_gid; /* group ID */
38 char **gr_mem; /* NULL-terminated array of pointers
39 to names of group members */
40 };
41
42 For more information about the fields of this structure, see group(5).
43
44 The nonreentrant functions return a pointer to static storage, where
45 this static storage contains further pointers to group name, password
46 and members. The reentrant functions described here return all of that
47 in caller-provided buffers. First of all there is the buffer gbuf that
48 can hold a struct group. And next the buffer buf of size buflen that
49 can hold additional strings. The result of these functions, the struct
50 group read from the stream, is stored in the provided buffer *gbuf, and
51 a pointer to this struct group is returned in *gbufp.
52
54 On success, these functions return 0 and *gbufp is a pointer to the
55 struct group. On error, these functions return an error value and
56 *gbufp is NULL.
57
59 ENOENT No more entries.
60
61 ERANGE Insufficient buffer space supplied. Try again with larger buf‐
62 fer.
63
65 For an explanation of the terms used in this section, see
66 attributes(7).
67
68 ┌──────────────┬───────────────┬─────────────────────────────┐
69 │Interface │ Attribute │ Value │
70 ├──────────────┼───────────────┼─────────────────────────────┤
71 │getgrent_r() │ Thread safety │ MT-Unsafe race:grent locale │
72 ├──────────────┼───────────────┼─────────────────────────────┤
73 │fgetgrent_r() │ Thread safety │ MT-Safe │
74 └──────────────┴───────────────┴─────────────────────────────┘
75 In the above table, grent in race:grent signifies that if any of the
76 functions setgrent(), getgrent(), endgrent(), or getgrent_r() are used
77 in parallel in different threads of a program, then data races could
78 occur.
79
81 These functions are GNU extensions, done in a style resembling the
82 POSIX version of functions like getpwnam_r(3). Other systems use the
83 prototype
84
85 struct group *getgrent_r(struct group *grp, char *buf,
86 int buflen);
87
88 or, better,
89
90 int getgrent_r(struct group *grp, char *buf, int buflen,
91 FILE **gr_fp);
92
94 The function getgrent_r() is not really reentrant since it shares the
95 reading position in the stream with all other threads.
96
98 #define _GNU_SOURCE
99 #include <grp.h>
100 #include <stdio.h>
101 #include <stdlib.h>
102 #define BUFLEN 4096
103
104 int
105 main(void)
106 {
107 struct group grp, *grpp;
108 char buf[BUFLEN];
109 int i;
110
111 setgrent();
112 while (1) {
113 i = getgrent_r(&grp, buf, BUFLEN, &grpp);
114 if (i)
115 break;
116 printf("%s (%d):", grpp->gr_name, grpp->gr_gid);
117 for (i = 0; ; i++) {
118 if (grpp->gr_mem[i] == NULL)
119 break;
120 printf(" %s", grpp->gr_mem[i]);
121 }
122 printf("\n");
123 }
124 endgrent();
125 exit(EXIT_SUCCESS);
126 }
127
129 fgetgrent(3), getgrent(3), getgrgid(3), getgrnam(3), putgrent(3),
130 group(5)
131
133 This page is part of release 5.04 of the Linux man-pages project. A
134 description of the project, information about reporting bugs, and the
135 latest version of this page, can be found at
136 https://www.kernel.org/doc/man-pages/.
137
138
139
140GNU 2017-09-15 GETGRENT_R(3)