Fixed some C/C++ compiler errors due to stricter checks.
[rubinius.git] / machine / windows_compat.h
blob8d6dd10a2acc06d44c1c32eb201a01470e6451f9
1 #ifndef RBX_WINDOWS_COMPAT_H
2 #define RBX_WINDOWS_COMPAT_H
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
8 #ifdef RBX_WINDOWS
10 // crypt
11 char *crypt(const char *, const char *);
13 // dlopen
14 #include <winsock2.h>
16 int map_errno(DWORD winerr);
18 inline void* dlopen(const char* name, int mode) {
19 return LoadLibrary(name);
22 inline int dlclose(void* handle) {
23 return !FreeLibrary((HMODULE)handle);
26 inline const char* dlerror(void) {
27 return strerror(map_errno(GetLastError()));
30 inline void* dlsym(void* handle, const char* symbol) {
31 return (void*)GetProcAddress((HMODULE)handle, symbol);
34 #define RTLD_LAZY -1
35 #define RTLD_NOW -1
36 #define RTLD_GLOBAL -1
38 // environment
39 int setenv(const char *name, const char *value, int overwrite);
40 int unsetenv(const char *name);
42 // Adapted from BSD source code.
43 #define UNAME_NAMELEN 256
45 struct utsname {
46 char sysname[UNAME_NAMELEN]; /* Name of OS */
47 char nodename[UNAME_NAMELEN]; /* Name of this network node */
48 char release[UNAME_NAMELEN]; /* Release level */
49 char version[UNAME_NAMELEN]; /* Version level */
50 char machine[UNAME_NAMELEN]; /* Hardware type */
53 int uname(struct utsname *name);
55 // socket
57 #include <winsock2.h>
59 #ifndef SHUT_RD
60 #ifdef SD_RECEIVE
61 #define SHUT_RD SD_RECEIVE
62 #else
63 #define SHUT_RD 0
64 #endif
65 #endif
67 #ifndef SHUT_WR
68 #ifdef SD_SEND
69 #define SHUT_WR SD_SEND
70 #else
71 #define SHUT_WR 1
72 #endif
73 #endif
75 #ifndef SHUT_RDWR
76 #ifdef SD_BOTH
77 #define SHUT_RDWR SD_BOTH
78 #else
79 #define SHUT_RDWR 2
80 #endif
81 #endif
83 #include <errno.h>
84 #include <pthread.h>
86 #ifndef EWOULDBLOCK
87 # define EWOULDBLOCK WSAEWOULDBLOCK
88 #endif
89 #ifndef EINPROGRESS
90 # define EINPROGRESS WSAEINPROGRESS
91 #endif
92 #ifndef EALREADY
93 # define EALREADY WSAEALREADY
94 #endif
95 #ifndef ENOTSOCK
96 # define ENOTSOCK WSAENOTSOCK
97 #endif
98 #ifndef EDESTADDRREQ
99 # define EDESTADDRREQ WSAEDESTADDRREQ
100 #endif
101 #ifndef EMSGSIZE
102 # define EMSGSIZE WSAEMSGSIZE
103 #endif
104 #ifndef EPROTOTYPE
105 # define EPROTOTYPE WSAEPROTOTYPE
106 #endif
107 #ifndef ENOPROTOOPT
108 # define ENOPROTOOPT WSAENOPROTOOPT
109 #endif
110 #ifndef EPROTONOSUPPORT
111 # define EPROTONOSUPPORT WSAEPROTONOSUPPORT
112 #endif
113 #ifndef ESOCKTNOSUPPORT
114 # define ESOCKTNOSUPPORT WSAESOCKTNOSUPPORT
115 #endif
116 #ifndef EOPNOTSUPP
117 # define EOPNOTSUPP WSAEOPNOTSUPP
118 #endif
119 #ifndef EPFNOSUPPORT
120 # define EPFNOSUPPORT WSAEPFNOSUPPORT
121 #endif
122 #ifndef EAFNOSUPPORT
123 # define EAFNOSUPPORT WSAEAFNOSUPPORT
124 #endif
125 #ifndef EADDRINUSE
126 # define EADDRINUSE WSAEADDRINUSE
127 #endif
128 #ifndef EADDRNOTAVAIL
129 # define EADDRNOTAVAIL WSAEADDRNOTAVAIL
130 #endif
131 #ifndef ENETDOWN
132 # define ENETDOWN WSAENETDOWN
133 #endif
134 #ifndef ENETUNREACH
135 # define ENETUNREACH WSAENETUNREACH
136 #endif
137 #ifndef ENETRESET
138 # define ENETRESET WSAENETRESET
139 #endif
140 #ifndef ECONNABORTED
141 # define ECONNABORTED WSAECONNABORTED
142 #endif
143 #ifndef ECONNRESET
144 # define ECONNRESET WSAECONNRESET
145 #endif
146 #ifndef ENOBUFS
147 # define ENOBUFS WSAENOBUFS
148 #endif
149 #ifndef EISCONN
150 # define EISCONN WSAEISCONN
151 #endif
152 #ifndef ENOTCONN
153 # define ENOTCONN WSAENOTCONN
154 #endif
155 #ifndef ESHUTDOWN
156 # define ESHUTDOWN WSAESHUTDOWN
157 #endif
158 #ifndef ETOOMANYREFS
159 # define ETOOMANYREFS WSAETOOMANYREFS
160 #endif
161 #ifndef ETIMEDOUT
162 # define ETIMEDOUT WSAETIMEDOUT
163 #endif
164 #ifndef ECONNREFUSED
165 # define ECONNREFUSED WSAECONNREFUSED
166 #endif
167 #ifndef ELOOP
168 # define ELOOP WSAELOOP
169 #endif
170 #ifndef EHOSTDOWN
171 # define EHOSTDOWN WSAEHOSTDOWN
172 #endif
173 #ifndef EHOSTUNREACH
174 # define EHOSTUNREACH WSAEHOSTUNREACH
175 #endif
176 #ifndef EPROCLIM
177 # define EPROCLIM WSAEPROCLIM
178 #endif
179 #ifndef EUSERS
180 # define EUSERS WSAEUSERS
181 #endif
182 #ifndef EDQUOT
183 # define EDQUOT WSAEDQUOT
184 #endif
185 #ifndef ESTALE
186 # define ESTALE WSAESTALE
187 #endif
188 #ifndef EREMOTE
189 # define EREMOTE WSAEREMOTE
190 #endif
193 int socketpair(int domain, int type, int protocol, int socket_vector[2]);
194 int pipe(int fildes[2]);
196 // file system
197 char* realpath(char* file_name, char* resolved_name);
199 // time
200 #define timezone _timezone
202 time_t timegm(struct tm* tm);
203 int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
205 #ifndef localtime_r
206 #define localtime_r(_Time, _Tm) ({ \
207 struct tm *___tmp_tm = localtime((_Time)); \
208 if (___tmp_tm) { \
209 *(_Tm) = *___tmp_tm; \
210 ___tmp_tm = (_Tm); \
212 ___tmp_tm; \
214 #endif
216 #ifndef gmtime_r
217 #define gmtime_r(_Time,_Tm) ({ \
218 struct tm *___tmp_tm = gmtime((_Time)); \
219 if (___tmp_tm) { \
220 *(_Tm) = *___tmp_tm; \
221 ___tmp_tm = (_Tm); \
223 ___tmp_tm; \
225 #endif
227 #define RBX_USEC_PER_SEC 1000000
228 #define timeradd(tvp, uvp, vvp) \
229 do { \
230 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
231 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
232 if ((vvp)->tv_usec >= RBX_USEC_PER_SEC) { \
233 (vvp)->tv_sec++; \
234 (vvp)->tv_usec -= RBX_USEC_PER_SEC; \
236 } while (0)
237 #define timersub(tvp, uvp, vvp) \
238 do { \
239 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
240 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
241 if ((vvp)->tv_usec < 0) { \
242 (vvp)->tv_sec--; \
243 (vvp)->tv_usec += RBX_USEC_PER_SEC; \
245 } while (0)
247 // fcntl
248 #define F_SETFL 1
249 #define O_NONBLOCK 1
251 int fcntl(int fildes, int cmd, ...);
253 #endif // RBX_WINDOWS
255 // Keep these ifdef short so they are clear. Each one
256 // defines a Windows and non-Windows alternative.
257 #ifdef RBX_WINDOWS
258 // MinGW defines setjmp in terms of _setjmp.
259 #define set_jump(x) setjmp(x)
260 #define long_jump(x, y) longjmp(x, y)
261 #else
262 #define set_jump(x) _setjmp(x)
263 #define long_jump(x, y) _longjmp(x, y)
264 #endif
266 #ifdef RBX_WINDOWS
267 typedef unsigned int int_fd_t;
268 #else
269 typedef int int_fd_t;
270 #endif
272 #ifdef __cplusplus
274 #endif
276 #endif // RBX_WINDOWS_COMPAT_H