svn path=/branches/mono-1-1-9/mcs/; revision=51214
[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         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)
94 {
95         if (Mono_Posix_FromPathConf (name, &name) == -1)
96                 return -1;
97         return fpathconf (filedes, name);
98 }
99
100 gint64
101 Mono_Posix_Syscall_pathconf (const char *path, int name)
102 {
103         if (Mono_Posix_FromPathConf (name, &name) == -1)
104                 return -1;
105         return pathconf (path, name);
106 }
107
108 gint64
109 Mono_Posix_Syscall_sysconf (int name)
110 {
111         if (Mono_Posix_FromSysConf (name, &name) == -1)
112                 return -1;
113         return sysconf (name);
114 }
115
116 mph_size_t
117 Mono_Posix_Syscall_confstr (int name, char *buf, mph_size_t len)
118 {
119         mph_return_if_size_t_overflow (len);
120         if (Mono_Posix_FromConfStr (name, &name) == -1)
121                 return -1;
122         return confstr (name, buf, (size_t) len);
123 }
124
125 #ifdef HAVE_TTYNAME_R
126 gint32
127 Mono_Posix_Syscall_ttyname_r (int fd, char *buf, mph_size_t len)
128 {
129         mph_return_if_size_t_overflow (len);
130         return ttyname_r (fd, buf, (size_t) len);
131 }
132 #endif /* ndef HAVE_TTYNAME_R */
133
134 gint32
135 Mono_Posix_Syscall_readlink (const char *path, char *buf, mph_size_t len)
136 {
137         int r;
138         mph_return_if_size_t_overflow (len);
139         r = readlink (path, buf, (size_t) len);
140         if (r >= 0 && r < len)
141                 buf [r] = '\0';
142         return r;
143 }
144
145 gint32
146 Mono_Posix_Syscall_getlogin_r (char *buf, mph_size_t len)
147 {
148         mph_return_if_size_t_overflow (len);
149         return getlogin_r (buf, (size_t) len);
150 }
151
152 gint32
153 Mono_Posix_Syscall_gethostname (char *buf, mph_size_t len)
154 {
155         mph_return_if_size_t_overflow (len);
156         return gethostname (buf, (size_t) len);
157 }
158
159 gint32
160 Mono_Posix_Syscall_sethostname (const char *name, mph_size_t len)
161 {
162         mph_return_if_size_t_overflow (len);
163         return sethostname (name, (size_t) len);
164 }
165
166 gint64
167 Mono_Posix_Syscall_gethostid (void)
168 {
169         return gethostid ();
170 }
171
172 #ifdef HAVE_SETHOSTID
173 gint32
174 Mono_Posix_Syscall_sethostid (gint64 hostid)
175 {
176         mph_return_if_long_overflow (hostid);
177 #ifdef MPH_ON_BSD
178         sethostid ((long) hostid);
179         return 0;
180 #else
181         return sethostid ((long) hostid);
182 #endif
183 }
184 #endif /* def HAVE_SETHOSTID */
185
186 #ifdef HAVE_GETDOMAINNAME
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 #endif /* def HAVE_GETDOMAINNAME */
194
195 #ifdef HAVE_SETDOMAINNAME
196 gint32
197 Mono_Posix_Syscall_setdomainname (const char *name, mph_size_t len)
198 {
199         mph_return_if_size_t_overflow (len);
200         return setdomainname (name, (size_t) len);
201 }
202 #endif /* def HAVE_SETDOMAINNAME */
203
204 gint32
205 Mono_Posix_Syscall_truncate (const char *path, mph_off_t length)
206 {
207         mph_return_if_off_t_overflow (length);
208         return truncate (path, (off_t) length);
209 }
210
211 gint32
212 Mono_Posix_Syscall_ftruncate (int fd, mph_off_t length)
213 {
214         mph_return_if_off_t_overflow (length);
215         return ftruncate (fd, (off_t) length);
216 }
217
218 gint32
219 Mono_Posix_Syscall_lockf (int fd, int cmd, mph_off_t len)
220 {
221         mph_return_if_off_t_overflow (len);
222         if (Mono_Posix_FromLockfCommand (cmd, &cmd) == -1)
223                 return -1;
224         return lockf (fd, cmd, (off_t) len);
225 }
226
227 void
228 Mono_Posix_Syscall_swab (void *from, void *to, mph_ssize_t n)
229 {
230         if (mph_have_long_overflow (n))
231                 return;
232         swab (from, to, (ssize_t) n);
233 }
234
235 G_END_DECLS
236
237 /*
238  * vim: noexpandtab
239  */