xref: /haiku/src/system/libroot/posix/fcntl.cpp (revision c90684742e7361651849be4116d0e5de3a817194)
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 <stdarg.h>
13 #include <unistd.h>
14 
15 #include <syscalls.h>
16 #include <syscall_utils.h>
17 #include <umask.h>
18 
19 
20 int
21 creat(const char *path, mode_t mode)
22 {
23 	int status = _kern_open(-1, path, O_CREAT | O_TRUNC | O_WRONLY,
24 		mode & ~__gUmask);
25 		// adapt the permissions as required by POSIX
26 	if (status < 0) {
27 		errno = status;
28 		return -1;
29 	}
30 	return status;
31 }
32 
33 
34 int
35 open(const char *path, int openMode, ...)
36 {
37 	int perms = 0;
38 	if (openMode & O_CREAT) {
39 		va_list args;
40 		va_start(args, openMode);
41 		perms = va_arg(args, int) & ~__gUmask;
42 			// adapt the permissions as required by POSIX
43 		va_end(args);
44 	}
45 
46 	int status = _kern_open(-1, path, openMode, perms);
47 	if (status < 0) {
48 		errno = status;
49 		return -1;
50 	}
51 	return status;
52 }
53 
54 
55 int
56 openat(int fd, const char *path, int openMode, ...)
57 {
58 	int perms = 0;
59 	if (openMode & O_CREAT) {
60 		va_list args;
61 		va_start(args, openMode);
62 		perms = va_arg(args, int) & ~__gUmask;
63 			// adapt the permissions as required by POSIX
64 		va_end(args);
65 	}
66 
67 	int status = _kern_open(fd, path, openMode, perms);
68 	if (status < 0) {
69 		errno = status;
70 		return -1;
71 	}
72 	return status;
73 }
74 
75 
76 int
77 fcntl(int fd, int op, ...)
78 {
79 	va_list args;
80 	va_start(args, op);
81 	uint32 argument = va_arg(args, uint32);
82 	va_end(args);
83 
84 	RETURN_AND_SET_ERRNO(_kern_fcntl(fd, op, argument));
85 }
86