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       #define _GNU_SOURCE
10       #include <pwd.h>
11
12       int getpwent_r(struct passwd *pwbuf, char *buf,
13                      size_t buflen, struct passwd **pwbufp);
14
15       int fgetpwent_r(FILE *fp, struct passwd *pwbuf, char *buf,
16                       size_t buflen, struct passwd **pwbufp);
17

DESCRIPTION

19       The functions getpwent_r() and fgetpwent_r() are the reentrant versions
20       of getpwent(3) and fgetpwent(3).  The  former  reads  the  next  passwd
21       entry from the stream initialized by setpwent(3).  The latter reads the
22       next passwd entry from the stream fp given as parameter.
23
24       The passwd structure is defined in <pwd.h> as follows:
25
26              struct passwd {
27                    char    *pw_name;      /* user name */
28                    char    *pw_passwd;    /* user password */
29                    uid_t   pw_uid;        /* user ID */
30                    gid_t   pw_gid;        /* group ID */
31                    char    *pw_gecos;     /* real name */
32                    char    *pw_dir;       /* home directory */
33                    char    *pw_shell;     /* shell program */
34              };
35
36       The non-reentrant functions return a pointer to static  storage,  where
37       this  static  storage contains further pointers to user name, password,
38       gecos  field,  home  directory  and  shell.   The  reentrant  functions
39       described  here return all of that in caller-provided buffers. First of
40       all there is the buffer pwbuf that can hold a struct passwd.  And  next
41       the  buffer  buf  of size buflen that can hold additional strings.  The
42       result of these functions, the struct passwd read from the  stream,  is
43       stored  in  the  provided  buffer  *pwbuf, and a pointer to this struct
44       passwd is returned in *pwbufp.
45

RETURN VALUE

47       On success, these functions return 0 and *pwbufp is a  pointer  to  the
48       struct  passwd.   On  error,  these functions return an error value and
49       *pwbufp is NULL.
50

ERRORS

52       ENOENT No more entries.
53
54       ERANGE Insufficient buffer space supplied. Try again with  larger  buf‐
55              fer.
56

EXAMPLE

58       #define _GNU_SOURCE
59       #include <pwd.h>
60       #include <stdio.h>
61       #define BUFLEN 4096
62
63       int main() {
64             struct passwd pw, *pwp;
65             char buf[BUFLEN];
66             int i;
67
68             setpwent();
69             while (1) {
70                   i = getpwent_r(&pw, buf, BUFLEN, &pwp);
71                   if (i)
72                         break;
73                   printf("%s (%d)\tHOME %s\tSHELL %s\n",
74                         pwp->pw_name, pwp->pw_uid,
75                         pwp->pw_dir, pwp->pw_shell);
76             }
77             endpwent();
78             return 0;
79       }
80

CONFORMING TO

82       These  functions  are  GNU  extensions,  done in a style resembling the
83       POSIX version of functions like getpwnam_r(3).  Other systems use  pro‐
84       totype
85
86           struct passwd *
87           getpwent_r(struct passwd *pwd, char *buf, int buflen);
88
89       or, better,
90
91           int
92           getpwent_r(struct passwd *pwd, char *buf, int buflen,
93                      FILE **pw_fp);
94
95

NOTES

97       The  function  getpwent_r() is not really reentrant since it shares the
98       reading position in the stream with all other threads.
99

SEE ALSO

101       fgetpwent(3), getpw(3), getpwent(3),  getpwnam(3),  getpwuid(3),  putp‐
102       went(3), passwd(5), feature_test_macros(7)
103
104
105
106GNU                               2003-11-15                     GETPWENT_R(3)
Impressum