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 *fp, 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(), _BSD_SOURCE || _SVID_SOURCE
20 fgetpwent_r(): _SVID_SOURCE
21
23 The functions getpwent_r() and fgetpwent_r() are the reentrant versions
24 of getpwent(3) and fgetpwent(3). The former reads the next passwd
25 entry from the stream initialized by setpwent(3). The latter reads the
26 next passwd entry from the stream fp.
27
28 The passwd structure is defined in <pwd.h> as follows:
29
30 struct passwd {
31 char *pw_name; /* username */
32 char *pw_passwd; /* user password */
33 uid_t pw_uid; /* user ID */
34 gid_t pw_gid; /* group ID */
35 char *pw_gecos; /* user information */
36 char *pw_dir; /* home directory */
37 char *pw_shell; /* shell program */
38 };
39
40 For more information about the fields of this structure, see passwd(5).
41
42 The nonreentrant functions return a pointer to static storage, where
43 this static storage contains further pointers to user name, password,
44 gecos field, home directory and shell. The reentrant functions
45 described here return all of that in caller-provided buffers. First of
46 all there is the buffer pwbuf that can hold a struct passwd. And next
47 the buffer buf of size buflen that can hold additional strings. The
48 result of these functions, the struct passwd read from the stream, is
49 stored in the provided buffer *pwbuf, and a pointer to this struct
50 passwd is returned in *pwbufp.
51
53 On success, these functions return 0 and *pwbufp is a pointer to the
54 struct passwd. On error, these functions return an error value and
55 *pwbufp is NULL.
56
58 ENOENT No more entries.
59
60 ERANGE Insufficient buffer space supplied. Try again with larger buf‐
61 fer.
62
64 These functions are GNU extensions, done in a style resembling the
65 POSIX version of functions like getpwnam_r(3). Other systems use pro‐
66 totype
67
68 struct passwd *
69 getpwent_r(struct passwd *pwd, char *buf, int buflen);
70
71 or, better,
72
73 int
74 getpwent_r(struct passwd *pwd, char *buf, int buflen,
75 FILE **pw_fp);
76
78 The function getpwent_r() is not really reentrant since it shares the
79 reading position in the stream with all other threads.
80
82 #define _GNU_SOURCE
83 #include <pwd.h>
84 #include <stdio.h>
85 #define BUFLEN 4096
86
87 int
88 main(void)
89 {
90 struct passwd pw, *pwp;
91 char buf[BUFLEN];
92 int i;
93
94 setpwent();
95 while (1) {
96 i = getpwent_r(&pw, buf, BUFLEN, &pwp);
97 if (i)
98 break;
99 printf("%s (%d)\tHOME %s\tSHELL %s\n", pwp->pw_name,
100 pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
101 }
102 endpwent();
103 exit(EXIT_SUCCESS);
104 }
105
107 fgetpwent(3), getpw(3), getpwent(3), getpwnam(3), getpwuid(3), putp‐
108 went(3), passwd(5)
109
111 This page is part of release 3.53 of the Linux man-pages project. A
112 description of the project, and information about reporting bugs, can
113 be found at http://www.kernel.org/doc/man-pages/.
114
115
116
117GNU 2010-10-21 GETPWENT_R(3)