xref: /haiku/src/tools/fs_shell/fcntl.cpp (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 /*
2  * Copyright 2007, Ingo Weinhold, bonefish@cs.tu-berlin.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 "stat_util.h"
13 
14 
15 using namespace FSShell;
16 
17 
18 #ifndef __BEOS__
19 	// The _kern_open() defined in libroot_build.so.
20 	extern "C"  int _kern_open(int fd, const char *path, int openMode,
21 		int perms);
22 #endif
23 
24 
25 int
26 fssh_open(const char *pathname, int oflags, ...)
27 {
28 	va_list args;
29 	va_start(args, oflags);
30 
31 	// get the mode, if O_CREAT was specified
32 	fssh_mode_t mode = 0;
33 	if (oflags & FSSH_O_CREAT)
34 		mode = va_arg(args, fssh_mode_t);
35 
36 	va_end(args);
37 
38 	// Use the _kern_open() defined in libroot on BeOS incompatible systems.
39 	// Required for proper attribute emulation support.
40 	#if __BEOS__
41 		return open(pathname, to_platform_open_mode(oflags),
42 			to_platform_mode(mode));
43 	#else
44 		int fd = _kern_open(-1, pathname, to_platform_open_mode(oflags),
45 			to_platform_mode(mode));
46 		if (fd < 0) {
47 			fssh_set_errno(fd);
48 			return -1;
49 		}
50 
51 		return fd;
52 	#endif
53 }
54 
55 
56 int
57 fssh_creat(const char *path, fssh_mode_t mode)
58 {
59 	return fssh_open(path, FSSH_O_WRONLY | FSSH_O_CREAT | FSSH_O_TRUNC, mode);
60 }
61