Skip to content

Commit

Permalink
LibCore: Add syscall wrapper for open()
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Nov 23, 2021
1 parent 094fa90 commit 50416c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Userland/Libraries/LibCore/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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
}

}
1 change: 1 addition & 0 deletions Userland/Libraries/LibCore/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);

}

0 comments on commit 50416c2

Please sign in to comment.