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 #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L
setenv(const char * name,const char * value,int)81 inline void setenv(const char* name, const char* value, int) {
82 // In windows, it's impossible to set a variable to the empty string.
83 // We handle this by setting it to "0" and the NUL-ing out the \0.
84 // That is, we putenv("FOO=0") and then find out where in memory the
85 // putenv wrote "FOO=0", and change it in-place to "FOO=\0".
86 // c.f. http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/environ.cpp?r1=611451&r2=637508&pathrev=637508
87 static const char* const kFakeZero = "0";
88 if (*value == '\0')
89 value = kFakeZero;
90 // Apparently the semantics of putenv() is that the input
91 // must live forever, so we leak memory here. :-(
92 const size_t nameval_len = strlen(name) + 1 + strlen(value) + 1;
93 char* nameval = reinterpret_cast<char*>(malloc(nameval_len));
94 snprintf(nameval, nameval_len, "%s=%s", name, value);
95 _putenv(nameval);
96 if (value == kFakeZero) {
97 nameval[nameval_len - 2] = '\0'; // works when putenv() makes no copy
98 if (*getenv(name) != '\0')
99 *getenv(name) = '\0'; // works when putenv() copies nameval
100 }
101 }
102 #endif
103 #ifdef _MSC_VER
104 # pragma warning(pop)
105 #endif
106
107 #define strcasecmp _stricmp
108
109 #if defined(_MSC_VER) && _MSC_VER >= 1400
110 #define strdup _strdup
111 #define unlink _unlink
112 #endif
113
114 #if defined(_MSC_VER) && _MSC_VER >= 1800
115 #include <inttypes.h>
116 #else
117 #define PRId32 "d"
118 #define PRIu32 "u"
119 #define PRId64 "I64d"
120 #define PRIu64 "I64u"
121 #endif
122
123 #if !defined(__MINGW32__) && !defined(__MINGW64__)
124 #define strtoq _strtoi64
125 #define strtouq _strtoui64
126 #define strtoll _strtoi64
127 #define strtoull _strtoui64
128 #define atoll _atoi64
129 #endif
130
131 #ifndef PATH_MAX
132 #define PATH_MAX 1024
133 #endif
134
135 #endif /* GFLAGS_WINDOWS_PORT_H_ */
136