blob: cbe130719a1be6915cde92ba46922e6800c113fa [file] [log] [blame]
Craig Silversteinab3d7652009-11-10 16:33:51 +00001/* 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
34#ifndef _WIN32
35# error You should only be including windows/port.cc in a windows environment!
36#endif
37
38#include <config.h>
39#include <string.h> // for strlen(), memset(), memcmp()
Craig Silverstein917f4e72011-07-29 04:26:49 +000040#include <stdlib.h> // for _putenv, etc.
Craig Silversteinab3d7652009-11-10 16:33:51 +000041#include <assert.h>
42#include <stdarg.h> // for va_list, va_start, va_end
43#include <windows.h>
44#include "port.h"
45
Craig Silverstein917f4e72011-07-29 04:26:49 +000046// These call the windows _vsnprintf, but always NUL-terminate.
47#if !defined(__MINGW32__) && !defined(__MINGW64__) /* mingw already defines */
48int safe_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
Craig Silversteinab3d7652009-11-10 16:33:51 +000049 if (size == 0) // not even room for a \0?
50 return -1; // not what C99 says to do, but what windows does
Craig Silverstein917f4e72011-07-29 04:26:49 +000051 str[size-1] = '\0';
52 return _vsnprintf(str, size-1, format, ap);
53}
54
55int snprintf(char *str, size_t size, const char *format, ...) {
56 int r;
Craig Silversteinab3d7652009-11-10 16:33:51 +000057 va_list ap;
58 va_start(ap, format);
Craig Silverstein917f4e72011-07-29 04:26:49 +000059 r = vsnprintf(str, size, format, ap);
Craig Silversteinab3d7652009-11-10 16:33:51 +000060 va_end(ap);
Craig Silversteinab3d7652009-11-10 16:33:51 +000061 return r;
62}
Craig Silverstein917f4e72011-07-29 04:26:49 +000063#endif /* #if !defined(__MINGW32__) && !defined(__MINGW64__) */
64
65void setenv(const char* name, const char* value, int) {
66 // In windows, it's impossible to set a variable to the empty string.
67 // We handle this by setting it to "0" and the NUL-ing out the \0.
68 // That is, we putenv("FOO=0") and then find out where in memory the
69 // putenv wrote "FOO=0", and change it in-place to "FOO=\0".
70 // c.f. http://svn.apache.org/viewvc/stdcxx/trunk/tests/src/environ.cpp?r1=611451&r2=637508&pathrev=637508
71 static const char* const kFakeZero = "0";
72 if (*value == '\0')
73 value = kFakeZero;
74 // Apparently the semantics of putenv() is that the input
75 // must live forever, so we leak memory here. :-(
76 const int nameval_len = strlen(name) + 1 + strlen(value) + 1;
77 char* nameval = reinterpret_cast<char*>(malloc(nameval_len));
78 snprintf(nameval, nameval_len, "%s=%s", name, value);
79 _putenv(nameval);
80 if (value == kFakeZero) {
81 nameval[nameval_len - 2] = '\0'; // works when putenv() makes no copy
82 if (*getenv(name) != '\0')
83 *getenv(name) = '\0'; // works when putenv() copies nameval
84 }
85}