Mercurial > dwindows
comparison compat.h @ 580:d7c338ac926a
Sweeping changes to the compatibility module, moved all socket code out
of the DLL and into macros in compat.h, socksprintf() is now gone, replaced
by the sockprint() macro and the vargs() function. This allows the DLL
and application to be compiled with different compilers (and C libraries).
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Fri, 20 May 2005 03:32:25 +0000 |
parents | 828e6a66c5c5 |
children | 53639d920735 |
comparison
equal
deleted
inserted
replaced
579:cea01bd2e0a7 | 580:d7c338ac926a |
---|---|
218 | 218 |
219 #ifndef API | 219 #ifndef API |
220 #define API | 220 #define API |
221 #endif | 221 #endif |
222 | 222 |
223 /* Compatibility layer for IBM C/Winsock */ | 223 /* Compatibility layer for IBM C/Winsock |
224 int API sockread (int a, void *b, int c, int d); | 224 * Now using macros so we can allow cross |
225 int API sockwrite (int a, void *b, int c, int d); | 225 * compiler support. |
226 int API sockclose(int a); | 226 */ |
227 int API socksprintf(int fd, char *format, ...); | 227 |
228 int API sockpipe(int *pipes); | 228 #if defined(__IBMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__)) |
229 void API sockinit(void); | 229 #define sockread(a, b, c, d) recv(a, b, c, d) |
230 void API sockshutdown(void); | 230 #else |
231 #define sockread(a, b, c, d) read(a, b, c) | |
232 #endif | |
233 | |
234 #if defined(__IBMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__)) | |
235 #define sockwrite(a, b, c, d) send(a, b, c, d) | |
236 #else | |
237 #define sockwrite(a, b, c, d) write(a, b, c) | |
238 #endif | |
239 | |
240 #ifdef __IBMC__ | |
241 #define sockclose(a) soclose(a) | |
242 #elif defined(__WIN32__) && !defined(__CYGWIN32__) | |
243 #define sockclose(a) closesocket(a) | |
244 #else | |
245 #define sockclose(a) close(a) | |
246 #endif | |
247 | |
248 #if defined(__OS2__) && !defined(__EMX__) | |
249 #define nonblock(a) { int _nonblock = 1; ioctl(a, FIONBIO, (char *)&_nonblock, sizeof(_nonblock)); } | |
250 #elif defined(__WIN32__) && !defined(__CYGWIN32__) | |
251 #define nonblock(a) { int _nonblock = 1; ioctlsocket(a, FIONBIO, (unsigned long *)&_nonblock); } | |
252 #else | |
253 #define nonblock(a) fcntl(a, F_SETFL, O_NONBLOCK) | |
254 #endif | |
255 | |
256 #if defined(__OS2__) && !defined(__EMX__) | |
257 #define block(a) { int _block = 0; ioctl(a, FIONBIO, (char *)&_nonblock, sizeof(_block)); } | |
258 #elif defined(__WIN32__) && !defined(__CYGWIN32__) | |
259 #define block(a) { int _block = 0; ioctlsocket(a, FIONBIO, (unsigned long *)&_block); } | |
260 #else | |
261 #define block(a) fcntl(a, F_SETFL, 0) | |
262 #endif | |
263 | |
264 #ifdef __IBMC__ | |
265 #define sockinit() sock_init(); | |
266 #elif defined(__WIN32__) || defined(WINNT) | |
267 static WSADATA wsa; | |
268 #define sockinit() WSAStartup(MAKEWORD (1, 1), &wsa) | |
269 #else /* !WIN32 */ | |
270 #define sockinit() | |
271 #endif | |
272 | |
273 #if defined(__WIN32__) || defined(WINNT) | |
274 #define sockshutdown() WSACleanup() | |
275 #else /* !WIN32 */ | |
276 #define sockshutdown() | |
277 #endif | |
278 | |
279 #ifdef HAVE_PIPE | |
280 #define sockpipe(pipes) { if(pipe(pipes) < 0) pipes[0] = pipes[1] = -1; } | |
281 #elif !defined(NO_DOMAIN_SOCKETS) | |
282 #define sockpipe(pipes) { \ | |
283 struct sockaddr_un un; \ | |
284 int tmpsock = socket(AF_UNIX, SOCK_STREAM, 0); \ | |
285 pipes[1] = socket(AF_UNIX, SOCK_STREAM, 0); \ | |
286 memset(&un, 0, sizeof(un)); \ | |
287 un.sun_family=AF_UNIX; \ | |
288 sprintf(un.sun_path, PIPENAME, pipes[1]); \ | |
289 bind(tmpsock, (struct sockaddr *)&un, sizeof(un)); \ | |
290 listen(tmpsock, 0); \ | |
291 connect(pipes[1], (struct sockaddr *)&un, sizeof(un)); \ | |
292 pipes[0] = accept(tmpsock, 0, 0); \ | |
293 sockclose(tmpsock); \ | |
294 } | |
295 #else | |
296 #define sockpipe(pipes) { \ | |
297 struct sockaddr_in server_addr; \ | |
298 struct sockaddr_in listen_addr = { 0 }; \ | |
299 int len = sizeof(struct sockaddr_in); \ | |
300 struct hostent *he = gethostbyname("localhost"); \ | |
301 pipes[0] = pipes[1] = -1; \ | |
302 if(he) \ | |
303 { \ | |
304 memset(&server_addr, 0, sizeof(server_addr)); \ | |
305 server_addr.sin_family = AF_INET; \ | |
306 server_addr.sin_port = 0; \ | |
307 server_addr.sin_addr.s_addr = INADDR_ANY; \ | |
308 if ((tmpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0 || bind(tmpsock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0 || listen(tmpsock, 0) < 0) \ | |
309 break; \ | |
310 memset(&listen_addr, 0, sizeof(listen_addr)); \ | |
311 getsockname(tmpsock, (struct sockaddr *)&listen_addr, &len); \ | |
312 server_addr.sin_family = AF_INET; \ | |
313 server_addr.sin_port = listen_addr.sin_port; \ | |
314 server_addr.sin_addr.s_addr = *((unsigned long *)he->h_addr); \ | |
315 if((pipes[1] = socket(AF_INET, SOCK_STREAM, 0)) < 0 || connect(pipes[1], (struct sockaddr *)&server_addr, sizeof(server_addr))) \ | |
316 break; \ | |
317 else \ | |
318 pipes[0] = accept(tmpsock, 0, 0); \ | |
319 sockclose(tmpsock); \ | |
320 } | |
321 #endif | |
322 | |
323 #define socksprint(a, b) sockwrite(a, b, strlen(b), 0) | |
324 | |
325 char * API vargs(char *buf, int len, char *format, ...); | |
231 int API makedir(char *path); | 326 int API makedir(char *path); |
232 void API nonblock(int fd); | |
233 void API block(int fd); | |
234 void API setfileinfo(char *filename, char *url, char *logfile); | 327 void API setfileinfo(char *filename, char *url, char *logfile); |
235 long double API drivesize(int drive); | 328 long double API drivesize(int drive); |
236 long double API drivefree(int drive); | 329 long double API drivefree(int drive); |
237 int API isdrive(int drive); | 330 int API isdrive(int drive); |
238 void API getfsname(int drive, char *buf, int len); | 331 void API getfsname(int drive, char *buf, int len); |