Fix SocketAcceptAsyncTest
[mono-project.git] / support / unistd.c
blob1123f2bc0fac9112b60e8f398e11b685eb59783c
1 /*
2 * <unistd.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2006 Jonathan Pryor
8 */
10 #include <config.h>
12 #ifndef _GNU_SOURCE
13 #define _GNU_SOURCE
14 #endif /* ndef _GNU_SOURCE */
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 */
24 #include "map.h"
25 #include "mph.h"
27 G_BEGIN_DECLS
29 mph_off_t
30 Mono_Posix_Syscall_lseek (gint32 fd, mph_off_t offset, gint32 whence)
32 mph_return_if_off_t_overflow (offset);
34 return lseek (fd, offset, whence);
37 mph_ssize_t
38 Mono_Posix_Syscall_read (gint32 fd, void *buf, mph_size_t count)
40 mph_return_if_size_t_overflow (count);
41 return read (fd, buf, (size_t) count);
44 mph_ssize_t
45 Mono_Posix_Syscall_write (gint32 fd, void *buf, mph_size_t count)
47 mph_return_if_size_t_overflow (count);
48 return write (fd, buf, (size_t) count);
51 mph_ssize_t
52 Mono_Posix_Syscall_pread (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
54 mph_return_if_size_t_overflow (count);
55 mph_return_if_off_t_overflow (offset);
57 return pread (fd, buf, (size_t) count, (off_t) offset);
60 mph_ssize_t
61 Mono_Posix_Syscall_pwrite (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
63 mph_return_if_size_t_overflow (count);
64 mph_return_if_off_t_overflow (offset);
66 return pwrite (fd, buf, (size_t) count, (off_t) offset);
69 gint32
70 Mono_Posix_Syscall_pipe (gint32 *reading, gint32 *writing)
72 int filedes[2] = {-1, -1};
73 int r;
75 if (reading == NULL || writing == NULL) {
76 errno = EFAULT;
77 return -1;
80 r = pipe (filedes);
82 *reading = filedes[0];
83 *writing = filedes[1];
84 return r;
87 void*
88 Mono_Posix_Syscall_getcwd (char *buf, mph_size_t size)
90 mph_return_val_if_size_t_overflow (size, NULL);
91 return getcwd (buf, (size_t) size);
94 gint64
95 Mono_Posix_Syscall_fpathconf (int filedes, int name, int defaultError)
97 errno = defaultError;
98 if (Mono_Posix_FromPathconfName (name, &name) == -1)
99 return -1;
100 return fpathconf (filedes, name);
103 gint64
104 Mono_Posix_Syscall_pathconf (const char *path, int name, int defaultError)
106 errno = defaultError;
107 if (Mono_Posix_FromPathconfName (name, &name) == -1)
108 return -1;
109 return pathconf (path, name);
112 gint64
113 Mono_Posix_Syscall_sysconf (int name, int defaultError)
115 errno = defaultError;
116 if (Mono_Posix_FromSysconfName (name, &name) == -1)
117 return -1;
118 return sysconf (name);
121 #if HAVE_CONFSTR
122 mph_size_t
123 Mono_Posix_Syscall_confstr (int name, char *buf, mph_size_t len)
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);
130 #endif /* def HAVE_CONFSTR */
132 #ifdef HAVE_TTYNAME_R
133 gint32
134 Mono_Posix_Syscall_ttyname_r (int fd, char *buf, mph_size_t len)
136 mph_return_if_size_t_overflow (len);
137 return ttyname_r (fd, buf, (size_t) len);
139 #endif /* ndef HAVE_TTYNAME_R */
141 gint64
142 Mono_Posix_Syscall_readlink (const char *path, unsigned char *buf, mph_size_t len)
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;
152 #ifdef HAVE_READLINKAT
153 gint64
154 Mono_Posix_Syscall_readlinkat (int dirfd, const char *path, unsigned char *buf, mph_size_t len)
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;
163 #endif /* def HAVE_READLINKAT */
165 #if HAVE_GETLOGIN_R
166 gint32
167 Mono_Posix_Syscall_getlogin_r (char *buf, mph_size_t len)
169 mph_return_if_size_t_overflow (len);
170 return getlogin_r (buf, (size_t) len);
172 #endif /* def HAVE_GETLOGIN_R */
174 gint32
175 Mono_Posix_Syscall_gethostname (char *buf, mph_size_t len)
177 mph_return_if_size_t_overflow (len);
178 return gethostname (buf, (size_t) len);
181 #if HAVE_SETHOSTNAME
182 gint32
183 Mono_Posix_Syscall_sethostname (const char *name, mph_size_t len)
185 mph_return_if_size_t_overflow (len);
186 return sethostname (name, (size_t) len);
188 #endif /* def HAVE_SETHOSTNAME */
190 #if HAVE_GETHOSTID
191 gint64
192 Mono_Posix_Syscall_gethostid (void)
194 return gethostid ();
196 #endif /* def HAVE_GETHOSTID */
198 #ifdef HAVE_SETHOSTID
199 gint32
200 Mono_Posix_Syscall_sethostid (gint64 hostid)
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
210 #endif /* def HAVE_SETHOSTID */
212 #ifdef HAVE_GETDOMAINNAME
213 gint32
214 Mono_Posix_Syscall_getdomainname (char *name, mph_size_t len)
216 mph_return_if_size_t_overflow (len);
217 return getdomainname (name, (size_t) len);
219 #endif /* def HAVE_GETDOMAINNAME */
221 #ifdef HAVE_SETDOMAINNAME
222 gint32
223 Mono_Posix_Syscall_setdomainname (const char *name, mph_size_t len)
225 mph_return_if_size_t_overflow (len);
226 return setdomainname (name, (size_t) len);
228 #endif /* def HAVE_SETDOMAINNAME */
230 /* Android implements truncate, but doesn't declare it.
231 * Result is a warning during compilation, so skip it.
233 #ifndef PLATFORM_ANDROID
234 gint32
235 Mono_Posix_Syscall_truncate (const char *path, mph_off_t length)
237 mph_return_if_off_t_overflow (length);
238 return truncate (path, (off_t) length);
240 #endif
242 gint32
243 Mono_Posix_Syscall_ftruncate (int fd, mph_off_t length)
245 mph_return_if_off_t_overflow (length);
246 return ftruncate (fd, (off_t) length);
249 #if HAVE_LOCKF
250 gint32
251 Mono_Posix_Syscall_lockf (int fd, int cmd, mph_off_t len)
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);
258 #endif /* def HAVE_LOCKF */
260 #if HAVE_SWAB
262 Mono_Posix_Syscall_swab (void *from, void *to, mph_ssize_t n)
264 if (mph_have_long_overflow (n))
265 return -1;
266 swab (from, to, (ssize_t) n);
267 return 0;
269 #endif /* def HAVE_SWAB */
271 #if HAVE_SETUSERSHELL
273 Mono_Posix_Syscall_setusershell (void)
275 setusershell ();
276 return 0;
278 #endif /* def HAVE_SETUSERSHELL */
280 #if HAVE_ENDUSERSHELL
282 Mono_Posix_Syscall_endusershell (void)
284 endusershell ();
285 return 0;
287 #endif /* def HAVE_ENDUSERSHELL */
290 Mono_Posix_Syscall_sync (void)
292 sync ();
293 return 0;
297 G_END_DECLS
300 * vim: noexpandtab