LibCore: Add syscall wrapper for getpwnam()

This commit is contained in:
Kenneth Myhra 2021-11-29 22:03:19 +01:00 committed by Brian Gianforcaro
commit cd5063555e
Notes: sideshowbarker 2024-07-17 22:56:47 +09:00
2 changed files with 20 additions and 0 deletions

View file

@ -324,4 +324,22 @@ ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid)
#endif
}
ErrorOr<struct passwd> getpwnam(StringView name)
{
::setpwent();
if (errno)
return Error::from_syscall("getpwnam"sv, -errno);
while (auto* pw = ::getpwent()) {
if (errno)
return Error::from_syscall("getpwnam"sv, -errno);
if (pw->pw_name == name)
return *pw;
}
if (errno)
return Error::from_syscall("getpwnam"sv, -errno);
else
return Error::from_string_literal("getpwnam: Unknown username"sv);
}
}