updating to the latest module.
[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 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         short _whence;
31         mph_return_if_off_t_overflow (offset);
32         if (Mono_Posix_FromSeekFlags (whence, &_whence) == -1)
33                 return -1;
34         whence = _whence;
35
36         return lseek (fd, offset, whence);
37 }
38
39 mph_ssize_t
40 Mono_Posix_Syscall_read (gint32 fd, void *buf, mph_size_t count)
41 {
42         mph_return_if_size_t_overflow (count);
43         return read (fd, buf, (size_t) count);
44 }
45
46 mph_ssize_t
47 Mono_Posix_Syscall_write (gint32 fd, const void *buf, mph_size_t count)
48 {
49         mph_return_if_size_t_overflow (count);
50         return write (fd, buf, (size_t) count);
51 }
52
53 mph_ssize_t
54 Mono_Posix_Syscall_pread (gint32 fd, void *buf, mph_size_t count, mph_off_t offset)
55 {
56         mph_return_if_size_t_overflow (count);
57         mph_return_if_off_t_overflow (offset);
58
59         return pread (fd, buf, (size_t) count, (off_t) offset);
60 }
61
62 mph_ssize_t
63 Mono_Posix_Syscall_pwrite (gint32 fd, const void *buf, mph_size_t count, mph_off_t offset)
64 {
65         mph_return_if_size_t_overflow (count);
66         mph_return_if_off_t_overflow (offset);
67
68         return pwrite (fd, buf, (size_t) count, (off_t) offset);
69 }
70
71 gint32
72 Mono_Posix_Syscall_pipe (gint32 *reading, gint32 *writing)
73 {
74         int filedes[2] = {-1, -1};
75         int r;
76
77         if (reading == NULL || writing == NULL) {
78                 errno = EFAULT;
79                 return -1;
80         }
81
82         r = pipe (filedes);
83
84         *reading = filedes[0];
85         *writing = filedes[1];
86         return r;
87 }
88
89 char*
90 Mono_Posix_Syscall_getcwd (char *buf, mph_size_t size)
91 {
92         mph_return_val_if_size_t_overflow (size, NULL);
93         return getcwd (buf, (size_t) size);
94 }
95
96 gint64
97 Mono_Posix_Syscall_fpathconf (int filedes, int name)
98 {
99         if (Mono_Posix_FromPathConf (name, &name) == -1)
100                 return -1;
101         return fpathconf (filedes, name);
102 }
103
104 gint64
105 Mono_Posix_Syscall_pathconf (char *path, int name)
106 {
107         if (Mono_Posix_FromPathConf (name, &name) == -1)
108                 return -1;
109         return pathconf (path, name);
110 }
111
112 gint64
113 Mono_Posix_Syscall_sysconf (int name)
114 {
115         if (Mono_Posix_FromSysConf (name, &name) == -1)
116                 return -1;
117         return sysconf (name);
118 }
119
120 gint64
121 Mono_Posix_Syscall_confstr (int name, char *buf, mph_size_t len)
122 {
123         mph_return_if_size_t_overflow (len);
124         if (Mono_Posix_FromConfStr (name, &name) == -1)
125                 return -1;
126         return confstr (name, buf, (size_t) len);
127 }
128
129 #ifdef HAVE_TTYNAME_R
130 gint32
131 Mono_Posix_Syscall_ttyname_r (int fd, char *buf, mph_size_t len)
132 {
133         mph_return_if_size_t_overflow (len);
134         return ttyname_r (fd, buf, (size_t) len);
135 }
136 #endif /* ndef HAVE_TTYNAME_R */
137
138 gint32
139 Mono_Posix_Syscall_readlink (const char *path, char *buf, mph_size_t len)
140 {
141         int r;
142         mph_return_if_size_t_overflow (len);
143         r = readlink (path, buf, (size_t) len);
144         if (r >= 0 && r < len)
145                 buf [r] = '\0';
146         return r;
147 }
148
149 gint32
150 Mono_Posix_Syscall_getlogin_r (char *buf, mph_size_t len)
151 {
152         mph_return_if_size_t_overflow (len);
153         return getlogin_r (buf, (size_t) len);
154 }
155
156 gint32
157 Mono_Posix_Syscall_gethostname (char *buf, mph_size_t len)
158 {
159         mph_return_if_size_t_overflow (len);
160         return gethostname (buf, (size_t) len);
161 }
162
163 gint32
164 Mono_Posix_Syscall_sethostname (const char *name, mph_size_t len)
165 {
166         mph_return_if_size_t_overflow (len);
167         return sethostname (name, (size_t) len);
168 }
169
170 gint64
171 Mono_Posix_Syscall_gethostid (void)
172 {
173         return gethostid ();
174 }
175
176 #ifdef HAVE_SETHOSTID
177 gint32
178 Mono_Posix_Syscall_sethostid (gint64 hostid)
179 {
180         mph_return_if_long_overflow (hostid);
181 #ifdef MPH_ON_BSD
182         sethostid ((long) hostid);
183         return 0;
184 #else
185         return sethostid ((long) hostid);
186 #endif
187 }
188 #endif /* def HAVE_SETHOSTID */
189
190 #ifdef HAVE_GETDOMAINNAME
191 gint32
192 Mono_Posix_Syscall_getdomainname (char *name, mph_size_t len)
193 {
194         mph_return_if_size_t_overflow (len);
195         return getdomainname (name, (size_t) len);
196 }
197 #endif /* def HAVE_GETDOMAINNAME */
198
199 #ifdef HAVE_SETDOMAINNAME
200 gint32
201 Mono_Posix_Syscall_setdomainname (const char *name, mph_size_t len)
202 {
203         mph_return_if_size_t_overflow (len);
204         return setdomainname (name, (size_t) len);
205 }
206 #endif /* def HAVE_SETDOMAINNAME */
207
208 gint32
209 Mono_Posix_Syscall_truncate (const char *path, mph_off_t length)
210 {
211         mph_return_if_off_t_overflow (length);
212         return truncate (path, (off_t) length);
213 }
214
215 gint32
216 Mono_Posix_Syscall_ftruncate (int fd, mph_off_t length)
217 {
218         mph_return_if_off_t_overflow (length);
219         return ftruncate (fd, (off_t) length);
220 }
221
222 gint32
223 Mono_Posix_Syscall_lockf (int fd, int cmd, mph_off_t len)
224 {
225         mph_return_if_off_t_overflow (len);
226         if (Mono_Posix_FromLockFlags (cmd, &cmd) == -1)
227                 return -1;
228         return lockf (fd, cmd, (off_t) len);
229 }
230
231 void
232 Mono_Posix_Syscall_swab (void *from, void *to, mph_ssize_t n)
233 {
234         if (mph_have_long_overflow (n))
235                 return;
236         swab (from, to, (ssize_t) n);
237 }
238
239 G_END_DECLS
240
241 /*
242  * vim: noexpandtab
243  */