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 *fp, 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(): _SVID_SOURCE
21
23 The functions getgrent_r() and fgetgrent_r() are the reentrant versions
24 of getgrent(3) and fgetgrent(3). The former reads the next group entry
25 from the stream initialized by setgrent(3). The latter reads the next
26 group entry from the stream fp.
27
28 The group structure is defined in <grp.h> as follows:
29
30 struct group {
31 char *gr_name; /* group name */
32 char *gr_passwd; /* group password */
33 gid_t gr_gid; /* group ID */
34 char **gr_mem; /* group members */
35 };
36
37 For more information about the fields of this structure, see group(5).
38
39 The nonreentrant functions return a pointer to static storage, where
40 this static storage contains further pointers to group name, password
41 and members. The reentrant functions described here return all of that
42 in caller-provided buffers. First of all there is the buffer gbuf that
43 can hold a struct group. And next the buffer buf of size buflen that
44 can hold additional strings. The result of these functions, the struct
45 group read from the stream, is stored in the provided buffer *gbuf, and
46 a pointer to this struct group is returned in *gbufp.
47
49 On success, these functions return 0 and *gbufp is a pointer to the
50 struct group. On error, these functions return an error value and
51 *gbufp is NULL.
52
54 ENOENT No more entries.
55
56 ERANGE Insufficient buffer space supplied. Try again with larger buf‐
57 fer.
58
60 These functions are GNU extensions, done in a style resembling the
61 POSIX version of functions like getpwnam_r(3). Other systems use pro‐
62 totype
63
64 struct group *getgrent_r(struct group *grp, char *buf,
65 int buflen);
66
67 or, better,
68
69 int getgrent_r(struct group *grp, char *buf, int buflen,
70 FILE **gr_fp);
71
73 The function getgrent_r() is not really reentrant since it shares the
74 reading position in the stream with all other threads.
75
77 #define _GNU_SOURCE
78 #include <grp.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #define BUFLEN 4096
82
83 int
84 main(void)
85 {
86 struct group grp, *grpp;
87 char buf[BUFLEN];
88 int i;
89
90 setgrent();
91 while (1) {
92 i = getgrent_r(&grp, buf, BUFLEN, &grpp);
93 if (i)
94 break;
95 printf("%s (%d):", grpp->gr_name, grpp->gr_gid);
96 for (i = 0; ; i++) {
97 if (grpp->gr_mem[i] == NULL)
98 break;
99 printf(" %s", grpp->gr_mem[i]);
100 }
101 printf("\n");
102 }
103 endgrent();
104 exit(EXIT_SUCCESS);
105 }
106
108 fgetgrent(3), getgrent(3), getgrgid(3), getgrnam(3), putgrent(3),
109 group(5)
110
112 This page is part of release 3.53 of the Linux man-pages project. A
113 description of the project, and information about reporting bugs, can
114 be found at http://www.kernel.org/doc/man-pages/.
115
116
117
118GNU 2010-10-21 GETGRENT_R(3)