comparison dwcompat.h @ 1594:6baf177f335c

Rename compat.c/h dwcompat.c/h and configure option to --with-dwcompat. There are several other projects that include compat.c and compat.h... To avoid conflicts make sure the header and source files match the library.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 23 Feb 2012 12:44:15 +0000
parents compat.h@9a21fbd6ae50
children 71e0a3ad07f7
comparison
equal deleted inserted replaced
1593:19af25f71e1f 1594:6baf177f335c
1 /* $Id$ */
2
3 #ifndef _DWCOMPAT_H
4 #define _DWCOMPAT_H
5
6 /* This header includes and defines everything needed for a given OS/compiler */
7 #if defined(__UNIX__) || defined(__MAC__)
8 #include "config.h"
9
10 /* Attempt to include 64 bit file functions on various unix flavors */
11 #ifndef _FILE_OFFSET_BITS
12 #define _FILE_OFFSET_BITS 64
13 #endif
14 #ifndef _LARGEFILE_SOURCE
15 #define _LARGEFILE_SOURCE 1
16 #endif
17 #ifndef _LARGEFILE64_SOURCE
18 #define _LARGEFILE64_SOURCE 1
19 #endif
20 #ifndef _LARGE_FILES
21 #define _LARGE_FILES 1
22 #endif
23 #ifndef _DARWIN_USE_64_BIT_INODE
24 #define _DARWIN_USE_64_BIT_INODE 1
25 #endif
26
27 #include <sys/stat.h>
28 #include <unistd.h>
29 void msleep(long period);
30 #endif /* Unix */
31
32 #ifndef __TARGET__
33 #define __TARGET__ "dw"
34 #endif
35
36 #include <sys/types.h>
37 #if HAVE_DIRENT_H
38 #include <dirent.h>
39 #define NAMLEN(dirent) strlen((dirent)->d_name)
40 #else
41 #define dirent direct
42 #define NAMLEN(dirent) (dirent)->d_namlen
43 #if HAVE_SYS_NDIR_H
44 #include <sys/ndir.h>
45 #endif /* HAVE_SYS_NDIR_H */
46 #if HAVE_SYS_DIR_H
47 #include <sys/dir.h>
48 #endif /* HAVE_SYS_DIR_H */
49 #if HAVE_NDIR_H
50 #include <ndir.h>
51 #endif /* HAVE_NDIR_H */
52 #endif /* HAVE_DIRENT_H */
53
54 #ifdef DIRSEP
55 #undef DIRSEP
56 #endif
57
58 #ifdef __WATCOMC__
59 #include <alloca.h>
60 #include <sys/select.h>
61 #include <sys/stat.h>
62 # ifdef _stati64
63 # ifdef stat
64 # undef stat
65 # endif
66 # define stat(a, b) _stati64(a, b)
67 # define dwstat _stati64
68 # endif
69 #endif
70
71 #if defined(__EMX__) || defined(__OS2__) || defined(__WIN32__) || defined(WINNT)
72 #include <io.h>
73 #include <process.h>
74
75 #define DIRSEP "\\"
76 #define TYPDIR "."
77 #else
78 #define DIRSEP "/"
79 #define TYPDIR "/usr/local/" __TARGET__
80 #endif
81 #define INIDIR "~/." __TARGET__
82
83 /* OS/2 */
84 #if defined(__EMX__) || defined(__OS2__)
85 #define INCL_WIN
86 #define INCL_GPI
87 #define INCL_VIO
88 #define INCL_NLS
89 #define INCL_DOS
90 #define INCL_DEV
91 #define INCL_DOSERRORS
92
93 #ifdef __OS2__
94 # if (defined(__IBMC__) || defined(__WATCOMC__) || defined(_System)) && !defined(API)
95 # define API _System
96 # endif
97 #endif
98
99 #ifndef API
100 #define API
101 #endif
102
103 #define msleep(a) DosSleep(a)
104
105 #ifdef __EMX__
106 #include "platform/dirent.h"
107 #include <sys/stat.h>
108 #define HAVE_PIPE
109 #ifdef FD_SETSIZE
110 #undef FD_SETSIZE
111 #endif
112 #define FD_SETSIZE 1024
113 #endif /* __EMX__ */
114
115 #if defined(__EMX__) || defined(__WATCOMC__)
116 #define strcasecmp stricmp
117 #define strncasecmp strnicmp
118 #endif
119
120 #ifndef OS2
121 #define OS2
122 #endif /* OS2 */
123
124 #include <os2.h>
125
126 #ifndef BKS_TABBEDDIALOG
127 #define BKS_TABBEDDIALOG 0x0800
128 #endif
129
130 #define PIPENAME "\\socket\\" __TARGET__ "%d"
131 #define TPIPENAME "\\socket\\" __TARGET__ "%d"
132 #else
133 #define PIPENAME "/tmp/" __TARGET__ "%d"
134 #define TPIPENAME "/tmp/" __TARGET__ "%d"
135 #endif /* __EMX__ || __IBMC__ */
136
137 #if defined(__OS2__) && (defined(__IBMC__) || defined(__WATCOMC__))
138 #define BSD_SELECT
139
140 #include <types.h>
141 #include <time.h>
142 #include <sys/stat.h>
143 #include <sys/select.h>
144 #include <sys/ioctl.h>
145 #include <direct.h>
146 #include <stdarg.h>
147 /* For VAC we are using the Mozilla dirent.c */
148 #ifndef __WATCOMC__
149 #include "platform/dirent.h"
150 #endif
151 #endif
152
153 /* Windows */
154 #if defined(__WIN32__) || defined(WINNT)
155
156 #if defined(MSVC) && !defined(API)
157 # ifdef __MINGW32__
158 # ifdef BUILD_DLL
159 # define API APIENTRY __declspec(dllexport)
160 # else
161 # define API APIENTRY __declspec(dllimport)
162 # endif
163 # else
164 # define API _cdecl
165 # endif
166 #endif
167
168 #include <windows.h>
169 #include <winsock.h>
170 #include <time.h>
171 #include <process.h>
172 #include <sys/stat.h>
173
174 #ifdef MSVC
175 #include "platform/dirent.h"
176 #define alloca _alloca
177 #ifdef __stat64
178 #undef stat
179 #define stat(a, b) _stat64(a, b)
180 #define dwstat __stat64
181 #endif
182 #else
183 #include <dir.h>
184 #include <dirent.h>
185 #endif
186
187 #include <stdarg.h>
188
189 #if defined(__CYGWIN32__) /*|| defined(__MINGW32__)*/
190 #include <sys/un.h>
191 #endif /* __CYGWIN32__ || __MINGW32__ */
192
193 #ifndef __CYGWIN32__
194 #define NO_DOMAIN_SOCKETS
195 #endif /* __CYGWIN32__ */
196
197 #if defined(_P_NOWAIT) && !defined(P_NOWAIT)
198 #define P_NOWAIT _P_NOWAIT
199 #endif
200
201 #define strcasecmp stricmp
202 #define strncasecmp strnicmp
203 #define msleep Sleep
204
205 #endif /* WIN32 */
206
207 /* Everything else ;) */
208 #include <stdio.h>
209 #ifdef HAVE_UNISTD_H
210 #include <unistd.h>
211 #endif /* HAVE_UNISTD_H */
212 #ifdef HAVE_SYS_STAT_H
213 #include <sys/stat.h>
214 #endif
215 #include <stdlib.h>
216 #include <stddef.h>
217 #include <signal.h>
218 #include <fcntl.h>
219
220 #if !defined(__WIN32__) && !defined(WINNT)
221 #include <sys/time.h>
222 #include <sys/socket.h>
223 #include <sys/un.h>
224 #include <netinet/in.h>
225 #include <netdb.h>
226 #ifndef __IBMC__
227 #include <arpa/inet.h>
228 #endif
229 #if defined(__OS2__) && defined(RES_DEFAULT)
230 #undef RES_DEFAULT
231 #endif
232 #include <stdarg.h>
233 #include <string.h>
234 #endif /* !WIN32 */
235 #include <ctype.h>
236
237 #ifndef _MAX_PATH
238 #define _MAX_PATH 255
239 #endif
240
241 /* IBM C doesn't allow "t" in the mode parameter
242 * because it violates the ANSI standard.
243 */
244 #ifdef __IBMC__
245 #define FOPEN_READ_TEXT "r"
246 #define FOPEN_WRITE_TEXT "w"
247 #define FOPEN_APPEND_TEXT "a"
248 #else
249 #define FOPEN_READ_TEXT "rt"
250 #define FOPEN_WRITE_TEXT "wt"
251 #define FOPEN_APPEND_TEXT "at"
252 #endif
253 #define FOPEN_READ_BINARY "rb"
254 #define FOPEN_WRITE_BINARY "wb"
255 #define FOPEN_APPEND_BINARY "ab"
256
257 #ifndef API
258 #define API
259 #endif
260
261 /* Compatibility layer for IBM C/Winsock
262 * Now using macros so we can allow cross
263 * compiler support.
264 */
265
266 #if defined(__IBMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__))
267 #define sockread(a, b, c, d) recv(a, b, c, d)
268 #else
269 #define sockread(a, b, c, d) read(a, b, c)
270 #endif
271
272 #if defined(__IBMC__) || (defined(__WIN32__) && !defined(__CYGWIN32__))
273 #define sockwrite(a, b, c, d) send(a, b, c, d)
274 #else
275 #define sockwrite(a, b, c, d) write(a, b, c)
276 #endif
277
278 #ifdef __IBMC__
279 #define sockclose(a) soclose(a)
280 #elif defined(__WIN32__) && !defined(__CYGWIN32__)
281 #define sockclose(a) closesocket(a)
282 #else
283 #define sockclose(a) close(a)
284 #endif
285
286 #if defined(__OS2__) && !defined(__EMX__)
287 #define nonblock(a) { int _nonblock = 1; ioctl(a, FIONBIO, (char *)&_nonblock, sizeof(_nonblock)); }
288 #elif defined(__WIN32__) && !defined(__CYGWIN32__)
289 #define nonblock(a) { int _nonblock = 1; ioctlsocket(a, FIONBIO, (unsigned long *)&_nonblock); }
290 #else
291 #define nonblock(a) fcntl(a, F_SETFL, O_NONBLOCK)
292 #endif
293
294 #if defined(__OS2__) && !defined(__EMX__)
295 #define block(a) { int _block = 0; ioctl(a, FIONBIO, (char *)&_nonblock, sizeof(_block)); }
296 #elif defined(__WIN32__) && !defined(__CYGWIN32__)
297 #define block(a) { int _block = 0; ioctlsocket(a, FIONBIO, (unsigned long *)&_block); }
298 #else
299 #define block(a) fcntl(a, F_SETFL, 0)
300 #endif
301
302 #ifdef __IBMC__
303 #define sockinit() sock_init();
304 #elif defined(__WIN32__) || defined(WINNT)
305 #define sockinit() { static WSADATA wsa; WSAStartup(MAKEWORD (1, 1), &wsa); }
306 #else /* !WIN32 */
307 #define sockinit()
308 #endif
309
310 #if defined(__WIN32__) || defined(WINNT)
311 #define sockshutdown() WSACleanup()
312 #else /* !WIN32 */
313 #define sockshutdown()
314 #endif
315
316 #ifdef HAVE_PIPE
317 #define sockpipe(pipes) { if(pipe(pipes) < 0) pipes[0] = pipes[1] = -1; }
318 #elif !defined(NO_DOMAIN_SOCKETS)
319 #define sockpipe(pipes) { \
320 struct sockaddr_un un; \
321 int tmpsock = socket(AF_UNIX, SOCK_STREAM, 0); \
322 pipes[1] = socket(AF_UNIX, SOCK_STREAM, 0); \
323 memset(&un, 0, sizeof(un)); \
324 un.sun_family=AF_UNIX; \
325 sprintf(un.sun_path, PIPENAME, pipes[1]); \
326 bind(tmpsock, (struct sockaddr *)&un, sizeof(un)); \
327 listen(tmpsock, 0); \
328 connect(pipes[1], (struct sockaddr *)&un, sizeof(un)); \
329 pipes[0] = accept(tmpsock, 0, 0); \
330 sockclose(tmpsock); \
331 }
332 #else
333 #define sockpipe(pipes) { \
334 struct sockaddr_in server_addr; \
335 struct sockaddr_in listen_addr = { 0 }; \
336 int tmpsock, len = sizeof(struct sockaddr_in); \
337 struct hostent *he = gethostbyname("localhost"); \
338 pipes[0] = pipes[1] = -1; \
339 if(he) \
340 { \
341 memset(&server_addr, 0, sizeof(server_addr)); \
342 server_addr.sin_family = AF_INET; \
343 server_addr.sin_port = 0; \
344 server_addr.sin_addr.s_addr = INADDR_ANY; \
345 if ((tmpsock = socket(AF_INET, SOCK_STREAM, 0)) > -1 && bind(tmpsock, (struct sockaddr *)&server_addr, sizeof(server_addr)) > -1 && listen(tmpsock, 0) > -1) \
346 { \
347 memset(&listen_addr, 0, sizeof(listen_addr)); \
348 getsockname(tmpsock, (struct sockaddr *)&listen_addr, &len); \
349 server_addr.sin_family = AF_INET; \
350 server_addr.sin_port = listen_addr.sin_port; \
351 server_addr.sin_addr.s_addr = *((unsigned long *)he->h_addr); \
352 if((pipes[1] = socket(AF_INET, SOCK_STREAM, 0)) > -1 && !connect(pipes[1], (struct sockaddr *)&server_addr, sizeof(server_addr))) \
353 pipes[0] = accept(tmpsock, 0, 0); \
354 } \
355 sockclose(tmpsock); \
356 } \
357 }
358 #endif
359
360 /* Ok Windows and OS/2 both seem to be missing this */
361 #if defined(__WIN32__) || defined(__OS2__)
362 typedef int socklen_t;
363 #endif
364
365 /* If dwstat didn't otherwise get defined */
366 #ifndef dwstat
367 #define dwstat stat
368 #endif
369
370 #define socksprint(a, b) sockwrite(a, b, strlen(b), 0)
371
372 char * API vargs(char *buf, int len, char *format, ...);
373 int API makedir(char *path);
374 void API setfileinfo(char *filename, char *url, char *logfile);
375 long double API drivesize(int drive);
376 long double API drivefree(int drive);
377 int API isdrive(int drive);
378 void API getfsname(int drive, char *buf, int len);
379 FILE * API fsopen(char *path, char *modes);
380 int API fsclose(FILE *fp);
381 char * API fsgets(char *str, int size, FILE *stream);
382 int API fsseek(FILE *stream, long offset, int whence);
383 int API locale_init(char *filename, int my_locale);
384 char * API locale_string(char *default_text, int message);
385 void API nice_strformat(char *dest, long double val, int dec);
386 void API initdir(int argc, char *argv[]);
387 int API setpath(char *path);
388
389 #ifdef __MINGW32__
390 # undef API
391 # define API APIENTRY
392 #endif
393
394 #endif