1GETPWENT_R(3)              Linux Programmer's Manual             GETPWENT_R(3)
2
3
4

NAME

6       getpwent_r, fgetpwent_r - get passwd file entry reentrantly
7

SYNOPSIS

9       #include <pwd.h>
10
11       int getpwent_r(struct passwd *restrict pwbuf,
12                      char *restrict buf, size_t buflen,
13                      struct passwd **restrict pwbufp);
14       int fgetpwent_r(FILE *restrict stream, struct passwd *restrict pwbuf,
15                      char *restrict buf, size_t buflen,
16                      struct passwd **restrict pwbufp);
17
18   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20       getpwent_r(),
21           Since glibc 2.19:
22               _DEFAULT_SOURCE
23           Glibc 2.19 and earlier:
24               _BSD_SOURCE || _SVID_SOURCE
25
26       fgetpwent_r():
27           Since glibc 2.19:
28               _DEFAULT_SOURCE
29           Glibc 2.19 and earlier:
30               _SVID_SOURCE
31

DESCRIPTION

33       The functions getpwent_r() and fgetpwent_r() are the reentrant versions
34       of getpwent(3) and fgetpwent(3).  The former reads the next passwd  en‐
35       try  from  the stream initialized by setpwent(3).  The latter reads the
36       next passwd entry from stream.
37
38       The passwd structure is defined in <pwd.h> as follows:
39
40           struct passwd {
41               char    *pw_name;      /* username */
42               char    *pw_passwd;    /* user password */
43               uid_t    pw_uid;       /* user ID */
44               gid_t    pw_gid;       /* group ID */
45               char    *pw_gecos;     /* user information */
46               char    *pw_dir;       /* home directory */
47               char    *pw_shell;     /* shell program */
48           };
49
50       For more information about the fields of this structure, see passwd(5).
51
52       The nonreentrant functions return a pointer to  static  storage,  where
53       this  static  storage contains further pointers to user name, password,
54       gecos field, home directory and shell.   The  reentrant  functions  de‐
55       scribed  here  return all of that in caller-provided buffers.  First of
56       all there is the buffer pwbuf that can hold a struct passwd.  And  next
57       the  buffer  buf  of size buflen that can hold additional strings.  The
58       result of these functions, the struct passwd read from the  stream,  is
59       stored  in  the  provided  buffer  *pwbuf, and a pointer to this struct
60       passwd is returned in *pwbufp.
61

RETURN VALUE

63       On success, these functions return 0 and *pwbufp is a  pointer  to  the
64       struct  passwd.   On  error,  these functions return an error value and
65       *pwbufp is NULL.
66

ERRORS

68       ENOENT No more entries.
69
70       ERANGE Insufficient buffer space supplied.  Try again with larger  buf‐
71              fer.
72

ATTRIBUTES

74       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
75       tributes(7).
76
77       ┌──────────────┬───────────────┬───────────────────────────────────────┐
78Interface     Attribute     Value                                 
79       ├──────────────┼───────────────┼───────────────────────────────────────┤
80getpwent_r()  │ Thread safety │ MT-Unsafe race:pwent locale           │
81       ├──────────────┼───────────────┼───────────────────────────────────────┤
82fgetpwent_r() │ Thread safety │ MT-Safe                               │
83       └──────────────┴───────────────┴───────────────────────────────────────┘
84       In the above table, pwent in race:pwent signifies that if  any  of  the
85       functions  setpwent(), getpwent(), endpwent(), or getpwent_r() are used
86       in parallel in different threads of a program, then  data  races  could
87       occur.
88

CONFORMING TO

90       These  functions  are  GNU  extensions,  done in a style resembling the
91       POSIX version of functions like getpwnam_r(3).  Other systems  use  the
92       prototype
93
94           struct passwd *
95           getpwent_r(struct passwd *pwd, char *buf, int buflen);
96
97       or, better,
98
99           int
100           getpwent_r(struct passwd *pwd, char *buf, int buflen,
101                      FILE **pw_fp);
102

NOTES

104       The  function  getpwent_r() is not really reentrant since it shares the
105       reading position in the stream with all other threads.
106

EXAMPLES

108       #define _GNU_SOURCE
109       #include <pwd.h>
110       #include <stdio.h>
111       #include <stdint.h>
112       #define BUFLEN 4096
113
114       int
115       main(void)
116       {
117           struct passwd pw;
118           struct passwd *pwp;
119           char buf[BUFLEN];
120           int i;
121
122           setpwent();
123           while (1) {
124               i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
125               if (i)
126                   break;
127               printf("%s (%jd)\tHOME %s\tSHELL %s\n", pwp->pw_name,
128                      (intmax_t) pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
129           }
130           endpwent();
131           exit(EXIT_SUCCESS);
132       }
133

SEE ALSO

135       fgetpwent(3), getpw(3), getpwent(3),  getpwnam(3),  getpwuid(3),  putp‐
136       went(3), passwd(5)
137

COLOPHON

139       This  page  is  part of release 5.12 of the Linux man-pages project.  A
140       description of the project, information about reporting bugs,  and  the
141       latest     version     of     this    page,    can    be    found    at
142       https://www.kernel.org/doc/man-pages/.
143
144
145
146GNU                               2021-03-22                     GETPWENT_R(3)
Impressum