summaryrefslogtreecommitdiff
path: root/lib/libc/fcntl/fcntl.c
blob: ae213df5eea329ccba36e2a81b2149df08b2588e (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
#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);
}