diff options
| author | Kacper <kacper@mail.openlinux.dev> | 2025-12-07 20:10:31 +0100 |
|---|---|---|
| committer | Kacper <kacper@mail.openlinux.dev> | 2025-12-07 20:10:31 +0100 |
| commit | fc00c656c96528112d05cf0edf8631bd5eaea446 (patch) | |
| tree | a6e0e6c588191a8bd1c64afc3b7a258e3e66c236 /lib/libc/fcntl | |
Add build system scaffolding and libc headers
Diffstat (limited to 'lib/libc/fcntl')
| -rw-r--r-- | lib/libc/fcntl/creat.c | 7 | ||||
| -rw-r--r-- | lib/libc/fcntl/fcntl.c | 37 | ||||
| -rw-r--r-- | lib/libc/fcntl/open.c | 23 | ||||
| -rw-r--r-- | lib/libc/fcntl/openat.c | 17 | ||||
| -rw-r--r-- | lib/libc/fcntl/posix_fadvise.c | 7 | ||||
| -rw-r--r-- | lib/libc/fcntl/posix_fallocate.c | 7 |
6 files changed, 98 insertions, 0 deletions
diff --git a/lib/libc/fcntl/creat.c b/lib/libc/fcntl/creat.c new file mode 100644 index 00000000..fdcb6655 --- /dev/null +++ b/lib/libc/fcntl/creat.c @@ -0,0 +1,7 @@ +#include <fcntl.h> +#include <syscall.h> + +int creat(const char *path, mode_t mode) +{ + return syscall(creat, path, mode); +} diff --git a/lib/libc/fcntl/fcntl.c b/lib/libc/fcntl/fcntl.c new file mode 100644 index 00000000..ae213df5 --- /dev/null +++ b/lib/libc/fcntl/fcntl.c @@ -0,0 +1,37 @@ +#include <fcntl.h> +#include <stdarg.h> +#include <syscall.h> + +int fcntl(int fildes, int cmd, ...) +{ + unsigned long arg; + va_list ap; + va_start(ap, cmd); + arg = va_arg(ap, unsigned long); + va_end(ap); + + if (cmd == F_DUPFD_CLOEXEC) { + int ret = syscall(fcntl, fildes, F_DUPFD_CLOEXEC, arg); + if (ret != -EINVAL) { + if (ret >= 0) + syscall(fcntl, ret, F_SETFD, FD_CLOEXEC); + return __syscall_ret(ret); + } + + ret = syscall(fcntl, fildes, F_DUPFD_CLOEXEC, 0); + if (ret != -EINVAL) { + if (ret >= 0) + __syscall(close, ret); + + return __syscall_ret(-EINVAL); + } + + ret = syscall(fcntl, fildes, F_DUPFD, arg); + if (ret >= 0) + syscall(fcntl, ret, F_SETFD, FD_CLOEXEC); + + return __syscall_ret(ret); + } + + return syscall(fcntl, fildes, cmd); +} diff --git a/lib/libc/fcntl/open.c b/lib/libc/fcntl/open.c new file mode 100644 index 00000000..dbdff8f9 --- /dev/null +++ b/lib/libc/fcntl/open.c @@ -0,0 +1,23 @@ +#include <fcntl.h> +#include <stdarg.h> +#include <syscall.h> + +int open(const char *path, int oflag, ...) +{ + long ret; + mode_t mode = 0; + + if ((oflag & O_CREAT)) { + va_list ap; + va_start(ap, oflag); + mode = va_arg(ap, mode_t); + va_end(ap); + } + + ret = syscall(open, path, oflag, mode); + + if (ret >= 0 && (oflag & O_CLOEXEC)) + return syscall(fcntl, ret, F_SETFD, FD_CLOEXEC); + + return ret; +} diff --git a/lib/libc/fcntl/openat.c b/lib/libc/fcntl/openat.c new file mode 100644 index 00000000..bb34d600 --- /dev/null +++ b/lib/libc/fcntl/openat.c @@ -0,0 +1,17 @@ +#include <fcntl.h> +#include <stdarg.h> +#include <syscall.h> + +int openat(int fd, const char *path, int oflag, ...) +{ + mode_t mode = 0; + + if (oflag & O_CREAT) { + va_list ap; + va_start(ap, oflag); + mode = va_arg(ap, mode_t); + va_end(ap); + } + + return syscall(openat, fd, path, oflag, mode); +} diff --git a/lib/libc/fcntl/posix_fadvise.c b/lib/libc/fcntl/posix_fadvise.c new file mode 100644 index 00000000..dcf278fd --- /dev/null +++ b/lib/libc/fcntl/posix_fadvise.c @@ -0,0 +1,7 @@ +#include <fcntl.h> +#include <syscall.h> + +int posix_fadvise(int fd, off_t offset, off_t len, int advice) +{ + return syscall(fadvise64, fd, offset, len, advice); +} diff --git a/lib/libc/fcntl/posix_fallocate.c b/lib/libc/fcntl/posix_fallocate.c new file mode 100644 index 00000000..221f2d38 --- /dev/null +++ b/lib/libc/fcntl/posix_fallocate.c @@ -0,0 +1,7 @@ +#include <fcntl.h> +#include <syscall.h> + +int posix_fallocate(int fd, off_t offset, off_t len) +{ + return syscall(fallocate, fd, 0, offset, len); +} |
