From 7008f74214aefc088eb15f0ff7656b31e70c0cbd Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 1 Jan 2022 17:53:57 +0100 Subject: [PATCH] LibCore: Add syscall wrappers for getpwuid() and getgrgid() --- Userland/Libraries/LibCore/System.cpp | 20 ++++++++++++++++++++ Userland/Libraries/LibCore/System.h | 2 ++ 2 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 37cca47260..17ff8023af 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -403,6 +403,26 @@ ErrorOr chown(StringView pathname, uid_t uid, gid_t gid) #endif } +ErrorOr> getpwuid(uid_t uid) +{ + errno = 0; + if (auto* pwd = ::getpwuid(uid)) + return *pwd; + if (errno) + return Error::from_syscall("getpwuid"sv, -errno); + return Optional {}; +} + +ErrorOr> getgrgid(gid_t gid) +{ + errno = 0; + if (auto* grp = ::getgrgid(gid)) + return *grp; + if (errno) + return Error::from_syscall("getgrgid"sv, -errno); + return Optional {}; +} + ErrorOr> getpwnam(StringView name) { errno = 0; diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 1353108885..318cdde735 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -70,6 +70,8 @@ ErrorOr lchown(StringView pathname, uid_t uid, gid_t gid); ErrorOr chown(StringView pathname, uid_t uid, gid_t gid); ErrorOr> getpwnam(StringView name); ErrorOr> getgrnam(StringView name); +ErrorOr> getpwuid(uid_t); +ErrorOr> getgrgid(gid_t); ErrorOr clock_settime(clockid_t clock_id, struct timespec* ts); ErrorOr posix_spawnp(StringView const path, posix_spawn_file_actions_t* const file_actions, posix_spawnattr_t* const attr, char* const arguments[], char* const envp[]); ErrorOr waitpid(pid_t waitee, int* wstatus, int options);