From 415eb17490607ac5a133ba62d0ea733393a30131 Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Mon, 24 Oct 2022 22:15:58 -0600 Subject: [PATCH] LibCore: Add Core::System::readlink --- Userland/Libraries/LibCore/System.cpp | 21 +++++++++++++++++++++ Userland/Libraries/LibCore/System.h | 1 + 2 files changed, 22 insertions(+) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 170a0c7336..f060bb87c3 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -1326,4 +1326,25 @@ ErrorOr access(StringView pathname, int mode) #endif } +ErrorOr readlink(StringView pathname) +{ + // FIXME: Try again with a larger buffer. + char data[PATH_MAX]; +#ifdef AK_OS_SERENITY + Syscall::SC_readlink_params small_params { + { pathname.characters_without_null_termination(), pathname.length() }, + { data, sizeof(data) } + }; + int rc = syscall(SC_readlink, &small_params); + HANDLE_SYSCALL_RETURN_VALUE("readlink", rc, String(data, rc)); +#else + String path_string = pathname; + int rc = ::readlink(path_string.characters(), data, sizeof(data)); + if (rc == -1) + return Error::from_syscall("readlink"sv, -errno); + + return String(data, rc); +#endif +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 12edc46638..9b1b8345e0 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -193,5 +193,6 @@ ErrorOr posix_openpt(int flags); ErrorOr grantpt(int fildes); ErrorOr unlockpt(int fildes); ErrorOr access(StringView pathname, int mode); +ErrorOr readlink(StringView pathname); }