LibCore: Add syscall wrapper for getgrnam()

This commit is contained in:
Kenneth Myhra 2021-12-06 21:24:18 +01:00 committed by Brian Gianforcaro
parent cd5063555e
commit c4bd46023b
Notes: sideshowbarker 2024-07-17 22:56:43 +09:00
2 changed files with 20 additions and 0 deletions

View file

@ -342,4 +342,22 @@ ErrorOr<struct passwd> getpwnam(StringView name)
return Error::from_string_literal("getpwnam: Unknown username"sv); return Error::from_string_literal("getpwnam: Unknown username"sv);
} }
ErrorOr<struct group> getgrnam(StringView name)
{
::setgrent();
if (errno)
return Error::from_syscall("getgrnam"sv, -errno);
while (auto* gr = ::getgrent()) {
if (errno)
return Error::from_syscall("getgrnam"sv, -errno);
if (gr->gr_name == name)
return *gr;
}
if (errno)
return Error::from_syscall("getgrnam"sv, -errno);
else
return Error::from_string_literal("getgrnam: Unknown username"sv);
}
} }

View file

@ -8,6 +8,7 @@
#pragma once #pragma once
#include <AK/Error.h> #include <AK/Error.h>
#include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <signal.h> #include <signal.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -47,5 +48,6 @@ ErrorOr<void> tcsetattr(int fd, int optional_actions, struct termios const&);
ErrorOr<void> chmod(StringView pathname, mode_t mode); ErrorOr<void> chmod(StringView pathname, mode_t mode);
ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid); ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid);
ErrorOr<struct passwd> getpwnam(StringView name); ErrorOr<struct passwd> getpwnam(StringView name);
ErrorOr<struct group> getgrnam(StringView name);
} }