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