4 * This header file handles porting issues that occur because of
5 * differences between systems. It reads in UNIX-related header files and
6 * sets up UNIX-related macros for Tcl's UNIX core. It should be the only
7 * file that contains #ifdefs to handle different flavors of UNIX. This
8 * file sets up the union of all UNIX-related things needed by any of the
9 * Tcl core files. This file depends on configuration #defines such as
10 * NO_DIRENT_H that are set up by the "configure" script.
12 * Much of the material in this file was originally contributed by Karl
13 * Lehenbauer, Mark Diekhans and Peter da Silva.
15 * Copyright (c) 1991-1994 The Regents of the University of California.
16 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
18 * See the file "license.terms" for information on usage and redistribution of
19 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
26 *---------------------------------------------------------------------------
27 * The following sets of #includes and #ifdefs are required to get Tcl to
28 * compile under the various flavors of unix.
29 *---------------------------------------------------------------------------
34 #ifdef HAVE_NET_ERRNO_H
35 # include <net/errno.h>
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
42 #include <sys/types.h>
44 # include "../compat/dirent2.h"
47 # include "../compat/dirent.h"
54 *---------------------------------------------------------------------------
55 * Parameterize for 64-bit filesystem support.
56 *---------------------------------------------------------------------------
59 #ifdef HAVE_STRUCT_DIRENT64
60 typedef struct dirent64 Tcl_DirEntry;
61 # define TclOSreaddir readdir64
63 typedef struct dirent Tcl_DirEntry;
64 # define TclOSreaddir readdir
68 # define TclOSopendir opendir64
69 # define TclOSrewinddir rewinddir64
70 # define TclOSclosedir closedir64
73 # define TclOSopendir opendir
74 # define TclOSrewinddir rewinddir
75 # define TclOSclosedir closedir
78 #ifdef HAVE_TYPE_OFF64_T
79 typedef off64_t Tcl_SeekOffset;
80 # define TclOSseek lseek64
81 # define TclOSopen open64
83 typedef off_t Tcl_SeekOffset;
84 # define TclOSseek lseek
85 # define TclOSopen open
92 /* Make some symbols available without including <windows.h> */
93 # define DWORD unsigned int
94 # define CP_UTF8 65001
95 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 0x00000004
96 # define HANDLE void *
97 # define HINSTANCE void *
98 # define SOCKET unsigned int
99 # define WSAEWOULDBLOCK 10035
100 typedef unsigned short WCHAR;
102 #pragma clang diagnostic push
103 #pragma clang diagnostic ignored "-Wignored-attributes"
105 __declspec(dllimport) extern __stdcall int GetModuleHandleExW(unsigned int, const void *, void *);
106 __declspec(dllimport) extern __stdcall int GetModuleFileNameW(void *, const void *, int);
107 __declspec(dllimport) extern __stdcall int WideCharToMultiByte(int, int, const void *, int,
108 char *, int, const char *, void *);
109 __declspec(dllimport) extern __stdcall int MultiByteToWideChar(int, int, const char *, int,
111 __declspec(dllimport) extern __stdcall void OutputDebugStringW(const WCHAR *);
112 __declspec(dllimport) extern __stdcall int IsDebuggerPresent(void);
113 __declspec(dllimport) extern __stdcall int GetLastError(void);
114 __declspec(dllimport) extern __stdcall int GetFileAttributesW(const WCHAR *);
115 __declspec(dllimport) extern __stdcall int SetFileAttributesW(const WCHAR *, int);
116 __declspec(dllimport) extern int cygwin_conv_path(int, const void *, void *, int);
118 #pragma clang diagnostic pop
120 # define timezone _timezone
121 extern int TclOSstat(const char *name, void *statBuf);
122 extern int TclOSlstat(const char *name, void *statBuf);
126 #elif defined(HAVE_STRUCT_STAT64) && !defined(__APPLE__)
127 # define TclOSstat(name, buf) stat64(name, (struct stat64 *)buf)
128 # define TclOSlstat(name,buf) lstat64(name, (struct stat64 *)buf)
130 # define TclOSstat(name, buf) stat(name, (struct stat *)buf)
131 # define TclOSlstat(name, buf) lstat(name, (struct stat *)buf)
135 *---------------------------------------------------------------------------
136 * Miscellaneous includes that might be missing.
137 *---------------------------------------------------------------------------
140 #include <sys/file.h>
141 #ifdef HAVE_SYS_SELECT_H
142 # include <sys/select.h>
144 #include <sys/stat.h>
145 #ifdef TIME_WITH_SYS_TIME
146 # include <sys/time.h>
149 #ifdef HAVE_SYS_TIME_H
150 # include <sys/time.h>
155 #ifndef NO_SYS_WAIT_H
156 # include <sys/wait.h>
158 #ifdef HAVE_INTTYPES_H
159 # include <inttypes.h>
168 # include "../compat/unistd.h"
171 extern int TclUnixSetBlockingMode(int fd, int mode);
176 *---------------------------------------------------------------------------
177 * Socket support stuff: This likely needs more work to parameterize for each
179 *---------------------------------------------------------------------------
182 #include <sys/socket.h> /* struct sockaddr, SOCK_STREAM, ... */
184 # include <sys/utsname.h> /* uname system call. */
186 #include <netinet/in.h> /* struct in_addr, struct sockaddr_in */
187 #include <arpa/inet.h> /* inet_ntoa() */
188 #include <netdb.h> /* getaddrinfo() */
189 #ifdef NEED_FAKE_RFC2553
190 # include "../compat/fake-rfc2553.h"
194 *---------------------------------------------------------------------------
195 * Some platforms (e.g. SunOS) don't define FLT_MAX and FLT_MIN, so we look
196 * for an alternative definition. If no other alternative is available we use
197 * a reasonable guess.
198 *---------------------------------------------------------------------------
211 # define FLT_MAX MAXFLOAT
213 # define FLT_MAX 3.402823466E+38F
218 # define FLT_MIN MINFLOAT
220 # define FLT_MIN 1.175494351E-38F
225 *---------------------------------------------------------------------------
226 * NeXT doesn't define O_NONBLOCK, so #define it here if necessary.
227 *---------------------------------------------------------------------------
231 # define O_NONBLOCK 0x80
235 *---------------------------------------------------------------------------
236 * The type of the status returned by wait varies from UNIX system to UNIX
237 * system. The macro below defines it:
238 *---------------------------------------------------------------------------
242 # define WAIT_STATUS_TYPE pid_t
244 #ifndef NO_UNION_WAIT
245 # define WAIT_STATUS_TYPE union wait
247 # define WAIT_STATUS_TYPE int
252 *---------------------------------------------------------------------------
253 * Supply definitions for macros to query wait status, if not already defined
254 * in header files above.
255 *---------------------------------------------------------------------------
259 # define WIFEXITED(stat) (((*((int *) &(stat))) & 0xFF) == 0)
263 # define WEXITSTATUS(stat) (((*((int *) &(stat))) >> 8) & 0xFF)
267 # define WIFSIGNALED(stat) \
268 (((*((int *) &(stat)))) && ((*((int *) &(stat))) \
269 == ((*((int *) &(stat))) & 0x00FF)))
273 # define WTERMSIG(stat) ((*((int *) &(stat))) & 0x7F)
277 # define WIFSTOPPED(stat) (((*((int *) &(stat))) & 0xFF) == 0177)
281 # define WSTOPSIG(stat) (((*((int *) &(stat))) >> 8) & 0xFF)
285 *---------------------------------------------------------------------------
286 * Define constants for waitpid() system call if they aren't defined by a
287 * system header file.
288 *---------------------------------------------------------------------------
299 *---------------------------------------------------------------------------
300 * Supply macros for seek offsets, if they're not already provided by an
302 *---------------------------------------------------------------------------
316 *---------------------------------------------------------------------------
317 * The stuff below is needed by the "time" command. If this system has no
318 * gettimeofday call, then must use times() instead.
319 *---------------------------------------------------------------------------
323 # include <sys/times.h>
325 # ifdef HAVE_BSDGETTIMEOFDAY
326 # define gettimeofday BSDgettimeofday
330 #ifdef GETTOD_NOT_DECLARED
331 extern int gettimeofday(struct timeval *tp,
332 struct timezone *tzp);
336 *---------------------------------------------------------------------------
337 * Define access mode constants if they aren't already defined.
338 *---------------------------------------------------------------------------
355 *---------------------------------------------------------------------------
356 * Define FD_CLOEEXEC (the close-on-exec flag bit) if it isn't already
358 *---------------------------------------------------------------------------
362 # define FD_CLOEXEC 1
366 *---------------------------------------------------------------------------
367 * On systems without symbolic links (i.e. S_IFLNK isn't defined) define
368 * "lstat" to use "stat" instead.
369 *---------------------------------------------------------------------------
375 # define lstat64 stat64
376 # define TclOSlstat TclOSstat
380 *---------------------------------------------------------------------------
381 * Define macros to query file type bits, if they're not already defined.
382 *---------------------------------------------------------------------------
387 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
389 # define S_ISREG(m) 0
391 #endif /* !S_ISREG */
394 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
396 # define S_ISDIR(m) 0
398 #endif /* !S_ISDIR */
401 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
403 # define S_ISCHR(m) 0
405 #endif /* !S_ISCHR */
409 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
411 # define S_ISBLK(m) 0
413 #endif /* !S_ISBLK */
417 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
419 # define S_ISFIFO(m) 0
421 #endif /* !S_ISFIFO */
425 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
427 # define S_ISLNK(m) 0
429 #endif /* !S_ISLNK */
433 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
435 # define S_ISSOCK(m) 0
437 #endif /* !S_ISSOCK */
440 *---------------------------------------------------------------------------
441 * Make sure that MAXPATHLEN and MAXNAMLEN are defined.
442 *---------------------------------------------------------------------------
447 # define MAXPATHLEN PATH_MAX
449 # define MAXPATHLEN 2048
455 # define MAXNAMLEN NAME_MAX
457 # define MAXNAMLEN 255
462 *---------------------------------------------------------------------------
463 * The following macro defines the type of the mask arguments to select:
464 *---------------------------------------------------------------------------
468 # define SELECT_MASK fd_set
469 #else /* NO_FD_SET */
471 typedef long fd_mask;
474 # define SELECT_MASK void
475 # else /* !defined(_IBMR2) */
476 # define SELECT_MASK int
477 # endif /* defined(_IBMR2) */
478 #endif /* !NO_FD_SET */
481 *---------------------------------------------------------------------------
482 * Define "NBBY" (number of bits per byte) if it's not already defined.
483 *---------------------------------------------------------------------------
491 *---------------------------------------------------------------------------
492 * The following macro defines the number of fd_masks in an fd_set:
493 *---------------------------------------------------------------------------
498 # define FD_SETSIZE OPEN_MAX
500 # define FD_SETSIZE 256
502 #endif /* FD_SETSIZE */
505 # define howmany(x, y) (((x)+((y)-1))/(y))
506 #endif /* !defined(howmany) */
509 # define NFDBITS NBBY*sizeof(fd_mask)
512 #define MASK_SIZE howmany(FD_SETSIZE, NFDBITS)
515 *---------------------------------------------------------------------------
516 * Not all systems declare the errno variable in errno.h. so this file does it
517 * explicitly. The list of system error messages also isn't generally declared
518 * in a header file anywhere.
519 *---------------------------------------------------------------------------
524 #endif /* NO_ERRNO */
527 *---------------------------------------------------------------------------
528 * Not all systems declare all the errors that Tcl uses! Provide some
530 *---------------------------------------------------------------------------
535 # define EOVERFLOW EFBIG
537 # define EOVERFLOW EINVAL
539 #endif /* EOVERFLOW */
542 *---------------------------------------------------------------------------
543 * Variables provided by the C library:
544 *---------------------------------------------------------------------------
547 #if defined(__APPLE__) && defined(__DYNAMIC__)
548 # include <crt_externs.h>
549 # define environ (*_NSGetEnviron())
550 # define USE_PUTENV 1
552 # if defined(_sgi) || defined(__sgi)
553 # define environ _environ
555 extern char ** environ;
559 *---------------------------------------------------------------------------
560 * Darwin specifc configure overrides.
561 *---------------------------------------------------------------------------
567 *---------------------------------------------------------------------------
568 * Support for fat compiles: configure runs only once for multiple architectures
569 *---------------------------------------------------------------------------
572 # if defined(__LP64__) && defined (NO_COREFOUNDATION_64)
573 # undef HAVE_COREFOUNDATION
574 # endif /* __LP64__ && NO_COREFOUNDATION_64 */
575 # include <sys/cdefs.h>
576 # ifdef __DARWIN_UNIX03
578 # undef HAVE_PUTENV_THAT_COPIES
580 # define HAVE_PUTENV_THAT_COPIES 1
582 # endif /* __DARWIN_UNIX03 */
585 *---------------------------------------------------------------------------
586 * Include AvailabilityMacros.h here (when available) to ensure any symbolic
587 * MAC_OS_X_VERSION_* constants passed on the command line are translated.
588 *---------------------------------------------------------------------------
591 # ifdef HAVE_AVAILABILITYMACROS_H
592 # include <AvailabilityMacros.h>
596 *---------------------------------------------------------------------------
597 * Support for weak import.
598 *---------------------------------------------------------------------------
601 # ifdef HAVE_WEAK_IMPORT
602 # if !defined(HAVE_AVAILABILITYMACROS_H) || !defined(MAC_OS_X_VERSION_MIN_REQUIRED)
603 # undef HAVE_WEAK_IMPORT
605 # ifndef WEAK_IMPORT_ATTRIBUTE
606 # define WEAK_IMPORT_ATTRIBUTE __attribute__((weak_import))
609 # endif /* HAVE_WEAK_IMPORT */
612 *---------------------------------------------------------------------------
613 * Support for MAC_OS_X_VERSION_MAX_ALLOWED define from AvailabilityMacros.h:
614 * only use API available in the indicated OS version or earlier.
615 *---------------------------------------------------------------------------
618 # ifdef MAC_OS_X_VERSION_MAX_ALLOWED
619 # if MAC_OS_X_VERSION_MAX_ALLOWED < 1050 && defined(__LP64__)
620 # undef HAVE_COREFOUNDATION
622 # if MAC_OS_X_VERSION_MAX_ALLOWED < 1040
623 # undef HAVE_OSSPINLOCKLOCK
624 # undef HAVE_PTHREAD_ATFORK
625 # undef HAVE_COPYFILE
627 # if MAC_OS_X_VERSION_MAX_ALLOWED < 1030
629 /* prior to 10.3, realpath is not threadsafe, c.f. bug 711232 */
630 # define NO_REALPATH 1
632 # undef HAVE_LANGINFO
634 # endif /* MAC_OS_X_VERSION_MAX_ALLOWED */
635 # if defined(HAVE_COREFOUNDATION) && defined(__LP64__) && \
636 defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1050
637 # warning "Weak import of 64-bit CoreFoundation is not supported, will not run on Mac OS X < 10.5."
641 *---------------------------------------------------------------------------
642 * At present, using vfork() instead of fork() causes execve() to fail
643 * intermittently on Darwin x86_64. rdar://4685553
644 *---------------------------------------------------------------------------
647 # if defined(__x86_64__) && !defined(FIXED_RDAR_4685553)
649 # endif /* __x86_64__ */
650 /* Workaround problems with vfork() when building with llvm-gcc-4.2 */
651 # if defined (__llvm__) && \
652 (__GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 2 || \
653 (__GNUC_MINOR__ == 2 && __GNUC_PATCHLEVEL__ > 0))))
655 # endif /* __llvm__ */
656 #endif /* __APPLE__ */
659 *---------------------------------------------------------------------------
660 * The following macros and declarations represent the interface between
661 * generic and unix-specific parts of Tcl. Some of the macros may override
662 * functions declared in tclInt.h.
663 *---------------------------------------------------------------------------
667 * The default platform eol translation on Unix is TCL_TRANSLATE_LF.
671 #define TCL_PLATFORM_TRANSLATION TCL_TRANSLATE_CRLF
672 typedef int socklen_t;
674 #define TCL_PLATFORM_TRANSLATION TCL_TRANSLATE_LF
678 *---------------------------------------------------------------------------
679 * The following macros have trivial definitions, allowing generic code to
680 * address platform-specific issues.
681 *---------------------------------------------------------------------------
684 #define TclpReleaseFile(file) /* Nothing. */
687 *---------------------------------------------------------------------------
688 * The following defines wrap the system memory allocation routines.
689 *---------------------------------------------------------------------------
692 #define TclpSysAlloc(size, isBin) malloc((size_t)(size))
693 #define TclpSysFree(ptr) free((char *)(ptr))
694 #define TclpSysRealloc(ptr, size) realloc((char *)(ptr), (size_t)(size))
697 *---------------------------------------------------------------------------
698 * The following macros and declaration wrap the C runtime library functions.
699 *---------------------------------------------------------------------------
702 #define TclpExit exit
705 # include <pthread.h>
706 #endif /* TCL_THREADS */
708 /* FIXME - Hyper-enormous platform assumption! */
714 *---------------------------------------------------------------------------
715 * Set of MT-safe implementations of some known-to-be-MT-unsafe library calls.
716 * Instead of returning pointers to the static storage, those return pointers
718 *---------------------------------------------------------------------------
724 extern struct passwd * TclpGetPwNam(const char *name);
725 extern struct group * TclpGetGrNam(const char *name);
726 extern struct passwd * TclpGetPwUid(uid_t uid);
727 extern struct group * TclpGetGrGid(gid_t gid);
728 extern struct hostent * TclpGetHostByName(const char *name);
729 extern struct hostent * TclpGetHostByAddr(const char *addr,
730 int length, int type);
731 extern void *TclpMakeTcpClientChannelMode(
732 void *tcpSocket, int mode);
734 #endif /* _TCLUNIXPORT */