user_from_uid(3)

πŸ“¦
version 0.12.2 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ user_from_uid β€” cache password and group entries ✨

okay bestie, so this is user_from_uid(3), and it lives in the Library Functions Manual section of the man pages (section 3, for the uninitiated πŸ’…). It's giving "look up names and IDs but, like, remember them so we don't keep asking" energy. Four little functions, one big glow-up. Let's go 🫢


🌸 LIBRARY 🌸

Utility functions from BSD systems β€” you're gonna want libbsd, linked with -lbsd. That's the whole vibe.


πŸŽ€ SYNOPSIS πŸŽ€

#include <pwd.h>
(See libbsd(7) for include usage.)

int
uid_from_user(const char *name, uid_t *uid);

const char *
user_from_uid(uid_t uid, int nouser);

#include <grp.h>

int
gid_from_group(const char *name, gid_t *gid);

const char *
group_from_gid(gid_t gid, int nogroup);
✨ psst β€” for how the includes actually work here, go read libbsd(7). it's the lore.

πŸ’– DESCRIPTION πŸ’–

πŸ¦‹ user_from_uid()

So user_from_uid() hands you back the user name that goes with the uid you passed in. And here's the iconic part: that user name gets cached, so if you call it a million times with the same uid, it does not keep bugging getpwuid(3) over and over. Main character efficiency, honestly.

But what if there's literally no user for that uid? 😭 Then:

  • if nouser is zero β†’ you get a pointer to a string representation of the uid (like, the number as text, cute)
  • if nouser is non-zero β†’ you get a null pointer. nothing. bye. πŸ’€

🌈 uid_from_user()

uid_from_user() is the reverse bestie β€” it gives you the user ID for the name you passed in. And yep, the user ID is cached too, so calling it repeatedly with the same name means you're not spamming getpwnam(3).

Return value check πŸ•΅οΈβ€β™€οΈ:

  • no user ID for that name? It returns -1. sad.
  • user ID found? It stores it at the location uid points to and returns 0. slay.

πŸ“ group_from_gid()

group_from_gid() is the group-flavoured twin. Give it a gid, it gives you back the group name. The group name is cached, so multiple calls with the same gid don't require extra calls to getgrgid(3). obsessed with the consistency tbh.

And if there's no group for that gid?

  • nogroup is zero β†’ pointer to a string representation of the gid
  • nogroup is non-zero β†’ null pointer, no group for you πŸ™ˆ

🧚 gid_from_group()

gid_from_group() completes the set ✨ it returns the group ID associated with the name argument. Group ID is cached, so repeated calls with the same name don't need extra calls to getgrnam(3).

And the returns:

  • no group ID for that name? β†’ -1
  • found it? β†’ stores the group ID where gid points, returns 0. periodt.

πŸ‘‘ SEE ALSO πŸ‘‘


⭐ HISTORY ⭐

The user_from_uid() and group_from_gid() functions first appeared in 4.4BSD. old money, honestly.

The uid_from_user() and gid_from_group() functions first appeared in NetBSD 1.4 and OpenBSD 6.4. newcomers who ate πŸ’…


and that's the whole page bestie, no cap πŸŽ€ this one's from the Debian crew, dated September 13, 2018 β€” so yeah, user_from_uid(3), written by the good people at Debian, and now explained to you with maximum sparkle. go forth and cache responsibly βœ¨πŸ¦‹πŸ’–

πŸ“¦ where this page comes from