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