user_from_uid(3)
β¨ 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
nouseris zero β you get a pointer to a string representation of theuid(like, the number as text, cute) - if
nouseris 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
uidpoints to and returns0. 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?
nogroupis zero β pointer to a string representation of thegidnogroupis 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
gidpoints, returns0. 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
- alpine 3.22: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.23: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- alpine 3.24: libbsd-doc 0.12.2-r0 Β· BSD-3-Clause
- arch rolling: libbsd 0.12.2-2 Β· custom
- debian trixie: libbsd-dev 0.12.2-2
- epel 10: libbsd-devel 0.12.2-4.el10_0 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- epel 9: libbsd-devel 0.12.2-1.el9 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 43: libbsd-devel 0.12.2-6.fc43 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- fedora 44: libbsd-devel 0.12.2-7.fc44 Β· Beerware AND BSD-2-Clause AND BSD-3-Clause AND ISC AND libutil-David-Nugent AND MIT AND LicenseRef-Fedora-Public-Domain
- nixos 26.05: libbsd 0.12.2
- ubuntu noble: libbsd-dev 0.12.1-1build1
- ubuntu noble: libbsd-dev 0.12.1-1build1.1
- ubuntu questing: libbsd-dev 0.12.2-2build1
- ubuntu resolute: libbsd-dev 0.12.2-2build2
- ubuntu stonking: libbsd-dev 0.12.2-3
- Copyright (c) 1989, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms