summaryrefslogtreecommitdiff
path: root/lib/libc/fcntl/fcntl.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/fcntl/fcntl.c')
-rw-r--r--lib/libc/fcntl/fcntl.c37
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);
+}