getpwent(3)
β¨ getpwent, setpwent, endpwent β bestie edition β¨
okay so this is a page from the Library Functions Manual (that's the manual name, hope that's helpful π) and it's literally all about reading entries out of the password database. no cap. let's go π
πΈ NAME πΈ
getpwent, setpwent, endpwent β get password file entry
π LIBRARY π
Standard C library (libc, -lc)
π SYNOPSIS π
#include <sys/types.h>
#include <pwd.h>
struct passwd *getpwent(void);
void setpwent(void);
void endpwent(void);
π§ Feature Test Macro Requirements for glibc π§
(see feature_test_macros(7) if you're nosy, queen)
getpwent(), setpwent(), endpwent():
_XOPEN_SOURCE >= 500
|| /* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
π DESCRIPTION π
so getpwent() gives you back a pointer to a struct that's basically a password-database record but broken out into its little fields β think the local password file /etc/passwd, or NIS, or LDAP, whichever one your system is feeling that day β¨. the first time you call getpwent(), it hands you the first entry. every call after that? it just keeps handing you the next ones, one by one, like a lil assembly line π.
setpwent() does a rewind, babe β it yeets you right back to the beginning of the password database βͺ.
endpwent() is the closer. once you're done processing everything, you call it to close the password database. it's giving "thanks for coming to my TED talk" π«Ά.
the passwd struct lives in <pwd.h> and looks exactly like this, no edits, no notes:
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 */
};
want the tea on what all those fields actually mean? go read passwd(5) π
π¦ RETURN VALUE π¦
getpwent() returns a pointer to a passwd struct β or NULL if there's literally nothing left, or if an error happened π. if an error happens, errno gets set so you know what went down. and listen: if you actually want to check errno after the call, you gotta set it to zero before the call. otherwise you're just reading stale vibes, fr fr.
β οΈ the return value might point at a static area, and later calls togetpwent(),getpwnam(3), orgetpwuid(3)can absolutely overwrite it. so likeβ¦ don't get attached. and do not pass that returned pointer tofree(3). that's not it, bestie. that's not it at all π
π₯ ERRORS π₯
EINTRβ a signal got caught; peepsignal(7)for the deets.EIOβ I/O error. the vibes are off. something broke.EMFILEβ you hit the per-process limit on how many file descriptors can be open at once π« .ENFILEβ you hit the system-wide limit on total open files. everybody's opening files, apparently.ENOMEMβ not enough memory to allocate thepasswdstruct. she's starving π.ERANGEβ not enough buffer space was supplied. give her more room.
π FILES π
/etc/passwdβ the local password database file π
π ATTRIBUTES π
for what all these words even mean, see attributes(7). here's the rundown because tables aren't a vibe here, so we're doing bullets:
getpwent()β Thread safety:MT-Unsafe race:pwent race:pwentbuf localesetpwent(),endpwent()β Thread safety:MT-Unsafe race:pwent locale
and real quick, what does pwent in race:pwent even mean? it means: if any of setpwent(), getpwent(), or endpwent() get used in parallel across different threads of your program, then data races could occur. so be careful, babes. parallel here does not mean bestie behavior π.
π VERSIONS π
the pw_gecos field is not specified in POSIX β but she's present on most implementations anyway. iconic of her honestly β¨.
β STANDARDS β
POSIX.1-2008.
π HISTORY π
POSIX.1-2001, SVr4, 4.3BSD. a whole legacy, no cap.
π«Ά SEE ALSO π«Ά
fgetpwent(3), getpw(3), getpwent_r(3), getpwnam(3), getpwuid(3), putpwent(3), passwd(5)
okay that's the whole page bestie, you ate that π π so this lil masterpiece is from Linux man-pages 6.20, dated 2026-02-08 β mwah, go forth and get those password entries πβ¨π¦
π¦ 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: Linux-man-pages-copyleft
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms