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); }