Austin Schuh | 1eb16d1 | 2015-09-06 17:21:56 -0700 | [diff] [blame^] | 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 | extern GFLAGS_DLL_DECL int snprintf(char *str, size_t size, |
| 67 | const char *format, ...); |
| 68 | extern int GFLAGS_DLL_DECL safe_vsnprintf(char *str, size_t size, |
| 69 | const char *format, va_list ap); |
| 70 | #define vsnprintf(str, size, format, ap) safe_vsnprintf(str, size, format, ap) |
| 71 | #define va_copy(dst, src) (dst) = (src) |
| 72 | #endif /* #if !defined(__MINGW32__) && !defined(__MINGW64__) */ |
| 73 | |
| 74 | #ifdef _MSC_VER |
| 75 | # pragma warning(push) |
| 76 | # pragma warning(disable: 4996) // ignore getenv security warning |
| 77 | #endif |
| 78 | inline void setenv(const char* name, const char* value, int) { |
| 79 | // In windows, it's impossible to set a variable to the empty string. |
| 80 | // We handle this by setting it to "0" and the NUL-ing out the \0. |
| 81 | // That is, we putenv("FOO=0") and then find out where in memory the |
| 82 | // putenv wrote "FOO=0", and change it in-place to "FOO=\0". |
| 83 | // c.f. http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/environ.cpp?r1=611451&r2=637508&pathrev=637508 |
| 84 | static const char* const kFakeZero = "0"; |
| 85 | if (*value == '\0') |
| 86 | value = kFakeZero; |
| 87 | // Apparently the semantics of putenv() is that the input |
| 88 | // must live forever, so we leak memory here. :-( |
| 89 | const size_t nameval_len = strlen(name) + 1 + strlen(value) + 1; |
| 90 | char* nameval = reinterpret_cast<char*>(malloc(nameval_len)); |
| 91 | snprintf(nameval, nameval_len, "%s=%s", name, value); |
| 92 | _putenv(nameval); |
| 93 | if (value == kFakeZero) { |
| 94 | nameval[nameval_len - 2] = '\0'; // works when putenv() makes no copy |
| 95 | if (*getenv(name) != '\0') |
| 96 | *getenv(name) = '\0'; // works when putenv() copies nameval |
| 97 | } |
| 98 | } |
| 99 | #ifdef _MSC_VER |
| 100 | # pragma warning(pop) |
| 101 | #endif |
| 102 | |
| 103 | #define strcasecmp _stricmp |
| 104 | |
| 105 | #if defined(_MSC_VER) && _MSC_VER >= 1400 |
| 106 | #define strdup _strdup |
| 107 | #define unlink _unlink |
| 108 | #endif |
| 109 | |
| 110 | #define PRId32 "d" |
| 111 | #define PRIu32 "u" |
| 112 | #define PRId64 "I64d" |
| 113 | #define PRIu64 "I64u" |
| 114 | |
| 115 | #if !defined(__MINGW32__) && !defined(__MINGW64__) |
| 116 | #define strtoq _strtoi64 |
| 117 | #define strtouq _strtoui64 |
| 118 | #define strtoll _strtoi64 |
| 119 | #define strtoull _strtoui64 |
| 120 | #define atoll _atoi64 |
| 121 | #endif |
| 122 | |
| 123 | #ifndef PATH_MAX |
| 124 | #define PATH_MAX 1024 |
| 125 | #endif |
| 126 | |
| 127 | #endif /* GFLAGS_WINDOWS_PORT_H_ */ |