xref: /haiku/src/system/libroot/posix/fcntl.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001, Manuel J. Petit. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 
9 
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <pthread.h>
13 #include <stdarg.h>
14 #include <unistd.h>
15 
16 #include <errno_private.h>
17 #include <syscalls.h>
18 #include <syscall_utils.h>
19 #include <umask.h>
20 
21 
22 int
23 creat(const char *path, mode_t mode)
24 {
25 	RETURN_AND_SET_ERRNO_TEST_CANCEL(
26 		_kern_open(-1, path, O_CREAT | O_TRUNC | O_WRONLY, mode & ~__gUmask));
27 		// adapt the permissions as required by POSIX
28 }
29 
30 
31 int
32 open(const char *path, int openMode, ...)
33 {
34 	int perms = 0;
35 	if (openMode & O_CREAT) {
36 		va_list args;
37 		va_start(args, openMode);
38 		perms = va_arg(args, int) & ~__gUmask;
39 			// adapt the permissions as required by POSIX
40 		va_end(args);
41 	}
42 
43 	RETURN_AND_SET_ERRNO_TEST_CANCEL(_kern_open(-1, path, openMode, perms));
44 }
45 
46 
47 int
48 openat(int fd, const char *path, int openMode, ...)
49 {
50 	int perms = 0;
51 	if (openMode & O_CREAT) {
52 		va_list args;
53 		va_start(args, openMode);
54 		perms = va_arg(args, int) & ~__gUmask;
55 			// adapt the permissions as required by POSIX
56 		va_end(args);
57 	}
58 
59 	RETURN_AND_SET_ERRNO_TEST_CANCEL(_kern_open(fd, path, openMode, perms));
60 }
61 
62 
63 int
64 fcntl(int fd, int op, ...)
65 {
66 	va_list args;
67 	va_start(args, op);
68 	size_t argument = va_arg(args, size_t);
69 	va_end(args);
70 
71 	status_t error = _kern_fcntl(fd, op, argument);
72 
73 	if (op == F_SETLKW)
74 		pthread_testcancel();
75 
76 	RETURN_AND_SET_ERRNO(error);
77 }
78