summaryrefslogtreecommitdiff
path: root/lib/libc/fcntl/fcntl.c
blob: b3ca012f6c1cd7a49b0c286b671dd1165e23869f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "errno.h"	   // for EINVAL

#include <fcntl.h>   // for F_DUPFD_CLOEXEC, FD_CLOEXEC, F_SETFD, F_D...
#include <stdarg.h>  // for va_arg, va_end, va_list, va_start
#include <syscall.h> // for __syscall_ret, syscall, __syscall_3, __sy...

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);
}