1 /* Copyright (c) 2009, Google Inc. 2 * All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * --- 31 * Author: Craig Silverstein 32 * 33 * These are some portability typedefs and defines to make it a bit 34 * easier to compile this code under VC++. 35 * 36 * Several of these are taken from glib: 37 * http://developer.gnome.org/doc/API/glib/glib-windows-compatability-functions.html 38 */ 39 40 #ifndef GFLAGS_WINDOWS_PORT_H_ 41 #define GFLAGS_WINDOWS_PORT_H_ 42 43 #include "config.h" 44 45 // This must be defined before the windows.h is included. 46 // It's needed for mutex.h, to give access to the TryLock method. 47 # if !defined(_WIN32_WINNT) && !(defined( __MINGW32__) || defined(__MINGW64__)) 48 # define _WIN32_WINNT 0x0400 49 # endif 50 // We always want minimal includes 51 #ifndef WIN32_LEAN_AND_MEAN 52 # define WIN32_LEAN_AND_MEAN 53 #endif 54 #include <windows.h> 55 #include <direct.h> /* for mkdir */ 56 #include <stdlib.h> /* for _putenv, getenv */ 57 #include <stdio.h> /* need this to override stdio's snprintf, also defines _unlink used by unit tests */ 58 #include <stdarg.h> /* util.h uses va_copy */ 59 #include <string.h> /* for _stricmp and _strdup */ 60 61 /* We can't just use _vsnprintf and _snprintf as drop-in-replacements, 62 * because they don't always NUL-terminate. :-( We also can't use the 63 * name vsnprintf, since windows defines that (but not snprintf (!)). 64 */ 65 #if !defined(__MINGW32__) && !defined(__MINGW64__) /* mingw already defines */ 66 #if !(defined(_MSC_VER) && _MSC_VER >= 1900) /* msvc 2015 already defines */ 67 extern GFLAGS_DLL_DECL int snprintf(char *str, size_t size, 68 const char *format, ...); 69 extern int GFLAGS_DLL_DECL safe_vsnprintf(char *str, size_t size, 70 const char *format, va_list ap); 71 #define vsnprintf(str, size, format, ap) safe_vsnprintf(str, size, format, ap) 72 #define va_copy(dst, src) (dst) = (src) 73 #endif 74 #endif /* #if !defined(__MINGW32__) && !defined(__MINGW64__) */ 75 76 #ifdef _MSC_VER 77 # pragma warning(push) 78 # pragma warning(disable: 4996) // ignore getenv security warning 79 #endif 80 inline void setenv(const char* name, const char* value, int) { 81 // In windows, it's impossible to set a variable to the empty string. 82 // We handle this by setting it to "0" and the NUL-ing out the \0. 83 // That is, we putenv("FOO=0") and then find out where in memory the 84 // putenv wrote "FOO=0", and change it in-place to "FOO=\0". 85 // c.f. http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/environ.cpp?r1=611451&r2=637508&pathrev=637508 86 static const char* const kFakeZero = "0"; 87 if (*value == '\0') 88 value = kFakeZero; 89 // Apparently the semantics of putenv() is that the input 90 // must live forever, so we leak memory here. :-( 91 const size_t nameval_len = strlen(name) + 1 + strlen(value) + 1; 92 char* nameval = reinterpret_cast<char*>(malloc(nameval_len)); 93 snprintf(nameval, nameval_len, "%s=%s", name, value); 94 _putenv(nameval); 95 if (value == kFakeZero) { 96 nameval[nameval_len - 2] = '\0'; // works when putenv() makes no copy 97 if (*getenv(name) != '\0') 98 *getenv(name) = '\0'; // works when putenv() copies nameval 99 } 100 } 101 #ifdef _MSC_VER 102 # pragma warning(pop) 103 #endif 104 105 #define strcasecmp _stricmp 106 107 #if defined(_MSC_VER) && _MSC_VER >= 1400 108 #define strdup _strdup 109 #define unlink _unlink 110 #endif 111 112 #if defined(_MSC_VER) && _MSC_VER >= 1800 113 #include <inttypes.h> 114 #else 115 #define PRId32 "d" 116 #define PRIu32 "u" 117 #define PRId64 "I64d" 118 #define PRIu64 "I64u" 119 #endif 120 121 #if !defined(__MINGW32__) && !defined(__MINGW64__) 122 #define strtoq _strtoi64 123 #define strtouq _strtoui64 124 #define strtoll _strtoi64 125 #define strtoull _strtoui64 126 #define atoll _atoi64 127 #endif 128 129 #ifndef PATH_MAX 130 #define PATH_MAX 1024 131 #endif 132 133 #endif /* GFLAGS_WINDOWS_PORT_H_ */ 134