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 GOOGLE_GFLAGS_WINDOWS_PORT_H_
41 #define GOOGLE_GFLAGS_WINDOWS_PORT_H_
42 
43 #ifdef _WIN32
44 
45 #ifndef WIN32_LEAN_AND_MEAN
46 #define WIN32_LEAN_AND_MEAN  /* We always want minimal includes */
47 #endif
48 #include <windows.h>
49 #include <direct.h>          /* for mkdir */
50 #include <stdlib.h>          /* for _putenv, getenv */
51 #include <stdio.h>           /* need this to override stdio's snprintf, also defines _unlink used by unit tests */
52 #include <stdarg.h>          /* util.h uses va_copy */
53 #include <string.h>          /* for _stricmp and _strdup */
54 
55 /* We can't just use _vsnprintf and _snprintf as drop-in-replacements,
56  * because they don't always NUL-terminate. :-(  We also can't use the
57  * name vsnprintf, since windows defines that (but not snprintf (!)).
58  */
59 #if !defined(__MINGW32__) && !defined(__MINGW64__)  /* mingw already defines */
60 extern GFLAGS_DLL_DECL int snprintf(char *str, size_t size,
61                                        const char *format, ...);
62 extern int GFLAGS_DLL_DECL safe_vsnprintf(char *str, size_t size,
63                                              const char *format, va_list ap);
64 #define vsnprintf(str, size, format, ap)  safe_vsnprintf(str, size, format, ap)
65 #define va_copy(dst, src)  (dst) = (src)
66 #endif  /* #if !defined(__MINGW32__) && !defined(__MINGW64__) */
67 
setenv(const char * name,const char * value,int)68 inline void setenv(const char* name, const char* value, int) {
69   // In windows, it's impossible to set a variable to the empty string.
70   // We handle this by setting it to "0" and the NUL-ing out the \0.
71   // That is, we putenv("FOO=0") and then find out where in memory the
72   // putenv wrote "FOO=0", and change it in-place to "FOO=\0".
73   // c.f. http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/environ.cpp?r1=611451&r2=637508&pathrev=637508
74   static const char* const kFakeZero = "0";
75   if (*value == '\0')
76     value = kFakeZero;
77   // Apparently the semantics of putenv() is that the input
78   // must live forever, so we leak memory here. :-(
79   const int nameval_len = strlen(name) + 1 + strlen(value) + 1;
80   char* nameval = reinterpret_cast<char*>(malloc(nameval_len));
81   snprintf(nameval, nameval_len, "%s=%s", name, value);
82   _putenv(nameval);
83   if (value == kFakeZero) {
84     nameval[nameval_len - 2] = '\0';   // works when putenv() makes no copy
85     if (*getenv(name) != '\0')
86       *getenv(name) = '\0';            // works when putenv() copies nameval
87   }
88 }
89 
90 #define strcasecmp _stricmp
91 
92 #if defined(_MSC_VER) && _MSC_VER >= 1400
93 #define strdup   _strdup
94 #define unlink   _unlink
95 #endif
96 
97 #define PRId32  "d"
98 #define PRIu32  "u"
99 #define PRId64  "I64d"
100 #define PRIu64  "I64u"
101 
102 #ifndef __MINGW32__
103 #define strtoq   _strtoi64
104 #define strtouq  _strtoui64
105 #define strtoll  _strtoi64
106 #define strtoull _strtoui64
107 #define atoll    _atoi64
108 #endif
109 
110 #ifndef PATH_MAX
111 #define PATH_MAX 1024
112 #endif
113 
114 #endif  /* _WIN32 */
115 
116 #endif  /* GOOGLE_GFLAGS_WINDOWS_PORT_H_ */
117