1getpwent_r(3) Library Functions Manual getpwent_r(3)
2
3
4
6 getpwent_r, fgetpwent_r - get passwd file entry reentrantly
7
9 Standard C library (libc, -lc)
10
12 #include <pwd.h>
13
14 int getpwent_r(struct passwd *restrict pwbuf,
15 char buf[restrict .buflen], size_t buflen,
16 struct passwd **restrict pwbufp);
17 int fgetpwent_r(FILE *restrict stream, struct passwd *restrict pwbuf,
18 char buf[restrict .buflen], size_t buflen,
19 struct passwd **restrict pwbufp);
20
21 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
22
23 getpwent_r(),
24 Since glibc 2.19:
25 _DEFAULT_SOURCE
26 glibc 2.19 and earlier:
27 _BSD_SOURCE || _SVID_SOURCE
28
29 fgetpwent_r():
30 Since glibc 2.19:
31 _DEFAULT_SOURCE
32 glibc 2.19 and earlier:
33 _SVID_SOURCE
34
36 The functions getpwent_r() and fgetpwent_r() are the reentrant versions
37 of getpwent(3) and fgetpwent(3). The former reads the next passwd en‐
38 try from the stream initialized by setpwent(3). The latter reads the
39 next passwd entry from stream.
40
41 The passwd structure is defined in <pwd.h> as follows:
42
43 struct passwd {
44 char *pw_name; /* username */
45 char *pw_passwd; /* user password */
46 uid_t pw_uid; /* user ID */
47 gid_t pw_gid; /* group ID */
48 char *pw_gecos; /* user information */
49 char *pw_dir; /* home directory */
50 char *pw_shell; /* shell program */
51 };
52
53 For more information about the fields of this structure, see passwd(5).
54
55 The nonreentrant functions return a pointer to static storage, where
56 this static storage contains further pointers to user name, password,
57 gecos field, home directory and shell. The reentrant functions de‐
58 scribed here return all of that in caller-provided buffers. First of
59 all there is the buffer pwbuf that can hold a struct passwd. And next
60 the buffer buf of size buflen that can hold additional strings. The
61 result of these functions, the struct passwd read from the stream, is
62 stored in the provided buffer *pwbuf, and a pointer to this struct
63 passwd is returned in *pwbufp.
64
66 On success, these functions return 0 and *pwbufp is a pointer to the
67 struct passwd. On error, these functions return an error value and
68 *pwbufp is NULL.
69
71 ENOENT No more entries.
72
73 ERANGE Insufficient buffer space supplied. Try again with larger buf‐
74 fer.
75
77 For an explanation of the terms used in this section, see at‐
78 tributes(7).
79
80 ┌──────────────┬───────────────┬───────────────────────────────────────┐
81 │Interface │ Attribute │ Value │
82 ├──────────────┼───────────────┼───────────────────────────────────────┤
83 │getpwent_r() │ Thread safety │ MT-Unsafe race:pwent locale │
84 ├──────────────┼───────────────┼───────────────────────────────────────┤
85 │fgetpwent_r() │ Thread safety │ MT-Safe │
86 └──────────────┴───────────────┴───────────────────────────────────────┘
87 In the above table, pwent in race:pwent signifies that if any of the
88 functions setpwent(), getpwent(), endpwent(), or getpwent_r() are used
89 in parallel in different threads of a program, then data races could
90 occur.
91
93 Other systems use the prototype
94
95 struct passwd *
96 getpwent_r(struct passwd *pwd, char *buf, int buflen);
97
98 or, better,
99
100 int
101 getpwent_r(struct passwd *pwd, char *buf, int buflen,
102 FILE **pw_fp);
103
105 None.
106
108 These functions are done in a style resembling the POSIX version of
109 functions like getpwnam_r(3).
110
112 The function getpwent_r() is not really reentrant since it shares the
113 reading position in the stream with all other threads.
114
116 #define _GNU_SOURCE
117 #include <pwd.h>
118 #include <stdint.h>
119 #include <stdio.h>
120 #include <stdlib.h>
121
122 #define BUFLEN 4096
123
124 int
125 main(void)
126 {
127 struct passwd pw;
128 struct passwd *pwp;
129 char buf[BUFLEN];
130 int i;
131
132 setpwent();
133 while (1) {
134 i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
135 if (i)
136 break;
137 printf("%s (%jd)\tHOME %s\tSHELL %s\n", pwp->pw_name,
138 (intmax_t) pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
139 }
140 endpwent();
141 exit(EXIT_SUCCESS);
142 }
143
145 fgetpwent(3), getpw(3), getpwent(3), getpwnam(3), getpwuid(3), putp‐
146 went(3), passwd(5)
147
148
149
150Linux man-pages 6.04 2023-03-30 getpwent_r(3)