1/*
2    platform.h - compiler and OS detection
3    Copyright (C) 2016-present, Przemyslaw Skibinski, Yann Collet
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License along
16    with this program; if not, write to the Free Software Foundation, Inc.,
17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#ifndef PLATFORM_H_MODULE
21#define PLATFORM_H_MODULE
22
23#if defined (__cplusplus)
24extern "C" {
25#endif
26
27
28
29/* **************************************
30*  Compiler Options
31****************************************/
32#if defined(_MSC_VER)
33#  define _CRT_SECURE_NO_WARNINGS   /* Disable Visual Studio warning messages for fopen, strncpy, strerror */
34#  define _CRT_SECURE_NO_DEPRECATE  /* VS2005 - must be declared before <io.h> and <windows.h> */
35#  if (_MSC_VER <= 1800)            /* (1800 = Visual Studio 2013) */
36#    define snprintf sprintf_s      /* snprintf unsupported by Visual <= 2013 */
37#  endif
38#endif
39
40
41/* **************************************
42*  Detect 64-bit OS
43*  http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros
44****************************************/
45#if defined __ia64 || defined _M_IA64                                                                               /* Intel Itanium */ \
46  || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__                                                /* POWER 64-bit */  \
47  || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__  /* SPARC 64-bit */  \
48  || defined __x86_64__s || defined _M_X64                                                                          /* x86 64-bit */    \
49  || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__                                           /* ARM 64-bit */    \
50  || (defined __mips  && (__mips == 64 || __mips == 4 || __mips == 3))                                              /* MIPS 64-bit */   \
51  || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */               \
52  || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */
53#  if !defined(__64BIT__)
54#    define __64BIT__  1
55#  endif
56#endif
57
58
59/* *********************************************************
60*  Turn on Large Files support (>4GB) for 32-bit Linux/Unix
61***********************************************************/
62#if !defined(__64BIT__)                               /* No point defining Large file for 64 bit */
63#  if !defined(_FILE_OFFSET_BITS)
64#    define _FILE_OFFSET_BITS 64                      /* turn off_t into a 64-bit type for ftello, fseeko */
65#  endif
66#  if !defined(_LARGEFILE_SOURCE)                     /* obsolete macro, replaced with _FILE_OFFSET_BITS */
67#    define _LARGEFILE_SOURCE 1                       /* Large File Support extension (LFS) - fseeko, ftello */
68#  endif
69#  if defined(_AIX) || defined(__hpux)
70#    define _LARGE_FILES                              /* Large file support on 32-bits AIX and HP-UX */
71#  endif
72#endif
73
74
75/* ************************************************************
76*  Detect POSIX version
77*  PLATFORM_POSIX_VERSION = -1 for non-Unix e.g. Windows
78*  PLATFORM_POSIX_VERSION = 0 for Unix-like non-POSIX
79*  PLATFORM_POSIX_VERSION >= 1 is equal to found _POSIX_VERSION
80***************************************************************/
81#if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \
82   || defined(__midipix__) || defined(__VMS))
83#  if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1–2001 (SUSv3) conformant */ \
84     || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)  /* BSD distros */
85#    define PLATFORM_POSIX_VERSION 200112L
86#  else
87#    if defined(__linux__) || defined(__linux)
88#      define _POSIX_C_SOURCE 200112L  /* use feature test macro */
89#    endif
90#    include <unistd.h>  /* declares _POSIX_VERSION */
91#    if defined(_POSIX_VERSION)  /* POSIX compliant */
92#      define PLATFORM_POSIX_VERSION _POSIX_VERSION
93#    else
94#      define PLATFORM_POSIX_VERSION 0
95#    endif
96#  endif
97#endif
98#if !defined(PLATFORM_POSIX_VERSION)
99#  define PLATFORM_POSIX_VERSION -1
100#endif
101
102
103/*-*********************************************
104*  Detect if isatty() and fileno() are available
105************************************************/
106#if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__)
107#  include <unistd.h>   /* isatty */
108#  define IS_CONSOLE(stdStream) isatty(fileno(stdStream))
109#elif defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__)
110#  include <io.h>       /* _isatty */
111#  define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream))
112#else
113#  define IS_CONSOLE(stdStream) 0
114#endif
115
116
117/******************************
118*  OS-specific Includes
119******************************/
120#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32)
121#  include <fcntl.h>   /* _O_BINARY */
122#  include <io.h>      /* _setmode, _fileno, _get_osfhandle */
123#  if !defined(__DJGPP__)
124#    include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */
125#    include <winioctl.h> /* FSCTL_SET_SPARSE */
126#    define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; }
127#    define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); }
128#  else
129#    define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
130#    define SET_SPARSE_FILE_MODE(file)
131#  endif
132#else
133#  define SET_BINARY_MODE(file)
134#  define SET_SPARSE_FILE_MODE(file)
135#endif
136
137
138
139#if defined (__cplusplus)
140}
141#endif
142
143#endif /* PLATFORM_H_MODULE */
144