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/fcntl.c | |
Add build system scaffolding and libc headers
Diffstat (limited to 'lib/libc/fcntl/fcntl.c')
| -rw-r--r-- | lib/libc/fcntl/fcntl.c | 37 |
1 files changed, 37 insertions, 0 deletions
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); +} |
