#include <pwd.h> int getpwent_r(struct passwd *pwbuf, char *buf,
size_t buflen, struct passwd **pwbufp); int fgetpwent_r(FILE *fp, struct passwd *pwbuf, char *buf,
size_t buflen, struct passwd **pwbufp);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
getpwent_r(),
_BSD_SOURCE || _SVID_SOURCE
fgetpwent_r():
_SVID_SOURCE
The passwd structure is defined in <pwd.h> as follows:
struct passwd { char *pw_name; /* username */ char *pw_passwd; /* user password */ uid_t pw_uid; /* user ID */ gid_t pw_gid; /* group ID */ char *pw_gecos; /* real name */ char *pw_dir; /* home directory */ char *pw_shell; /* shell program */ };
The nonreentrant functions return a pointer to static storage, where this static storage contains further pointers to user name, password, gecos field, home directory and shell. The reentrant functions described here return all of that in caller-provided buffers. First of all there is the buffer pwbuf that can hold a struct passwd. And next the buffer buf of size buflen that can hold additional strings. The result of these functions, the struct passwd read from the stream, is stored in the provided buffer *pwbuf, and a pointer to this struct passwd is returned in *pwbufp.
struct passwd * getpwent_r(struct passwd *pwd, char *buf, int buflen);
or, better,
int getpwent_r(struct passwd *pwd, char *buf, int buflen, FILE **pw_fp);
#define _GNU_SOURCE #include <pwd.h> #include <stdio.h> #define BUFLEN 4096 int main(void) { struct passwd pw, *pwp; char buf[BUFLEN]; int i; setpwent(); while (1) { i = getpwent_r(&pw, buf, BUFLEN, &pwp); if (i) break; printf("%s (%d)\tHOME %s\tSHELL %s\n", pwp->pw_name, pwp->pw_uid, pwp->pw_dir, pwp->pw_shell); } endpwent(); exit(EXIT_SUCCESS); }