summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Myhra <kennethmyhra@gmail.com>2021-12-06 21:24:18 +0100
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-12-11 15:10:42 -0800
commitc4bd46023b315b202a1d4d14720018a700ac57a3 (patch)
tree7fa99dd3e47456641c359e2b23b9e0eaa5d9cb84
parentcd5063555e92de8f273d92b35e64968e94205a6c (diff)
downloadserenity-c4bd46023b315b202a1d4d14720018a700ac57a3.zip
LibCore: Add syscall wrapper for getgrnam()
-rw-r--r--Userland/Libraries/LibCore/System.cpp18
-rw-r--r--Userland/Libraries/LibCore/System.h2
2 files changed, 20 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp
index 500b39b39f..55801d6e91 100644
--- a/Userland/Libraries/LibCore/System.cpp
+++ b/Userland/Libraries/LibCore/System.cpp
@@ -342,4 +342,22 @@ ErrorOr<struct passwd> getpwnam(StringView name)
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);
+}
+
}
diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h
index f6f5db9a0b..0905fbb294 100644
--- a/Userland/Libraries/LibCore/System.h
+++ b/Userland/Libraries/LibCore/System.h
@@ -8,6 +8,7 @@
#pragma once
#include <AK/Error.h>
+#include <grp.h>
#include <pwd.h>
#include <signal.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> chown(StringView pathname, uid_t uid, gid_t gid);
ErrorOr<struct passwd> getpwnam(StringView name);
+ErrorOr<struct group> getgrnam(StringView name);
}