xref: /haiku/src/tools/fs_shell/fcntl.cpp (revision 4b3b81da9e459443d75329cfd08bc9a57ad02653)
1 /*
2  * Copyright 2007-2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "fssh_fcntl.h"
7 
8 #include <fcntl.h>
9 #include <stdarg.h>
10 
11 #include "fssh_errno.h"
12 #include "partition_support.h"
13 #include "stat_util.h"
14 
15 
16 using namespace FSShell;
17 
18 
19 #ifndef __BEOS__
20 	// The _kern_open() defined in libroot_build.so.
21 	extern "C"  int _kern_open(int fd, const char *path, int openMode,
22 		int perms);
23 #endif
24 
25 
26 int
27 fssh_open(const char *pathname, int oflags, ...)
28 {
29 	va_list args;
30 	va_start(args, oflags);
31 
32 	// get the mode, if O_CREAT was specified
33 	fssh_mode_t mode = 0;
34 	if (oflags & FSSH_O_CREAT)
35 		mode = va_arg(args, fssh_mode_t);
36 
37 	va_end(args);
38 
39 	// Use the _kern_open() defined in libroot on BeOS incompatible systems.
40 	// Required for proper attribute emulation support.
41 	int fd;
42 	#if __BEOS__
43 		fd = open(pathname, to_platform_open_mode(oflags),
44 			to_platform_mode(mode));
45 	#else
46 		fd = _kern_open(-1, pathname, to_platform_open_mode(oflags),
47 			to_platform_mode(mode));
48 		if (fd < 0) {
49 			fssh_set_errno(fd);
50 			fd = -1;
51 		}
52 
53 	#endif
54 
55 	restricted_file_opened(fd);
56 
57 	return fd;
58 }
59 
60 
61 int
62 fssh_creat(const char *path, fssh_mode_t mode)
63 {
64 	return fssh_open(path, FSSH_O_WRONLY | FSSH_O_CREAT | FSSH_O_TRUNC, mode);
65 }
66