mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-21 10:19:03 +03:00
LibCore: Add syscall wrapper for open()
This commit is contained in:
parent
094fa900a3
commit
50416c286d
Notes:
sideshowbarker
2024-07-18 00:48:57 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/50416c286d0
@ -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, ¶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
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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, ...);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user