xref: /haiku/src/tools/fs_shell/fcntl.cpp (revision 637d99977c22c39134c5c6e38314682196941a1c)
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 #if (!defined(__BEOS__) && !defined(__HAIKU__))
20 	// The _kern_open() defined in libroot_build.so.
21 #	define _kern_open	_kernbuild_open
22 	extern "C"  int _kern_open(int fd, const char *path, int openMode,
23 		int perms);
24 #endif
25 
26 
27 int
fssh_open(const char * pathname,int oflags,...)28 fssh_open(const char *pathname, int oflags, ...)
29 {
30 	va_list args;
31 	va_start(args, oflags);
32 
33 	// get the mode, if O_CREAT was specified
34 	fssh_mode_t mode = 0;
35 	if (oflags & FSSH_O_CREAT)
36 		mode = va_arg(args, fssh_mode_t);
37 
38 	va_end(args);
39 
40 	// Use the _kern_open() defined in libroot on BeOS incompatible systems.
41 	// Required for proper attribute emulation support.
42 	int fd;
43 	#if (defined(__BEOS__) || defined(__HAIKU__))
44 		fd = open(pathname, to_platform_open_mode(oflags),
45 			to_platform_mode(mode));
46 	#else
47 		fd = _kern_open(-1, pathname, to_platform_open_mode(oflags),
48 			to_platform_mode(mode));
49 		if (fd < 0) {
50 			fssh_set_errno(fd);
51 			fd = -1;
52 		}
53 
54 	#endif
55 
56 	restricted_file_opened(fd);
57 
58 	return fd;
59 }
60 
61 
62 int
fssh_creat(const char * path,fssh_mode_t mode)63 fssh_creat(const char *path, fssh_mode_t mode)
64 {
65 	return fssh_open(path, FSSH_O_WRONLY | FSSH_O_CREAT | FSSH_O_TRUNC, mode);
66 }
67