getpwent_r(3)
β¨ getpwent_r β get passwd file entry reentrantly β¨
okay bestie, welcome to the getpwent_r(3) page from the Library Functions Manual π this is the reentrant girlie version of the passwd-reading functions and she's lowkey iconic. pull up a chair, we're doing this together π«Ά
π LIBRARY
Standard C library β literally libc, link it with -lc π
πΈ SYNOPSIS
#include <pwd.h>
int getpwent_r(size_t size;
struct passwd *restrict pwbuf,
char buf[restrict size], size_t size,
struct passwd **restrict pwbufp);
int fgetpwent_r(size_t size;
FILE *restrict stream, struct passwd *restrict pwbuf,
char buf[restrict size], size_t size,
struct passwd **restrict pwbufp);
π§ Feature Test Macro Requirements for glibc
(see feature_test_macros(7) if you need the lore, iykyk)
getpwent_r()- Since glibc 2.19:
_DEFAULT_SOURCE - glibc 2.19 and earlier:
_BSD_SOURCE || _SVID_SOURCE
- Since glibc 2.19:
fgetpwent_r()- Since glibc 2.19:
_DEFAULT_SOURCE - glibc 2.19 and earlier:
_SVID_SOURCE
- Since glibc 2.19:
π DESCRIPTION
So here's the tea: getpwent_r() and fgetpwent_r() are the reentrant versions of getpwent(3) and fgetpwent(3). The first one (getpwent_r()) reads the next passwd entry from the stream that got initialized by setpwent(3). The second one (fgetpwent_r()) reads the next passwd entry from stream. simple as that, no cap β¨
The passwd structure lives in <pwd.h> and looks like this:
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; /* user information */
char *pw_dir; /* home directory */
char *pw_shell; /* shell program */
};
Wanna know more about all those fields? Go read passwd(5), she explains everything π¦
Now, the nonreentrant functions hand you back a pointer to static storage, and that static storage contains further pointers to user name, password, gecos field, home directory, and shell. kind of a whole house of cards honestly. The reentrant functions described here though? They give you all of that inside buffers you provide. main character energy π
First up there's the buffer pwbuf, which can hold a struct passwd. And then there's the buffer buf of size size that can hold additional strings. The result of these functions β the struct passwd read from the stream β gets stored in the provided buffer *pwbuf, and a pointer to this struct passwd is returned in *pwbufp.
π RETURN VALUE
- On success: these functions return
0and*pwbufpis a pointer to thestruct passwd. slay π - On error: these functions return an error value and
*pwbufpisNULL. π
π₯ ERRORS
ENOENTβ No more entries. that's it, that's the whole vibe, we ran out πERANGEβ Insufficient buffer space supplied. Try again with larger buffer. bestie your buffer is simply not big enough, size up π«Ά
π ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
getpwent_r()β Thread safety:MT-Unsafe race:pwent localefgetpwent_r()β Thread safety:MT-Safe
In the table above, pwent in race:pwent signifies that if any of the functions setpwent(), getpwent(), endpwent(), or getpwent_r() are used in parallel in different threads of a program, then data races could occur. so likeβ¦ don't do that π
π VERSIONS
Other systems use this prototype:
struct passwd *
getpwent_r(struct passwd *pwd, char buf[.size], int size);
or, better:
int
getpwent_r(struct passwd *pwd, char buf[.size], int size,
FILE **pw_fp);
not us though, we do it our way π
β STANDARDS
None. literally none. she's built different, no standard claims her π
π HISTORY
These functions are done in a style resembling the POSIX version of functions like getpwnam_r(3). periodt.
π NOTES
The function getpwent_r() is not really reentrant since it shares the reading position in the stream with all other threads. yeah. the name is giving reentrant but the behaviour is not, we're just being honest with you here bestie π
πΈ EXAMPLES
#define _GNU_SOURCE
#include <pwd.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFLEN 4096
int
main(void)
{
struct passwd pw;
struct passwd *pwp;
char buf[BUFLEN];
int i;
setpwent();
for (;;) {
i = getpwent_r(&pw, buf, sizeof(buf), &pwp);
if (i)
break;
printf("%s (%jd)\tHOME %s\tSHELL %s\n", pwp->pw_name,
(intmax_t) pwp->pw_uid, pwp->pw_dir, pwp->pw_shell);
}
endpwent();
exit(EXIT_SUCCESS);
}
π¦ SEE ALSO
fgetpwent(3), getpw(3), getpwent(3), getpwnam(3), getpwuid(3), putpwent(3), passwd(5)
okay that's the whole page, you ate that π this was Linux man-pages 6.20, dated 2026-02-14 β shoutout to the man-pages girlies for keeping the lore alive, mwah π«Άβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: GPL-2.0-or-later
- Copyright 2003, Andries E. Brouwer <aeb@cwi.nl>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms