diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index b5e6290c300..7f91c311f7b 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -328,22 +328,23 @@ ErrorOr anon_create([[maybe_unused]] size_t size, [[maybe_unused]] int opti return fd; } -ErrorOr open(StringView path, int options, ...) +ErrorOr open(StringView path, int options, mode_t mode) +{ + return openat(AT_FDCWD, path, options, mode); +} + +ErrorOr openat(int fd, StringView path, int options, mode_t mode) { 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 }; + Syscall::SC_open_params params { fd, { 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 = path; - int rc = ::open(path_string.characters(), options, mode); + int rc = ::openat(fd, path_string.characters(), options, mode); if (rc < 0) return Error::from_syscall("open"sv, -errno); return rc; diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 24898608837..71dcfc6b4cd 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -68,7 +69,8 @@ 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 anon_create(size_t size, int options); -ErrorOr open(StringView path, int options, ...); +ErrorOr open(StringView path, int options, mode_t mode = 0); +ErrorOr openat(int fd, StringView path, int options, mode_t mode = 0); ErrorOr close(int fd); ErrorOr ftruncate(int fd, off_t length); ErrorOr stat(StringView path);