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) 24 extern "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 # if (_MSC_VER <= 1800) /* (1800 = Visual Studio 2013) */ 35 # define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */ 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__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */ 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__) || defined(__MidnightBSD__) /* BSD distros */ 85 # define PLATFORM_POSIX_VERSION 200112L 86 # else 87 # if defined(__linux__) || defined(__linux) 88 # ifndef _POSIX_C_SOURCE 89 # define _POSIX_C_SOURCE 200112L /* use feature test macro */ 90 # endif 91 # endif 92 # include <unistd.h> /* declares _POSIX_VERSION */ 93 # if defined(_POSIX_VERSION) /* POSIX compliant */ 94 # define PLATFORM_POSIX_VERSION _POSIX_VERSION 95 # else 96 # define PLATFORM_POSIX_VERSION 0 97 # endif 98 # endif 99 #endif 100 #if !defined(PLATFORM_POSIX_VERSION) 101 # define PLATFORM_POSIX_VERSION -1 102 #endif 103 104 105 /*-********************************************* 106 * Detect if isatty() and fileno() are available 107 ************************************************/ 108 #if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__) 109 # include <unistd.h> /* isatty */ 110 # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) 111 #elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__) 112 # include <io.h> /* _isatty */ 113 # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) 114 #elif defined(WIN32) || defined(_WIN32) 115 # include <io.h> /* _isatty */ 116 # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ 117 # include <stdio.h> /* FILE */ 118 static __inline int IS_CONSOLE(FILE* stdStream) 119 { 120 DWORD dummy; 121 return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy); 122 } 123 #else 124 # define IS_CONSOLE(stdStream) 0 125 #endif 126 127 128 /****************************** 129 * OS-specific Includes 130 ******************************/ 131 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) 132 # include <fcntl.h> /* _O_BINARY */ 133 # include <io.h> /* _setmode, _fileno, _get_osfhandle */ 134 # if !defined(__DJGPP__) 135 # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ 136 # include <winioctl.h> /* FSCTL_SET_SPARSE */ 137 # define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; } 138 # define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); } 139 # else 140 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) 141 # define SET_SPARSE_FILE_MODE(file) 142 # endif 143 #else 144 # define SET_BINARY_MODE(file) 145 # define SET_SPARSE_FILE_MODE(file) 146 #endif 147 148 149 150 #if defined (__cplusplus) 151 } 152 #endif 153 154 #endif /* PLATFORM_H_MODULE */ 155