1 /*
2  * tclWinPort.h --
3  *
4  *  This header file handles porting issues that occur because of
5  *  differences between Windows and Unix. It should be the only
6  *  file that contains #ifdefs to handle different flavors of OS.
7  *
8  * Copyright (c) 1994-1997 Sun Microsystems, Inc.
9  *
10  * See the file "license.terms" for information on usage and redistribution
11  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12  *
13  * RCS: @(#) Id
14  */
15 
16 #ifndef _TCLWINPORT
17 #define _TCLWINPORT
18 
19 #ifdef CHECK_UNICODE_CALLS
20 #   define _UNICODE
21 #   define UNICODE
22 #   define __TCHAR_DEFINED
23     typedef float *_TCHAR;
24 #   define _TCHAR_DEFINED
25     typedef float *TCHAR;
26 #endif /* CHECK_UNICODE_CALLS */
27 
28 /*
29  *---------------------------------------------------------------------------
30  * The following sets of #includes and #ifdefs are required to get Tcl to
31  * compile under the windows compilers.
32  *---------------------------------------------------------------------------
33  */
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <float.h>
41 #include <io.h>
42 #include <malloc.h>
43 #include <process.h>
44 #include <signal.h>
45 #include <string.h>
46 
47 /*
48  * These string functions are not defined with the same names on Windows.
49  */
50 
51 #define strcasecmp stricmp
52 #define strncasecmp strnicmp
53 
54 /*
55  * Need to block out these includes for building extensions with MetroWerks
56  * compiler for Win32.
57  */
58 
59 #ifndef __MWERKS__
60 #include <sys/stat.h>
61 #include <sys/timeb.h>
62 #   ifdef __BORLANDC__
63 #  include <utime.h>
64 #   else
65 #  include <sys/utime.h>
66 #   endif /* __BORLANDC__ */
67 #endif /* __MWERKS__ */
68 
69 #include <time.h>
70 
71 #define WIN32_LEAN_AND_MEAN
72 #include <windows.h>
73 #undef WIN32_LEAN_AND_MEAN
74 
75 /*
76  * Ask for the winsock function typedefs, also.
77  */
78 #define INCL_WINSOCK_API_TYPEDEFS   1
79 #include <winsock2.h>
80 
81 /*
82  * Define EINPROGRESS in terms of WSAEINPROGRESS.
83  */
84 
85 #ifndef  EINPROGRESS
86 #   define EINPROGRESS  WSAEINPROGRESS
87 #endif
88 
89 /*
90  * If ENOTSUP is not defined, define it to a value that will never occur.
91  */
92 
93 #ifndef ENOTSUP
94 #   define ENOTSUP  -1030507
95 #endif
96 
97 /*
98  * The following defines redefine the Windows Socket errors as
99  * BSD errors so Tcl_PosixError can do the right thing.
100  */
101 
102 #ifndef EWOULDBLOCK
103 #   define EWOULDBLOCK  EAGAIN
104 #endif
105 #ifndef EALREADY
106 #   define EALREADY  149  /* operation already in progress */
107 #endif
108 #ifndef ENOTSOCK
109 #   define ENOTSOCK  95  /* Socket operation on non-socket */
110 #endif
111 #ifndef EDESTADDRREQ
112 #   define EDESTADDRREQ  96  /* Destination address required */
113 #endif
114 #ifndef EMSGSIZE
115 #   define EMSGSIZE  97  /* Message too long */
116 #endif
117 #ifndef EPROTOTYPE
118 #   define EPROTOTYPE  98  /* Protocol wrong type for socket */
119 #endif
120 #ifndef ENOPROTOOPT
121 #   define ENOPROTOOPT  99  /* Protocol not available */
122 #endif
123 #ifndef EPROTONOSUPPORT
124 #   define EPROTONOSUPPORT 120  /* Protocol not supported */
125 #endif
126 #ifndef ESOCKTNOSUPPORT
127 #   define ESOCKTNOSUPPORT 121  /* Socket type not supported */
128 #endif
129 #ifndef EOPNOTSUPP
130 #   define EOPNOTSUPP  122  /* Operation not supported on socket */
131 #endif
132 #ifndef EPFNOSUPPORT
133 #   define EPFNOSUPPORT  123  /* Protocol family not supported */
134 #endif
135 #ifndef EAFNOSUPPORT
136 #   define EAFNOSUPPORT  124  /* Address family not supported */
137 #endif
138 #ifndef EADDRINUSE
139 #   define EADDRINUSE  125  /* Address already in use */
140 #endif
141 #ifndef EADDRNOTAVAIL
142 #   define EADDRNOTAVAIL 126  /* Can't assign requested address */
143 #endif
144 #ifndef ENETDOWN
145 #   define ENETDOWN  127  /* Network is down */
146 #endif
147 #ifndef ENETUNREACH
148 #   define ENETUNREACH  128  /* Network is unreachable */
149 #endif
150 #ifndef ENETRESET
151 #   define ENETRESET  129  /* Network dropped connection on reset */
152 #endif
153 #ifndef ECONNABORTED
154 #   define ECONNABORTED  130  /* Software caused connection abort */
155 #endif
156 #ifndef ECONNRESET
157 #   define ECONNRESET  131  /* Connection reset by peer */
158 #endif
159 #ifndef ENOBUFS
160 #   define ENOBUFS  132  /* No buffer space available */
161 #endif
162 #ifndef EISCONN
163 #   define EISCONN  133  /* Socket is already connected */
164 #endif
165 #ifndef ENOTCONN
166 #   define ENOTCONN  134  /* Socket is not connected */
167 #endif
168 #ifndef ESHUTDOWN
169 #   define ESHUTDOWN  143  /* Can't send after socket shutdown */
170 #endif
171 #ifndef ETOOMANYREFS
172 #   define ETOOMANYREFS  144  /* Too many references: can't splice */
173 #endif
174 #ifndef ETIMEDOUT
175 #   define ETIMEDOUT  145  /* Connection timed out */
176 #endif
177 #ifndef ECONNREFUSED
178 #   define ECONNREFUSED  146  /* Connection refused */
179 #endif
180 #ifndef ELOOP
181 #   define ELOOP  90  /* Symbolic link loop */
182 #endif
183 #ifndef EHOSTDOWN
184 #   define EHOSTDOWN  147  /* Host is down */
185 #endif
186 #ifndef EHOSTUNREACH
187 #   define EHOSTUNREACH  148  /* No route to host */
188 #endif
189 #ifndef ENOTEMPTY
190 #   define ENOTEMPTY   93  /* directory not empty */
191 #endif
192 #ifndef EUSERS
193 #   define EUSERS  94  /* Too many users (for UFS) */
194 #endif
195 #ifndef EDQUOT
196 #   define EDQUOT  69  /* Disc quota exceeded */
197 #endif
198 #ifndef ESTALE
199 #   define ESTALE  151  /* Stale NFS file handle */
200 #endif
201 #ifndef EREMOTE
202 #   define EREMOTE  66  /* The object is remote */
203 #endif
204 
205 /*
206  * It is very hard to determine how Windows reacts to attempting to
207  * set a file pointer outside the input datatype's representable
208  * region.  So we fake the error code ourselves.
209  */
210 
211 #ifndef EOVERFLOW
212 #   ifdef EFBIG
213 #      define EOVERFLOW  EFBIG  /* The object couldn't fit in the datatype */
214 #   else /* !EFBIG */
215 #      define EOVERFLOW  EINVAL  /* Better than nothing! */
216 #   endif /* EFBIG */
217 #endif /* !EOVERFLOW */
218 
219 /*
220  * Signals not known to the standard ANSI signal.h.  These are used
221  * by Tcl_WaitPid() and generic/tclPosixStr.c
222  */
223 
224 #ifndef SIGTRAP
225 #   define SIGTRAP  5
226 #endif
227 #ifndef SIGBUS
228 #   define SIGBUS   10
229 #endif
230 
231 /*
232  * Supply definitions for macros to query wait status, if not already
233  * defined in header files above.
234  */
235 
236 #if TCL_UNION_WAIT
237 #   define WAIT_STATUS_TYPE union wait
238 #else
239 #   define WAIT_STATUS_TYPE int
240 #endif /* TCL_UNION_WAIT */
241 
242 #ifndef WIFEXITED
243 #   define WIFEXITED(stat)  (((*((int *) &(stat))) & 0xC0000000) == 0)
244 #endif
245 
246 #ifndef WEXITSTATUS
247 #   define WEXITSTATUS(stat) (*((int *) &(stat)))
248 #endif
249 
250 #ifndef WIFSIGNALED
251 #   define WIFSIGNALED(stat) ((*((int *) &(stat))) & 0xC0000000)
252 #endif
253 
254 #ifndef WTERMSIG
255 #   define WTERMSIG(stat)    ((*((int *) &(stat))) & 0x7f)
256 #endif
257 
258 #ifndef WIFSTOPPED
259 #   define WIFSTOPPED(stat)  0
260 #endif
261 
262 #ifndef WSTOPSIG
263 #   define WSTOPSIG(stat)    (((*((int *) &(stat))) >> 8) & 0xff)
264 #endif
265 
266 /*
267  * Define constants for waitpid() system call if they aren't defined
268  * by a system header file.
269  */
270 
271 #ifndef WNOHANG
272 #   define WNOHANG 1
273 #endif
274 #ifndef WUNTRACED
275 #   define WUNTRACED 2
276 #endif
277 
278 /*
279  * Define access mode constants if they aren't already defined.
280  */
281 
282 #ifndef F_OK
283 #    define F_OK 00
284 #endif
285 #ifndef X_OK
286 #    define X_OK 01
287 #endif
288 #ifndef W_OK
289 #    define W_OK 02
290 #endif
291 #ifndef R_OK
292 #    define R_OK 04
293 #endif
294 
295 /*
296  * Define macros to query file type bits, if they're not already
297  * defined.
298  */
299 
300 #ifndef S_IFLNK
301 #define S_IFLNK        0120000  /* Symbolic Link */
302 #endif
303 
304 #ifndef S_ISREG
305 #   ifdef S_IFREG
306 #       define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
307 #   else
308 #       define S_ISREG(m) 0
309 #   endif
310 #endif /* !S_ISREG */
311 #ifndef S_ISDIR
312 #   ifdef S_IFDIR
313 #       define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
314 #   else
315 #       define S_ISDIR(m) 0
316 #   endif
317 #endif /* !S_ISDIR */
318 #ifndef S_ISCHR
319 #   ifdef S_IFCHR
320 #       define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
321 #   else
322 #       define S_ISCHR(m) 0
323 #   endif
324 #endif /* !S_ISCHR */
325 #ifndef S_ISBLK
326 #   ifdef S_IFBLK
327 #       define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
328 #   else
329 #       define S_ISBLK(m) 0
330 #   endif
331 #endif /* !S_ISBLK */
332 #ifndef S_ISFIFO
333 #   ifdef S_IFIFO
334 #       define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
335 #   else
336 #       define S_ISFIFO(m) 0
337 #   endif
338 #endif /* !S_ISFIFO */
339 #ifndef S_ISLNK
340 #   ifdef S_IFLNK
341 #       define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
342 #   else
343 #       define S_ISLNK(m) 0
344 #   endif
345 #endif /* !S_ISLNK */
346 
347 
348 /*
349  * Define MAXPATHLEN in terms of MAXPATH if available
350  */
351 
352 #ifndef MAXPATH
353 #define MAXPATH MAX_PATH
354 #endif /* MAXPATH */
355 
356 #ifndef MAXPATHLEN
357 #define MAXPATHLEN MAXPATH
358 #endif /* MAXPATHLEN */
359 
360 /*
361  * Define pid_t and uid_t if they're not already defined.
362  */
363 
364 #if ! TCL_PID_T
365 #   define pid_t int
366 #endif /* !TCL_PID_T */
367 #if ! TCL_UID_T
368 #   define uid_t int
369 #endif /* !TCL_UID_T */
370 
371 /*
372  * Visual C++ has some odd names for common functions, so we need to
373  * define a few macros to handle them.  Also, it defines EDEADLOCK and
374  * EDEADLK as the same value, which confuses Tcl_ErrnoId().
375  */
376 
377 #if defined(_MSC_VER) || defined(__MINGW32__)
378 #    define environ _environ
379 #    define hypot _hypot
380 #    define exception _exception
381 #    undef EDEADLOCK
382 #    if defined(__MINGW32__) && !defined(__MSVCRT__)
383 #  define timezone _timezone
384 #    endif
385 #endif /* _MSC_VER || __MINGW32__ */
386 
387 /*
388  * Borland's timezone and environ functions.
389  */
390 
391 #ifdef  __BORLANDC__
392 #   define timezone _timezone
393 #   define environ  _environ
394 #endif /* __BORLANDC__ */
395 
396 #ifdef __CYGWIN__
397 /* On Cygwin, the environment is imported from the Cygwin DLL. */
398      DLLIMPORT extern char **__cygwin_environ;
399 #    define environ __cygwin_environ
400 #    define putenv TclCygwinPutenv
401 #    define timezone _timezone
402 #endif /* __CYGWIN__ */
403 
404 
405 #ifdef __WATCOMC__
406     /*
407      * OpenWatcom uses a wine derived winsock2.h that is missing the
408      * LPFN_* typedefs.
409      */
410 #   define HAVE_NO_LPFN_DECLS
411 #   if !defined(__CHAR_SIGNED__)
412 #  error "You must use the -j switch to ensure char is signed."
413 #   endif
414 #endif
415 
416 
417 /*
418  * MSVC 8.0 started to mark many standard C library functions depreciated
419  * including the *printf family and others. Tell it to shut up.
420  * (_MSC_VER is 1200 for VC6, 1300 or 1310 for vc7.net, 1400 for 8.0)
421  */
422 #if _MSC_VER >= 1400
423 #pragma warning(disable:4996)
424 #endif
425 
426 
427 /*
428  * There is no platform-specific panic routine for Windows in the Tcl internals.
429  */
430 
431 #define TclpPanic ((Tcl_PanicProc *) NULL)
432 
433 /*
434  *---------------------------------------------------------------------------
435  * The following macros and declarations represent the interface between
436  * generic and windows-specific parts of Tcl.  Some of the macros may
437  * override functions declared in tclInt.h.
438  *---------------------------------------------------------------------------
439  */
440 
441 /*
442  * The default platform eol translation on Windows is TCL_TRANSLATE_CRLF:
443  */
444 
445 #define  TCL_PLATFORM_TRANSLATION  TCL_TRANSLATE_CRLF
446 
447 /*
448  * Declare dynamic loading extension macro.
449  */
450 
451 #define TCL_SHLIB_EXT ".dll"
452 
453 /*
454  * The following define ensures that we use the native putenv
455  * implementation to modify the environment array.  This keeps
456  * the C level environment in synch with the system level environment.
457  */
458 
459 #define USE_PUTENV    1
460 #define USE_PUTENV_FOR_UNSET  1
461 
462 /*
463  * Msvcrt's putenv() copies the string rather than takes ownership of it.
464  */
465 
466 #if defined(_MSC_VER) || defined(__MINGW32__)
467 #   define HAVE_PUTENV_THAT_COPIES 1
468 #endif
469 
470 /*
471  * Older version of Mingw are known to lack a MWMO_ALERTABLE define.
472  */
473 #if defined(HAVE_NO_MWMO_ALERTABLE)
474 #   define MWMO_ALERTABLE 2
475 #endif
476 
477 /*
478  * The following defines wrap the system memory allocation routines for
479  * use by tclAlloc.c.
480  */
481 
482 #ifdef __CYGWIN__
483 #   define TclpSysAlloc(size, isBin)  malloc((size))
484 #   define TclpSysFree(ptr)    free((ptr))
485 #   define TclpSysRealloc(ptr, size)  realloc((ptr), (size))
486 #else
487 #   define TclpSysAlloc(size, isBin)  ((void*)HeapAlloc(GetProcessHeap(), \
488               (DWORD)0, (DWORD)size))
489 #   define TclpSysFree(ptr)    (HeapFree(GetProcessHeap(), \
490               (DWORD)0, (HGLOBAL)ptr))
491 #   define TclpSysRealloc(ptr, size)  ((void*)HeapReAlloc(GetProcessHeap(), \
492               (DWORD)0, (LPVOID)ptr, (DWORD)size))
493 #endif
494 
495 /*
496  * The following defines map from standard socket names to our internal
497  * wrappers that redirect through the winSock function table (see the
498  * file tclWinSock.c).
499  */
500 
501 #define getservbyname  TclWinGetServByName
502 #define getsockopt  TclWinGetSockOpt
503 #define ntohs    TclWinNToHS
504 #define setsockopt  TclWinSetSockOpt
505 /* This type is not defined in the Windows headers */
506 #define socklen_t       int
507 
508 
509 /*
510  * The following macros have trivial definitions, allowing generic code to
511  * address platform-specific issues.
512  */
513 
514 #define TclpReleaseFile(file)  ckfree((char *) file)
515 
516 /*
517  * The following macros and declarations wrap the C runtime library
518  * functions.
519  */
520 
521 #define TclpExit    exit
522 
523 #ifndef INVALID_SET_FILE_POINTER
524 #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
525 #endif /* INVALID_SET_FILE_POINTER */
526 
527 #endif /* _TCLWINPORT */
528