From 3db9979e40971bdde9cff828cd3382fa7332f3f8 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Nov 2021 12:22:21 +0100 Subject: [PATCH] LibCore: Add syscall wrapper for stat() --- Userland/Libraries/LibCore/System.cpp | 18 ++++++++++++++++++ Userland/Libraries/LibCore/System.h | 1 + 2 files changed, 19 insertions(+) diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 05a7341f24..93f44a0a5e 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -130,4 +130,22 @@ ErrorOr ftruncate(int fd, off_t length) return {}; } +ErrorOr stat(StringView path) +{ + if (!path.characters_without_null_termination()) + return Error::from_syscall("stat"sv, -EFAULT); + + struct stat st = {}; +#ifdef __serenity__ + Syscall::SC_stat_params params { { path.characters_without_null_termination(), path.length() }, &st, AT_FDCWD, true }; + int rc = syscall(SC_stat, ¶ms); + HANDLE_SYSCALL_RETURN_VALUE("stat"sv, rc, st); +#else + String path_string = path; + if (::stat(path_string.characters(), &st) < 0) + return Error::from_syscall("stat"sv, -errno); + return st; +#endif +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index aa2a01d5f5..53dc7f578d 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -25,5 +25,6 @@ ErrorOr munmap(void* address, size_t); ErrorOr open(StringView path, int options, ...); ErrorOr close(int fd); ErrorOr ftruncate(int fd, off_t length); +ErrorOr stat(StringView path); }