Merge pull request #18491 from PreferLinux/RTF+Textbox-work
[mono-project.git] / support / unistd.c
blob0972f3b97ebf83d3f0ea4b3a144d456e938b642c
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 */
23 #if defined(_AIX)
24 #include <netdb.h> /* for get/setdomainname */
26 * Yet more stuff in libc that isn't in headers.
27 * Python does the same thing we do here.
28 * see: bugs.python.org/issue18259
30 extern int sethostname(const char *, size_t);
31 extern int sethostid(long);
32 #endif
34 #include "mph.h" /* Don't remove or move after map.h! Works around issues with Android SDK unified headers */
35 #include "map.h"
37 G_BEGIN_DECLS
39 mph_off_t
40 Mono_Posix_Syscall_lseek (gint32 fd, mph_off_t offset, gint32 whence)
42 mph_return_if_off_t_overflow (offset);
44 return lseek (fd, offset, whence);
47 mph_ssize_t
48 Mono_Posix_Syscall_read (gint32 fd, void *buf, mph_size_t count)
50 mph_return_if_size_t_overflow (count);
51 return read (fd, buf, (size_t) count);
54 mph_ssize_t
55 Mono_Posix_Syscall_write (gint32 fd, void *buf, mph_size_t count)
57 mph_return_if_size_t_overflow (count);
58 return write (fd, buf, (size_t) count);
61 mph_ssize_t
62 Mono_Posix_Syscall_pread (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
64 mph_return_if_size_t_overflow (count);
65 mph_return_if_off_t_overflow (offset);
67 return pread (fd, buf, (size_t) count, (off_t) offset);
70 mph_ssize_t
71 Mono_Posix_Syscall_pwrite (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
73 mph_return_if_size_t_overflow (count);
74 mph_return_if_off_t_overflow (offset);
76 return pwrite (fd, buf, (size_t) count, (off_t) offset);
79 gint32
80 Mono_Posix_Syscall_pipe (gint32 *reading, gint32 *writing)
82 int filedes[2] = {-1, -1};
83 int r;
85 if (reading == NULL || writing == NULL) {
86 errno = EFAULT;
87 return -1;
90 r = pipe (filedes);
92 *reading = filedes[0];
93 *writing = filedes[1];
94 return r;
97 void*
98 Mono_Posix_Syscall_getcwd (char *buf, mph_size_t size)
100 mph_return_val_if_size_t_overflow (size, NULL);
101 return getcwd (buf, (size_t) size);
104 gint64
105 Mono_Posix_Syscall_fpathconf (int filedes, int name, int defaultError)
107 errno = defaultError;
108 if (Mono_Posix_FromPathconfName (name, &name) == -1)
109 return -1;
110 return fpathconf (filedes, name);
113 gint64
114 Mono_Posix_Syscall_pathconf (const char *path, int name, int defaultError)
116 errno = defaultError;
117 if (Mono_Posix_FromPathconfName (name, &name) == -1)
118 return -1;
119 return pathconf (path, name);
122 gint64
123 Mono_Posix_Syscall_sysconf (int name, int defaultError)
125 errno = defaultError;
126 if (Mono_Posix_FromSysconfName (name, &name) == -1)
127 return -1;
128 return sysconf (name);
131 #if HAVE_CONFSTR
132 mph_size_t
133 Mono_Posix_Syscall_confstr (int name, char *buf, mph_size_t len)
135 mph_return_if_size_t_overflow (len);
136 if (Mono_Posix_FromConfstrName (name, &name) == -1)
137 return -1;
138 return confstr (name, buf, (size_t) len);
140 #endif /* def HAVE_CONFSTR */
142 #ifdef HAVE_TTYNAME_R
143 gint32
144 Mono_Posix_Syscall_ttyname_r (int fd, char *buf, mph_size_t len)
146 mph_return_if_size_t_overflow (len);
147 return ttyname_r (fd, buf, (size_t) len);
149 #endif /* ndef HAVE_TTYNAME_R */
151 gint64
152 Mono_Posix_Syscall_readlink (const char *path, unsigned char *buf, mph_size_t len)
154 gint64 r;
155 mph_return_if_size_t_overflow (len);
156 r = readlink (path, (char*) buf, (size_t) len);
157 if (r >= 0 && r < len)
158 buf [r] = '\0';
159 return r;
162 #ifdef HAVE_READLINKAT
163 gint64
164 Mono_Posix_Syscall_readlinkat (int dirfd, const char *path, unsigned char *buf, mph_size_t len)
166 gint64 r;
167 mph_return_if_size_t_overflow (len);
168 r = readlinkat (dirfd, path, (char*) buf, (size_t) len);
169 if (r >= 0 && r < len)
170 buf [r] = '\0';
171 return r;
173 #endif /* def HAVE_READLINKAT */
175 #if HAVE_GETLOGIN_R
176 gint32
177 Mono_Posix_Syscall_getlogin_r (char *buf, mph_size_t len)
179 mph_return_if_size_t_overflow (len);
180 return getlogin_r (buf, (size_t) len);
182 #endif /* def HAVE_GETLOGIN_R */
184 gint32
185 Mono_Posix_Syscall_gethostname (char *buf, mph_size_t len)
187 mph_return_if_size_t_overflow (len);
188 return gethostname (buf, (size_t) len);
191 #if HAVE_SETHOSTNAME
192 gint32
193 Mono_Posix_Syscall_sethostname (const char *name, mph_size_t len)
195 mph_return_if_size_t_overflow (len);
196 return sethostname (name, (size_t) len);
198 #endif /* def HAVE_SETHOSTNAME */
200 #if HAVE_GETHOSTID
201 gint64
202 Mono_Posix_Syscall_gethostid (void)
204 return gethostid ();
206 #endif /* def HAVE_GETHOSTID */
208 #ifdef HAVE_SETHOSTID
209 gint32
210 Mono_Posix_Syscall_sethostid (gint64 hostid)
212 mph_return_if_long_overflow (hostid);
213 #ifdef MPH_ON_BSD
214 sethostid ((long) hostid);
215 return 0;
216 #else
217 return sethostid ((long) hostid);
218 #endif
220 #endif /* def HAVE_SETHOSTID */
222 #ifdef HAVE_GETDOMAINNAME
223 gint32
224 Mono_Posix_Syscall_getdomainname (char *name, mph_size_t len)
226 mph_return_if_size_t_overflow (len);
227 return getdomainname (name, (size_t) len);
229 #endif /* def HAVE_GETDOMAINNAME */
231 #ifdef HAVE_SETDOMAINNAME
232 gint32
233 Mono_Posix_Syscall_setdomainname (const char *name, mph_size_t len)
235 mph_return_if_size_t_overflow (len);
236 return setdomainname (name, (size_t) len);
238 #endif /* def HAVE_SETDOMAINNAME */
240 /* Android implements truncate, but doesn't declare it.
241 * Result is a warning during compilation, so skip it.
243 #ifndef HOST_ANDROID
244 gint32
245 Mono_Posix_Syscall_truncate (const char *path, mph_off_t length)
247 mph_return_if_off_t_overflow (length);
248 return truncate (path, (off_t) length);
250 #endif
252 gint32
253 Mono_Posix_Syscall_ftruncate (int fd, mph_off_t length)
255 mph_return_if_off_t_overflow (length);
256 return ftruncate (fd, (off_t) length);
259 #if HAVE_LOCKF
260 gint32
261 Mono_Posix_Syscall_lockf (int fd, int cmd, mph_off_t len)
263 mph_return_if_off_t_overflow (len);
264 if (Mono_Posix_FromLockfCommand (cmd, &cmd) == -1)
265 return -1;
266 return lockf (fd, cmd, (off_t) len);
268 #endif /* def HAVE_LOCKF */
270 #if HAVE_SWAB
272 Mono_Posix_Syscall_swab (void *from, void *to, mph_ssize_t n)
274 if (mph_have_long_overflow (n))
275 return -1;
276 swab (from, to, (ssize_t) n);
277 return 0;
279 #endif /* def HAVE_SWAB */
281 #if HAVE_SETUSERSHELL
283 Mono_Posix_Syscall_setusershell (void)
285 setusershell ();
286 return 0;
288 #endif /* def HAVE_SETUSERSHELL */
290 #if HAVE_ENDUSERSHELL
292 Mono_Posix_Syscall_endusershell (void)
294 endusershell ();
295 return 0;
297 #endif /* def HAVE_ENDUSERSHELL */
300 Mono_Posix_Syscall_sync (void)
302 sync ();
303 return 0;
307 G_END_DECLS
310 * vim: noexpandtab