LibCore: Add syscall wrapper for open()

This commit is contained in:
Andreas Kling 2021-11-23 11:59:16 +01:00
parent 094fa900a3
commit 50416c286d
Notes: sideshowbarker 2024-07-18 00:48:57 +09:00
2 changed files with 24 additions and 0 deletions

View File

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <LibCore/System.h>
#include <LibSystem/syscall.h>
#include <fcntl.h>
@ -92,4 +93,26 @@ ErrorOr<void> munmap(void* address, size_t size)
return {};
}
ErrorOr<int> 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, &params);
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
}
}

View File

@ -22,5 +22,6 @@ ErrorOr<struct stat> fstat(int fd);
ErrorOr<int> fcntl(int fd, int command, ...);
ErrorOr<void*> mmap(void* address, size_t, int protection, int flags, int fd, off_t, size_t alignment = 0, StringView name = {});
ErrorOr<void> munmap(void* address, size_t);
ErrorOr<int> open(StringView path, int options, ...);
}