2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2018-11-11 10:38:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdarg.h>
|
2019-11-16 17:08:11 +01:00
|
|
|
#include <string.h>
|
2021-02-05 12:16:30 +01:00
|
|
|
#include <syscall.h>
|
2018-11-11 10:38:33 +01:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
int fcntl(int fd, int cmd, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, cmd);
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 extra_arg = va_arg(ap, u32);
|
2018-12-21 03:02:06 +01:00
|
|
|
int rc = syscall(SC_fcntl, fd, cmd, extra_arg);
|
2020-08-16 17:40:07 -07:00
|
|
|
va_end(ap);
|
2018-11-11 10:38:33 +01:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
2019-07-22 20:01:11 +02:00
|
|
|
|
2020-01-06 11:12:29 +01:00
|
|
|
int watch_file(const char* path, size_t path_length)
|
2019-07-22 20:01:11 +02:00
|
|
|
{
|
|
|
|
|
int rc = syscall(SC_watch_file, path, path_length);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 17:08:11 +01:00
|
|
|
int creat(const char* path, mode_t mode)
|
|
|
|
|
{
|
|
|
|
|
return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-12 19:21:59 +01:00
|
|
|
int open(const char* path, int options, ...)
|
2019-11-16 17:08:11 +01:00
|
|
|
{
|
2020-01-11 12:47:47 +01:00
|
|
|
if (!path) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-01-12 19:21:59 +01:00
|
|
|
auto path_length = strlen(path);
|
2019-11-16 17:08:11 +01:00
|
|
|
if (path_length > INT32_MAX) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, options);
|
|
|
|
|
auto mode = (mode_t)va_arg(ap, unsigned);
|
|
|
|
|
va_end(ap);
|
2021-01-12 19:21:59 +01:00
|
|
|
Syscall::SC_open_params params { AT_FDCWD, { path, path_length }, options, mode };
|
|
|
|
|
int rc = syscall(SC_open, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-11-16 17:08:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int openat(int dirfd, const char* path, int options, ...)
|
|
|
|
|
{
|
|
|
|
|
if (!path) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-01-12 19:21:59 +01:00
|
|
|
auto path_length = strlen(path);
|
|
|
|
|
if (path_length > INT32_MAX) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-11-16 17:08:11 +01:00
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, options);
|
|
|
|
|
auto mode = (mode_t)va_arg(ap, unsigned);
|
|
|
|
|
va_end(ap);
|
2021-01-12 19:21:59 +01:00
|
|
|
Syscall::SC_open_params params { dirfd, { path, path_length }, options, mode };
|
|
|
|
|
int rc = syscall(SC_open, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-11-16 17:08:11 +01:00
|
|
|
}
|
2018-11-11 10:38:33 +01:00
|
|
|
}
|