New test.
[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 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif /* ndef _GNU_SOURCE */
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <string.h>     /* for swab(3) on Mac OS X */
21
22 #include "map.h"
23 #include "mph.h"
24
25 G_BEGIN_DECLS
26
27 mph_off_t
28 Mono_Posix_Syscall_lseek (gint32 fd, mph_off_t offset, gint32 whence)
29 {
30         mph_return_if_off_t_overflow (offset);
31
32         return lseek (fd, offset, whence);
33 }
34
35 mph_ssize_t
36 Mono_Posix_Syscall_read (gint32 fd, void *buf, mph_size_t count)
37 {
38         mph_return_if_size_t_overflow (count);
39         return read (fd, buf, (size_t) count);
40 }
41
42 mph_ssize_t
43 Mono_Posix_Syscall_write (gint32 fd, void *buf, mph_size_t count)
44 {
45         mph_return_if_size_t_overflow (count);
46         return write (fd, buf, (size_t) count);
47 }
48
49 mph_ssize_t
50 Mono_Posix_Syscall_pread (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
51 {
52         mph_return_if_size_t_overflow (count);
53         mph_return_if_off_t_overflow (offset);
54
55         return pread (fd, buf, (size_t) count, (off_t) offset);
56 }
57
58 mph_ssize_t
59 Mono_Posix_Syscall_pwrite (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
60 {
61         mph_return_if_size_t_overflow (count);
62         mph_return_if_off_t_overflow (offset);
63
64         return pwrite (fd, buf, (size_t) count, (off_t) offset);
65 }
66
67 gint32
68 Mono_Posix_Syscall_pipe (gint32 *reading, gint32 *writing)
69 {
70         int filedes[2] = {-1, -1};
71         int r;
72
73         if (reading == NULL || writing == NULL) {
74                 errno = EFAULT;
75                 return -1;
76         }
77
78         r = pipe (filedes);
79
80         *reading = filedes[0];
81         *writing = filedes[1];
82         return r;
83 }
84
85 void*
86 Mono_Posix_Syscall_getcwd (char *buf, mph_size_t size)
87 {
88         mph_return_val_if_size_t_overflow (size, NULL);
89         return getcwd (buf, (size_t) size);
90 }
91
92 gint64
93 Mono_Posix_Syscall_fpathconf (int filedes, int name, int defaultError)
94 {
95         errno = defaultError;
96         if (Mono_Posix_FromPathconfName (name, &name) == -1)
97                 return -1;
98         return fpathconf (filedes, name);
99 }
100
101 gint64
102 Mono_Posix_Syscall_pathconf (const char *path, int name, int defaultError)
103 {
104         errno = defaultError;
105         if (Mono_Posix_FromPathconfName (name, &name) == -1)
106                 return -1;
107         return pathconf (path, name);
108 }
109
110 gint64
111 Mono_Posix_Syscall_sysconf (int name, int defaultError)
112 {
113         errno = defaultError;
114         if (Mono_Posix_FromSysconfName (name, &name) == -1)
115                 return -1;
116         return sysconf (name);
117 }
118
119 mph_size_t
120 Mono_Posix_Syscall_confstr (int name, char *buf, mph_size_t len)
121 {
122         mph_return_if_size_t_overflow (len);
123         if (Mono_Posix_FromConfstrName (name, &name) == -1)
124                 return -1;
125         return confstr (name, buf, (size_t) len);
126 }
127
128 #ifdef HAVE_TTYNAME_R
129 gint32
130 Mono_Posix_Syscall_ttyname_r (int fd, char *buf, mph_size_t len)
131 {
132         mph_return_if_size_t_overflow (len);
133         return ttyname_r (fd, buf, (size_t) len);
134 }
135 #endif /* ndef HAVE_TTYNAME_R */
136
137 gint32
138 Mono_Posix_Syscall_readlink (const char *path, char *buf, mph_size_t len)
139 {
140         int r;
141         mph_return_if_size_t_overflow (len);
142         r = readlink (path, buf, (size_t) len);
143         if (r >= 0 && r < len)
144                 buf [r] = '\0';
145         return r;
146 }
147
148 gint32
149 Mono_Posix_Syscall_getlogin_r (char *buf, mph_size_t len)
150 {
151         mph_return_if_size_t_overflow (len);
152         return getlogin_r (buf, (size_t) len);
153 }
154
155 gint32
156 Mono_Posix_Syscall_gethostname (char *buf, mph_size_t len)
157 {
158         mph_return_if_size_t_overflow (len);
159         return gethostname (buf, (size_t) len);
160 }
161
162 gint32
163 Mono_Posix_Syscall_sethostname (const char *name, mph_size_t len)
164 {
165         mph_return_if_size_t_overflow (len);
166         return sethostname (name, (size_t) len);
167 }
168
169 gint64
170 Mono_Posix_Syscall_gethostid (void)
171 {
172         return gethostid ();
173 }
174
175 #ifdef HAVE_SETHOSTID
176 gint32
177 Mono_Posix_Syscall_sethostid (gint64 hostid)
178 {
179         mph_return_if_long_overflow (hostid);
180 #ifdef MPH_ON_BSD
181         sethostid ((long) hostid);
182         return 0;
183 #else
184         return sethostid ((long) hostid);
185 #endif
186 }
187 #endif /* def HAVE_SETHOSTID */
188
189 #ifdef HAVE_GETDOMAINNAME
190 gint32
191 Mono_Posix_Syscall_getdomainname (char *name, mph_size_t len)
192 {
193         mph_return_if_size_t_overflow (len);
194         return getdomainname (name, (size_t) len);
195 }
196 #endif /* def HAVE_GETDOMAINNAME */
197
198 #ifdef HAVE_SETDOMAINNAME
199 gint32
200 Mono_Posix_Syscall_setdomainname (const char *name, mph_size_t len)
201 {
202         mph_return_if_size_t_overflow (len);
203         return setdomainname (name, (size_t) len);
204 }
205 #endif /* def HAVE_SETDOMAINNAME */
206
207 gint32
208 Mono_Posix_Syscall_truncate (const char *path, mph_off_t length)
209 {
210         mph_return_if_off_t_overflow (length);
211         return truncate (path, (off_t) length);
212 }
213
214 gint32
215 Mono_Posix_Syscall_ftruncate (int fd, mph_off_t length)
216 {
217         mph_return_if_off_t_overflow (length);
218         return ftruncate (fd, (off_t) length);
219 }
220
221 gint32
222 Mono_Posix_Syscall_lockf (int fd, int cmd, mph_off_t len)
223 {
224         mph_return_if_off_t_overflow (len);
225         if (Mono_Posix_FromLockfCommand (cmd, &cmd) == -1)
226                 return -1;
227         return lockf (fd, cmd, (off_t) len);
228 }
229
230 int
231 Mono_Posix_Syscall_swab (void *from, void *to, mph_ssize_t n)
232 {
233         if (mph_have_long_overflow (n))
234                 return -1;
235         swab (from, to, (ssize_t) n);
236         return 0;
237 }
238
239 int
240 Mono_Posix_Syscall_setusershell (void)
241 {
242         errno = 0;
243         setusershell ();
244         return errno == 0 ? 0 : -1;
245 }
246
247 int
248 Mono_Posix_Syscall_endusershell (void)
249 {
250         errno = 0;
251         endusershell ();
252         return errno == 0 ? 0 : -1;
253 }
254
255 int
256 Mono_Posix_Syscall_sync (void)
257 {
258         errno = 0;
259         sync ();
260         return errno == 0 ? 0 : -1;
261 }
262
263
264 G_END_DECLS
265
266 /*
267  * vim: noexpandtab
268  */