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
33 entry 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
53 described 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
73 attributes(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 #define BUFLEN 4096
110
111 int
112 main(void)
113 {
114 struct passwd pw, *pwp;
115 char buf[BUFLEN];
116 int i;
117
118 setpwent();
119 while (1) {
120 i = getpwent_r(&pw, buf, BUFLEN, &pwp);
121 if (i)
122 break;
123 printf("%s (%d)\tHOME %s\tSHELL %s\n", pwp->pw_name,
124 pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
125 }
126 endpwent();
127 exit(EXIT_SUCCESS);
128 }
129
131 fgetpwent(3), getpw(3), getpwent(3), getpwnam(3), getpwuid(3), putp‐
132 went(3), passwd(5)
133
135 This page is part of release 5.02 of the Linux man-pages project. A
136 description of the project, information about reporting bugs, and the
137 latest version of this page, can be found at
138 https://www.kernel.org/doc/man-pages/.
139
140
141
142GNU 2017-09-15 GETPWENT_R(3)