Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Yann Collet | 232d62b | 2017-08-21 11:24:32 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 8 | */ |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 9 | |
| 10 | #ifndef PLATFORM_H_MODULE |
| 11 | #define PLATFORM_H_MODULE |
| 12 | |
| 13 | #if defined (__cplusplus) |
| 14 | extern "C" { |
| 15 | #endif |
| 16 | |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 17 | |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 18 | |
| 19 | /* ************************************** |
| 20 | * Compiler Options |
| 21 | ****************************************/ |
| 22 | #if defined(_MSC_VER) |
| 23 | # define _CRT_SECURE_NO_WARNINGS /* Disable Visual Studio warning messages for fopen, strncpy, strerror */ |
Nick Terrell | a0f9006 | 2017-02-08 17:25:01 -0800 | [diff] [blame] | 24 | # define _CRT_SECURE_NO_DEPRECATE /* VS2005 - must be declared before <io.h> and <windows.h> */ |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 25 | # if (_MSC_VER <= 1800) /* (1800 = Visual Studio 2013) */ |
| 26 | # define snprintf sprintf_s /* snprintf unsupported by Visual <= 2013 */ |
| 27 | # endif |
| 28 | #endif |
| 29 | |
| 30 | |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 31 | /* ************************************** |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 32 | * Detect 64-bit OS |
| 33 | * http://nadeausoftware.com/articles/2012/02/c_c_tip_how_detect_processor_type_using_compiler_predefined_macros |
Przemyslaw Skibinski | b866e72 | 2016-12-16 14:24:01 +0100 | [diff] [blame] | 34 | ****************************************/ |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 35 | #if defined __ia64 || defined _M_IA64 /* Intel Itanium */ \ |
| 36 | || defined __powerpc64__ || defined __ppc64__ || defined __PPC64__ /* POWER 64-bit */ \ |
| 37 | || (defined __sparc && (defined __sparcv9 || defined __sparc_v9__ || defined __arch64__)) || defined __sparc64__ /* SPARC 64-bit */ \ |
| 38 | || defined __x86_64__s || defined _M_X64 /* x86 64-bit */ \ |
| 39 | || defined __arm64__ || defined __aarch64__ || defined __ARM64_ARCH_8__ /* ARM 64-bit */ \ |
| 40 | || (defined __mips && (__mips == 64 || __mips == 4 || __mips == 3)) /* MIPS 64-bit */ \ |
| 41 | || defined _LP64 || defined __LP64__ /* NetBSD, OpenBSD */ || defined __64BIT__ /* AIX */ || defined _ADDR64 /* Cray */ \ |
| 42 | || (defined __SIZEOF_POINTER__ && __SIZEOF_POINTER__ == 8) /* gcc */ |
| 43 | # if !defined(__64BIT__) |
| 44 | # define __64BIT__ 1 |
| 45 | # endif |
| 46 | #endif |
| 47 | |
| 48 | |
| 49 | /* ********************************************************* |
| 50 | * Turn on Large Files support (>4GB) for 32-bit Linux/Unix |
| 51 | ***********************************************************/ |
Przemyslaw Skibinski | 35bf23c | 2017-02-13 13:57:29 +0100 | [diff] [blame] | 52 | #if !defined(__64BIT__) || defined(__MINGW32__) /* No point defining Large file for 64 bit but MinGW-w64 requires it */ |
| 53 | # if !defined(_FILE_OFFSET_BITS) |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 54 | # define _FILE_OFFSET_BITS 64 /* turn off_t into a 64-bit type for ftello, fseeko */ |
| 55 | # endif |
| 56 | # if !defined(_LARGEFILE_SOURCE) /* obsolete macro, replaced with _FILE_OFFSET_BITS */ |
| 57 | # define _LARGEFILE_SOURCE 1 /* Large File Support extension (LFS) - fseeko, ftello */ |
| 58 | # endif |
| 59 | # if defined(_AIX) || defined(__hpux) |
| 60 | # define _LARGE_FILES /* Large file support on 32-bits AIX and HP-UX */ |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 61 | # endif |
| 62 | #endif |
| 63 | |
| 64 | |
Przemyslaw Skibinski | 3cdfe26 | 2016-12-16 15:00:50 +0100 | [diff] [blame] | 65 | /* ************************************************************ |
Przemyslaw Skibinski | b866e72 | 2016-12-16 14:24:01 +0100 | [diff] [blame] | 66 | * Detect POSIX version |
Przemyslaw Skibinski | 3cdfe26 | 2016-12-16 15:00:50 +0100 | [diff] [blame] | 67 | * PLATFORM_POSIX_VERSION = -1 for non-Unix e.g. Windows |
| 68 | * PLATFORM_POSIX_VERSION = 0 for Unix-like non-POSIX |
| 69 | * PLATFORM_POSIX_VERSION >= 1 is equal to found _POSIX_VERSION |
| 70 | ***************************************************************/ |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 71 | #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) /* UNIX-like OS */ \ |
| 72 | || defined(__midipix__) || defined(__VMS)) |
| 73 | # if (defined(__APPLE__) && defined(__MACH__)) || defined(__SVR4) || defined(_AIX) || defined(__hpux) /* POSIX.1–2001 (SUSv3) conformant */ \ |
| 74 | || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) /* BSD distros */ |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 75 | # define PLATFORM_POSIX_VERSION 200112L |
| 76 | # else |
| 77 | # if defined(__linux__) || defined(__linux) |
Nick Terrell | d080198 | 2017-02-09 14:20:52 -0800 | [diff] [blame] | 78 | # ifndef _POSIX_C_SOURCE |
| 79 | # define _POSIX_C_SOURCE 200112L /* use feature test macro */ |
Nick Terrell | a0f9006 | 2017-02-08 17:25:01 -0800 | [diff] [blame] | 80 | # endif |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 81 | # endif |
| 82 | # include <unistd.h> /* declares _POSIX_VERSION */ |
| 83 | # if defined(_POSIX_VERSION) /* POSIX compliant */ |
| 84 | # define PLATFORM_POSIX_VERSION _POSIX_VERSION |
Przemyslaw Skibinski | b866e72 | 2016-12-16 14:24:01 +0100 | [diff] [blame] | 85 | # else |
Przemyslaw Skibinski | 3cdfe26 | 2016-12-16 15:00:50 +0100 | [diff] [blame] | 86 | # define PLATFORM_POSIX_VERSION 0 |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 87 | # endif |
| 88 | # endif |
| 89 | #endif |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 90 | #if !defined(PLATFORM_POSIX_VERSION) |
Przemyslaw Skibinski | 3cdfe26 | 2016-12-16 15:00:50 +0100 | [diff] [blame] | 91 | # define PLATFORM_POSIX_VERSION -1 |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 92 | #endif |
| 93 | |
| 94 | |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 95 | /*-********************************************* |
| 96 | * Detect if isatty() and fileno() are available |
| 97 | ************************************************/ |
| 98 | #if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 1)) || (PLATFORM_POSIX_VERSION >= 200112L) || defined(__DJGPP__) |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 99 | # include <unistd.h> /* isatty */ |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 100 | # define IS_CONSOLE(stdStream) isatty(fileno(stdStream)) |
Sean Purcell | 894bf49 | 2017-03-27 12:19:30 -0700 | [diff] [blame] | 101 | #elif defined(MSDOS) || defined(OS2) || defined(__CYGWIN__) |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 102 | # include <io.h> /* _isatty */ |
| 103 | # define IS_CONSOLE(stdStream) _isatty(_fileno(stdStream)) |
Sean Purcell | 894bf49 | 2017-03-27 12:19:30 -0700 | [diff] [blame] | 104 | #elif defined(WIN32) || defined(_WIN32) |
| 105 | # include <io.h> /* _isatty */ |
| 106 | # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ |
| 107 | # include <stdio.h> /* FILE */ |
Sean Purcell | efdaf8b | 2017-03-27 12:26:40 -0700 | [diff] [blame] | 108 | static __inline int IS_CONSOLE(FILE* stdStream) |
Sean Purcell | 894bf49 | 2017-03-27 12:19:30 -0700 | [diff] [blame] | 109 | { |
| 110 | DWORD dummy; |
| 111 | return _isatty(_fileno(stdStream)) && GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stdStream)), &dummy); |
| 112 | } |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 113 | #else |
| 114 | # define IS_CONSOLE(stdStream) 0 |
| 115 | #endif |
| 116 | |
| 117 | |
| 118 | /****************************** |
| 119 | * OS-specific Includes |
| 120 | ******************************/ |
| 121 | #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(_WIN32) |
| 122 | # include <fcntl.h> /* _O_BINARY */ |
| 123 | # include <io.h> /* _setmode, _fileno, _get_osfhandle */ |
| 124 | # if !defined(__DJGPP__) |
| 125 | # include <windows.h> /* DeviceIoControl, HANDLE, FSCTL_SET_SPARSE */ |
| 126 | # include <winioctl.h> /* FSCTL_SET_SPARSE */ |
| 127 | # define SET_BINARY_MODE(file) { int unused=_setmode(_fileno(file), _O_BINARY); (void)unused; } |
| 128 | # define SET_SPARSE_FILE_MODE(file) { DWORD dw; DeviceIoControl((HANDLE) _get_osfhandle(_fileno(file)), FSCTL_SET_SPARSE, 0, 0, 0, 0, &dw, 0); } |
| 129 | # else |
| 130 | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) |
| 131 | # define SET_SPARSE_FILE_MODE(file) |
| 132 | # endif |
| 133 | #else |
| 134 | # define SET_BINARY_MODE(file) |
| 135 | # define SET_SPARSE_FILE_MODE(file) |
| 136 | #endif |
| 137 | |
| 138 | |
Nick Terrell | 96fe545 | 2017-03-31 15:16:43 -0700 | [diff] [blame] | 139 | #ifndef ZSTD_SPARSE_DEFAULT |
| 140 | # if (defined(__APPLE__) && defined(__MACH__)) |
| 141 | # define ZSTD_SPARSE_DEFAULT 0 |
| 142 | # else |
| 143 | # define ZSTD_SPARSE_DEFAULT 1 |
| 144 | # endif |
| 145 | #endif |
| 146 | |
Przemyslaw Skibinski | b866e72 | 2016-12-16 14:24:01 +0100 | [diff] [blame] | 147 | |
Przemyslaw Skibinski | b3843af | 2016-12-16 14:13:15 +0100 | [diff] [blame] | 148 | #if defined (__cplusplus) |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | #endif /* PLATFORM_H_MODULE */ |