Prepare Mono for Android NDK with unified headers (#5680)
[mono.git] / support / unistd.c
1 /*
2  * <unistd.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004-2006 Jonathan Pryor
8  */
9
10 #include <config.h>
11
12 #ifndef _GNU_SOURCE
13 #define _GNU_SOURCE
14 #endif /* ndef _GNU_SOURCE */
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <string.h>     /* for swab(3) on Mac OS X */
23
24 #include "mph.h" /* Don't remove or move after map.h! Works around issues with Android SDK unified headers */
25 #include "map.h"
26
27 G_BEGIN_DECLS
28
29 mph_off_t
30 Mono_Posix_Syscall_lseek (gint32 fd, mph_off_t offset, gint32 whence)
31 {
32         mph_return_if_off_t_overflow (offset);
33
34         return lseek (fd, offset, whence);
35 }
36
37 mph_ssize_t
38 Mono_Posix_Syscall_read (gint32 fd, void *buf, mph_size_t count)
39 {
40         mph_return_if_size_t_overflow (count);
41         return read (fd, buf, (size_t) count);
42 }
43
44 mph_ssize_t
45 Mono_Posix_Syscall_write (gint32 fd, void *buf, mph_size_t count)
46 {
47         mph_return_if_size_t_overflow (count);
48         return write (fd, buf, (size_t) count);
49 }
50
51 mph_ssize_t
52 Mono_Posix_Syscall_pread (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
53 {
54         mph_return_if_size_t_overflow (count);
55         mph_return_if_off_t_overflow (offset);
56
57         return pread (fd, buf, (size_t) count, (off_t) offset);
58 }
59
60 mph_ssize_t
61 Mono_Posix_Syscall_pwrite (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
62 {
63         mph_return_if_size_t_overflow (count);
64         mph_return_if_off_t_overflow (offset);
65
66         return pwrite (fd, buf, (size_t) count, (off_t) offset);
67 }
68
69 gint32
70 Mono_Posix_Syscall_pipe (gint32 *reading, gint32 *writing)
71 {
72         int filedes[2] = {-1, -1};
73         int r;
74
75         if (reading == NULL || writing == NULL) {
76                 errno = EFAULT;
77                 return -1;
78         }
79
80         r = pipe (filedes);
81
82         *reading = filedes[0];
83         *writing = filedes[1];
84         return r;
85 }
86
87 void*
88 Mono_Posix_Syscall_getcwd (char *buf, mph_size_t size)
89 {
90         mph_return_val_if_size_t_overflow (size, NULL);
91         return getcwd (buf, (size_t) size);
92 }
93
94 gint64
95 Mono_Posix_Syscall_fpathconf (int filedes, int name, int defaultError)
96 {
97         errno = defaultError;
98         if (Mono_Posix_FromPathconfName (name, &name) == -1)
99                 return -1;
100         return fpathconf (filedes, name);
101 }
102
103 gint64
104 Mono_Posix_Syscall_pathconf (const char *path, int name, int defaultError)
105 {
106         errno = defaultError;
107         if (Mono_Posix_FromPathconfName (name, &name) == -1)
108                 return -1;
109         return pathconf (path, name);
110 }
111
112 gint64
113 Mono_Posix_Syscall_sysconf (int name, int defaultError)
114 {
115         errno = defaultError;
116         if (Mono_Posix_FromSysconfName (name, &name) == -1)
117                 return -1;
118         return sysconf (name);
119 }
120
121 #if HAVE_CONFSTR
122 mph_size_t
123 Mono_Posix_Syscall_confstr (int name, char *buf, mph_size_t len)
124 {
125         mph_return_if_size_t_overflow (len);
126         if (Mono_Posix_FromConfstrName (name, &name) == -1)
127                 return -1;
128         return confstr (name, buf, (size_t) len);
129 }
130 #endif  /* def HAVE_CONFSTR */
131
132 #ifdef HAVE_TTYNAME_R
133 gint32
134 Mono_Posix_Syscall_ttyname_r (int fd, char *buf, mph_size_t len)
135 {
136         mph_return_if_size_t_overflow (len);
137         return ttyname_r (fd, buf, (size_t) len);
138 }
139 #endif /* ndef HAVE_TTYNAME_R */
140
141 gint64
142 Mono_Posix_Syscall_readlink (const char *path, unsigned char *buf, mph_size_t len)
143 {
144         gint64 r;
145         mph_return_if_size_t_overflow (len);
146         r = readlink (path, (char*) buf, (size_t) len);
147         if (r >= 0 && r < len)
148                 buf [r] = '\0';
149         return r;
150 }
151
152 #ifdef HAVE_READLINKAT
153 gint64
154 Mono_Posix_Syscall_readlinkat (int dirfd, const char *path, unsigned char *buf, mph_size_t len)
155 {
156         gint64 r;
157         mph_return_if_size_t_overflow (len);
158         r = readlinkat (dirfd, path, (char*) buf, (size_t) len);
159         if (r >= 0 && r < len)
160                 buf [r] = '\0';
161         return r;
162 }
163 #endif /* def HAVE_READLINKAT */
164
165 #if HAVE_GETLOGIN_R
166 gint32
167 Mono_Posix_Syscall_getlogin_r (char *buf, mph_size_t len)
168 {
169         mph_return_if_size_t_overflow (len);
170         return getlogin_r (buf, (size_t) len);
171 }
172 #endif  /* def HAVE_GETLOGIN_R */
173
174 gint32
175 Mono_Posix_Syscall_gethostname (char *buf, mph_size_t len)
176 {
177         mph_return_if_size_t_overflow (len);
178         return gethostname (buf, (size_t) len);
179 }
180
181 #if HAVE_SETHOSTNAME
182 gint32
183 Mono_Posix_Syscall_sethostname (const char *name, mph_size_t len)
184 {
185         mph_return_if_size_t_overflow (len);
186         return sethostname (name, (size_t) len);
187 }
188 #endif  /* def HAVE_SETHOSTNAME */
189
190 #if HAVE_GETHOSTID
191 gint64
192 Mono_Posix_Syscall_gethostid (void)
193 {
194         return gethostid ();
195 }
196 #endif  /* def HAVE_GETHOSTID */
197
198 #ifdef HAVE_SETHOSTID
199 gint32
200 Mono_Posix_Syscall_sethostid (gint64 hostid)
201 {
202         mph_return_if_long_overflow (hostid);
203 #ifdef MPH_ON_BSD
204         sethostid ((long) hostid);
205         return 0;
206 #else
207         return sethostid ((long) hostid);
208 #endif
209 }
210 #endif /* def HAVE_SETHOSTID */
211
212 #ifdef HAVE_GETDOMAINNAME
213 gint32
214 Mono_Posix_Syscall_getdomainname (char *name, mph_size_t len)
215 {
216         mph_return_if_size_t_overflow (len);
217         return getdomainname (name, (size_t) len);
218 }
219 #endif /* def HAVE_GETDOMAINNAME */
220
221 #ifdef HAVE_SETDOMAINNAME
222 gint32
223 Mono_Posix_Syscall_setdomainname (const char *name, mph_size_t len)
224 {
225         mph_return_if_size_t_overflow (len);
226         return setdomainname (name, (size_t) len);
227 }
228 #endif /* def HAVE_SETDOMAINNAME */
229
230 /* Android implements truncate, but doesn't declare it.
231  * Result is a warning during compilation, so skip it.
232  */
233 #ifndef HOST_ANDROID
234 gint32
235 Mono_Posix_Syscall_truncate (const char *path, mph_off_t length)
236 {
237         mph_return_if_off_t_overflow (length);
238         return truncate (path, (off_t) length);
239 }
240 #endif
241
242 gint32
243 Mono_Posix_Syscall_ftruncate (int fd, mph_off_t length)
244 {
245         mph_return_if_off_t_overflow (length);
246         return ftruncate (fd, (off_t) length);
247 }
248
249 #if HAVE_LOCKF
250 gint32
251 Mono_Posix_Syscall_lockf (int fd, int cmd, mph_off_t len)
252 {
253         mph_return_if_off_t_overflow (len);
254         if (Mono_Posix_FromLockfCommand (cmd, &cmd) == -1)
255                 return -1;
256         return lockf (fd, cmd, (off_t) len);
257 }
258 #endif  /* def HAVE_LOCKF */
259
260 #if HAVE_SWAB
261 int
262 Mono_Posix_Syscall_swab (void *from, void *to, mph_ssize_t n)
263 {
264         if (mph_have_long_overflow (n))
265                 return -1;
266         swab (from, to, (ssize_t) n);
267         return 0;
268 }
269 #endif  /* def HAVE_SWAB */
270
271 #if HAVE_SETUSERSHELL
272 int
273 Mono_Posix_Syscall_setusershell (void)
274 {
275         setusershell ();
276         return 0;
277 }
278 #endif  /* def HAVE_SETUSERSHELL */
279
280 #if HAVE_ENDUSERSHELL
281 int
282 Mono_Posix_Syscall_endusershell (void)
283 {
284         endusershell ();
285         return 0;
286 }
287 #endif  /* def HAVE_ENDUSERSHELL */
288
289 int
290 Mono_Posix_Syscall_sync (void)
291 {
292         sync ();
293         return 0;
294 }
295
296
297 G_END_DECLS
298
299 /*
300  * vim: noexpandtab
301  */