summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorKenneth Myhra <kennethmyhra@gmail.com>2021-11-29 22:03:19 +0100
committerBrian Gianforcaro <b.gianfo@gmail.com>2021-12-11 15:10:42 -0800
commitcd5063555e92de8f273d92b35e64968e94205a6c (patch)
tree91b7f091be30863d0c99f99debe959fee0d0636f /Userland/Libraries
parentc8080fc2ca9185570a7d61935b0f28beb2184f50 (diff)
downloadserenity-cd5063555e92de8f273d92b35e64968e94205a6c.zip
LibCore: Add syscall wrapper for getpwnam()
Diffstat (limited to 'Userland/Libraries')
-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 bd6b00509a..500b39b39f 100644
--- a/Userland/Libraries/LibCore/System.cpp
+++ b/Userland/Libraries/LibCore/System.cpp
@@ -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);
+}
+
}
diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h
index dbf9d27026..f6f5db9a0b 100644
--- a/Userland/Libraries/LibCore/System.h
+++ b/Userland/Libraries/LibCore/System.h
@@ -8,6 +8,7 @@
#pragma once
#include <AK/Error.h>
+#include <pwd.h>
#include <signal.h>
#include <sys/stat.h>
#include <termios.h>
@@ -45,5 +46,6 @@ ErrorOr<struct termios> tcgetattr(int fd);
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);
}