diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index bd6b00509ae..500b39b39ff 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -324,4 +324,22 @@ ErrorOr chown(StringView pathname, uid_t uid, gid_t gid) #endif } +ErrorOr 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 dbf9d270267..f6f5db9a0b9 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include #include #include @@ -45,5 +46,6 @@ ErrorOr tcgetattr(int fd); ErrorOr tcsetattr(int fd, int optional_actions, struct termios const&); ErrorOr chmod(StringView pathname, mode_t mode); ErrorOr chown(StringView pathname, uid_t uid, gid_t gid); +ErrorOr getpwnam(StringView name); }