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