Prepare Mono for Android NDK with unified headers (#5680)
[mono.git] / support / fcntl.c
1 /*
2  * <fcntl.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004, 2006 Jonathan Pryor
8  */
9
10 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <fcntl.h>
20 #include <errno.h>
21 #ifdef HOST_WIN32
22 #include <corecrt_io.h>
23 #endif
24
25 #include "mph.h" /* Don't remove or move after map.h! Works around issues with Android SDK unified headers */
26 #include "map.h"
27
28 G_BEGIN_DECLS
29
30 #ifndef HOST_WIN32
31 gint32
32 Mono_Posix_Syscall_fcntl (gint32 fd, gint32 cmd)
33 {
34         if (Mono_Posix_FromFcntlCommand (cmd, &cmd) == -1)
35                 return -1;
36         return fcntl (fd, cmd);
37 }
38
39 gint32
40 Mono_Posix_Syscall_fcntl_arg_int (gint32 fd, gint32 cmd, int arg)
41 {
42         if (Mono_Posix_FromFcntlCommand (cmd, &cmd) == -1)
43                 return -1;
44         return fcntl (fd, cmd, arg);
45 }
46
47 gint32
48 Mono_Posix_Syscall_fcntl_arg_ptr (gint32 fd, gint32 cmd, void *arg)
49 {
50         if (Mono_Posix_FromFcntlCommand (cmd, &cmd) == -1)
51                 return -1;
52         return fcntl (fd, cmd, arg);
53 }
54
55 gint32
56 Mono_Posix_Syscall_fcntl_arg (gint32 fd, gint32 cmd, gint64 arg)
57 {
58         long _arg;
59         gint32 _cmd;
60
61         mph_return_if_long_overflow (arg);
62
63 #ifdef F_NOTIFY
64         if (cmd == F_NOTIFY) {
65                 int _argi;
66                 if (Mono_Posix_FromDirectoryNotifyFlags (arg, &_argi) == -1) {
67                         return -1;
68                 }
69                 _arg = _argi;
70         }
71         else
72 #endif
73                 _arg = (long) arg;
74
75         if (Mono_Posix_FromFcntlCommand (cmd, &_cmd) == -1)
76                 return -1;
77         return fcntl (fd, cmd, _arg);
78 }
79
80 gint32
81 Mono_Posix_Syscall_fcntl_lock (gint32 fd, gint32 cmd, struct Mono_Posix_Flock *lock)
82 {
83         struct flock _lock;
84         int r;
85
86         if (lock == NULL) {
87                 errno = EFAULT;
88                 return -1;
89         }
90
91         if (Mono_Posix_FromFlock (lock, &_lock) == -1)
92                 return -1;
93
94         if (Mono_Posix_FromFcntlCommand (cmd, &cmd) == -1)
95                 return -1;
96
97         r = fcntl (fd, cmd, &_lock);
98
99         if (Mono_Posix_ToFlock (&_lock, lock) == -1)
100                 return -1;
101
102         return r;
103 }
104 #endif
105
106 gint32
107 Mono_Posix_Syscall_open (const char *pathname, gint32 flags)
108 {
109         if (Mono_Posix_FromOpenFlags (flags, &flags) == -1)
110                 return -1;
111
112         return open (pathname, flags);
113 }
114
115 gint32
116 Mono_Posix_Syscall_open_mode (const char *pathname, gint32 flags, guint32 mode)
117 {
118         if (Mono_Posix_FromOpenFlags (flags, &flags) == -1)
119                 return -1;
120         if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
121                 return -1;
122
123         return open (pathname, flags, mode);
124 }
125
126 gint32
127 Mono_Posix_Syscall_get_at_fdcwd ()
128 {
129 #ifdef AT_FDCWD
130         return AT_FDCWD;
131 #else
132         return -1;
133 #endif
134 }
135
136 gint32
137 Mono_Posix_Syscall_creat (const char *pathname, guint32 mode)
138 {
139         if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
140                 return -1;
141
142         return creat (pathname, mode);
143 }
144
145 #ifdef HAVE_POSIX_FADVISE
146 gint32
147 Mono_Posix_Syscall_posix_fadvise (gint32 fd, mph_off_t offset, mph_off_t len, 
148         gint32 advice)
149 {
150         mph_return_if_off_t_overflow (offset);
151         mph_return_if_off_t_overflow (len);
152
153         if (Mono_Posix_FromPosixFadviseAdvice (advice, &advice) == -1)
154                 return -1;
155
156         return posix_fadvise (fd, (off_t) offset, (off_t) len, advice);
157 }
158 #endif /* ndef HAVE_POSIX_FADVISE */
159
160 #ifdef HAVE_POSIX_FALLOCATE
161 gint32
162 Mono_Posix_Syscall_posix_fallocate (gint32 fd, mph_off_t offset, mph_size_t len)
163 {
164         mph_return_if_off_t_overflow (offset);
165         mph_return_if_size_t_overflow (len);
166
167         return posix_fallocate (fd, (off_t) offset, (size_t) len);
168 }
169 #endif /* ndef HAVE_POSIX_FALLOCATE */
170
171 G_END_DECLS
172
173 /*
174  * vim: noexpandtab
175  */