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