summaryrefslogtreecommitdiff
path: root/src/fcntl/fcntl.c
blob: 3e2715b48f3dc1221677ad854a6993a6990ece2e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <stdarg.h>
#include "_syscall.h"

int fcntl(int fildes, int cmd, ...)
{
	SYSCALL_NUMBER(scno, fcntl, -1);

	int r = -ENOSYS;
	enum { NONE, INT, FLOCK } arg = NONE;

	switch (cmd) {
	case F_GETFD:
	case F_GETFL:
		break;

	case F_DUPFD:
	case F_SETFD:
	case F_SETFL:
		arg = INT;
		break;

	case F_GETLK:
	case F_SETLK:
	case F_SETLKW:
		arg = FLOCK;
		break;

	default:
		errno = EINVAL;
		return -1;
	}

	if (arg == NONE) {
		r = __syscall(scno, fildes);
	} else {
		va_list ap;
		va_start(ap, cmd);
		if (arg == INT) {
			int n = va_arg(ap, int);
			r = __syscall(scno, fildes, n);
		} else if (arg == FLOCK) {
			struct flock *fl = va_arg(ap, struct flock *);
			r = __syscall(scno, fildes, fl);
		}
		va_end(ap);
	}

	if (r < 0) {
		errno = -r;
		return -1;
	}

	return r;
}
/*
POSIX(1)
*/