mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:07:46 +00:00
LibCore: Add syscall wrapper for open()
This commit is contained in:
parent
094fa900a3
commit
50416c286d
2 changed files with 24 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue