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 to getpwent(), getpwnam(3), or getpwuid(3) can absolutely overwrite it. so like… don't get attached. and do not pass that returned pointer to free(3). that's not it, bestie. that's not it at all 😭

πŸ”₯ ERRORS πŸ”₯

  • EINTR β€” a signal got caught; peep signal(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 the passwd struct. 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 locale
  • setpwent(), 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms