getpw(3)

πŸ’– getpw β€” bestie we need to talk πŸ’–

okay so first of all, hi, welcome to getpw(3) β€” yeah, that's the manual page name, the 3 means it lives in the library functions section, and if you're here it's because you found this little fossil in some codebase and went "wait what". valid. sit down. 🫢


✨ NAME ✨

getpw β€” reconstruct password line entry. that's it, that's the whole job description. she reconstructs. she does not create. she does not innovate. reconstruction only. πŸ’…

πŸ“š LIBRARY πŸ“š

Standard C library (libc, -lc). the classic. the blueprint. the mother of all libraries, no cap.

🌸 SYNOPSIS 🌸

#define _GNU_SOURCE             /* See feature_test_macros(7) */
#include <sys/types.h>
#include <pwd.h>

[[deprecated]] int getpw(uid_t uid, char *buf);

okay bestie let me translate: you gotta define _GNU_SOURCE first (peep feature_test_macros(7) for the whole lore), pull in <sys/types.h> and <pwd.h>, and then… the [[deprecated]] sitting right there in front of the function signature like a giant "don't touch this" sticker. she's marked. she's over. and yet. 😭

πŸ¦‹ DESCRIPTION πŸ¦‹

so what does she actually do. The getpw() function reconstructs the password line entry for the given user ID uid in the buffer buf. you hand her a UID, you hand her a buffer, she fills the buffer with a whole recreated password line. iconic behavior, questionable execution.

The returned buffer contains a line of format:

name:passwd:uid:gid:gecos:dir:shell

yes, colons and all. the classic seven-field lineup. πŸ“

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;      /* user information */
    char   *pw_dir;        /* home directory */
    char   *pw_shell;      /* shell program */
};

For more information about the fields of this structure, see passwd(5). go read the sister page, she has the details, this page is not gonna hold your hand through every field, that's not the vibe here. πŸ’œ

πŸ‘‘ RETURN VALUE πŸ‘‘

The getpw() function returns 0 on success; on error, it returns -1, and errno is set to indicate the error. standard king behavior, very on brand.

and here's the spicy bit πŸ‘€ β€” if uid is not found in the password database, getpw() returns -1, sets errno to 0, and leaves buf unchanged. not ENOENT, not some sad lil errno, literally 0. zero. which is technically not an error code. she's not okay and she's telling you via errno = 0. πŸ’€

🌈 ERRORS 🌈

  • 0 or ENOENT β€” No user corresponding to uid. (the zero thing again. yes, both. it's giving chaos.)
  • EINVAL β€” buf is NULL. girl you gotta give her somewhere to write. 😭
  • ENOMEM β€” Insufficient memory to allocate passwd structure. she ran out of room, it happens, budget cuts.

πŸŽ€ FILES πŸŽ€

  • /etc/passwd β€” password database file. the source of all truths, the registry of souls, the file everyone edits and immediately regrets.

πŸ’… ATTRIBUTES πŸ’…

For an explanation of the terms used in this section, see attributes(7).

  • Interface: getpw()
  • Attribute: Thread safety
  • Value: MT-Safe locale

so like, thread-safe, but only because of locale β€” meaning it depends on locale behavior. do with that information what you will. ✨

⭐ STANDARDS ⭐

None. like. nothing. no standard claims her. she is officially un-standardized. a free agent. a rogue. πŸ¦‹

🌸 HISTORY 🌸

SVr2. ancient. older than most of y'all's parents' shell scripts. she has history, literally, that's the name of the section, i'm not even being cute right now. she's been here since System V Release 2 and she has seen some things. πŸ’œ

πŸ’€ BUGS πŸ’€

okay bestie this is the part where i need you to actually pay attention and not scroll. 🚨

The getpw() function is dangerous as it may overflow the provided buffer buf. It is obsoleted by getpwuid(3).

like, genuinely dangerous. buffer overflow dangerous. "your program is now the attacker's program" dangerous. she will not check the size of buf. she will not warn you. she will write past the end of it and just assume you knew. do not use this function. use getpwuid(3). the manpage is telling you to use the other one. i am telling you to use the other one. we are all telling you. πŸ˜­πŸ™ˆ

πŸ’– SEE ALSO πŸ’–

endpwent(3), fgetpwent(3), getpwent(3), getpwnam(3), getpwuid(3), putpwent(3), setpwent(3), passwd(5) β€” the whole friend group. getpwuid(3) is the one who actually has her life together, the rest are the ensemble cast. go say hi. 🫢


and that's the page!! this masterpiece ships as part of Linux man-pages 6.20, dated 2026-02-08 β€” so like, girlies are still maintaining this archive and honestly? obsessed. stay safe out there, use getpwuid(3), and never trust a function with a [[deprecated]] sticker and a buffer overflow in the same body. 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: Linux-man-pages-copyleft
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms