diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 8d5195163c2..37e1099b66b 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -92,4 +93,26 @@ ErrorOr munmap(void* address, size_t size) return {}; } +ErrorOr open(StringView path, int options, ...) +{ + if (!path.characters_without_null_termination()) + return Error::from_syscall("open"sv, -EFAULT); + va_list ap; + va_start(ap, options); + auto mode = (mode_t)va_arg(ap, unsigned); + va_end(ap); +#ifdef __serenity__ + Syscall::SC_open_params params { AT_FDCWD, { path.characters_without_null_termination(), path.length() }, options, mode }; + int rc = syscall(SC_open, ¶ms); + HANDLE_SYSCALL_RETURN_VALUE("open"sv, rc, rc); +#else + // NOTE: We have to ensure that the path is null-terminated. + String path_string; + int rc = ::open(path_string.characters(), options, mode); + if (rc < 0) + return Error::from_syscall("open"sv, -errno); + return rc; +#endif +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 98517c73cce..07179cf78f3 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -22,5 +22,6 @@ ErrorOr fstat(int fd); ErrorOr fcntl(int fd, int command, ...); ErrorOr mmap(void* address, size_t, int protection, int flags, int fd, off_t, size_t alignment = 0, StringView name = {}); ErrorOr munmap(void* address, size_t); +ErrorOr open(StringView path, int options, ...); }