From 431bd069f0cc66abec8b27b94289955310db8679 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 1 Jan 2022 18:26:17 +0100 Subject: LibCore: Add Core::System wrappers for getspent() and getspnam() --- Userland/Libraries/LibCore/System.cpp | 25 +++++++++++++++++++++++++ Userland/Libraries/LibCore/System.h | 9 +++++++++ 2 files changed, 34 insertions(+) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 17ff8023af..3cddacc95e 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -107,6 +107,31 @@ ErrorOr ptrace(int request, pid_t tid, void* address, void* data) } #endif +#ifndef AK_OS_BSD_GENERIC +ErrorOr> getspent() +{ + errno = 0; + if (auto* spwd = ::getspent()) + return *spwd; + if (errno) + return Error::from_syscall("getspent"sv, -errno); + return Optional {}; +} + +ErrorOr> getspnam(StringView name) +{ + errno = 0; + ::setspent(); + while (auto* spwd = ::getspent()) { + if (spwd->sp_namp == name) + return *spwd; + } + if (errno) + return Error::from_syscall("getspnam"sv, -errno); + return Optional {}; +} +#endif + #ifndef AK_OS_MACOS ErrorOr accept4(int sockfd, sockaddr* address, socklen_t* address_length, int flags) { diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 318cdde735..8630e58485 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -22,6 +22,10 @@ #include #include +#ifndef AK_OS_BSD_GENERIC +# include +#endif + namespace Core::System { #ifdef __serenity__ @@ -35,6 +39,11 @@ ErrorOr mount(int source_fd, StringView target, StringView fs_type, int fl ErrorOr ptrace(int request, pid_t tid, void* address, void* data); #endif +#ifndef AK_OS_BSD_GENERIC +ErrorOr> getspent(); +ErrorOr> getspnam(StringView name); +#endif + #ifndef AK_OS_MACOS ErrorOr accept4(int sockfd, struct sockaddr*, socklen_t*, int flags); #endif -- cgit v1.2.3