lseek returns the new offset, not 0
[folly.git] / folly / portability / Environment.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/portability/Environment.h>
18
19 #ifdef _WIN32
20 #include <Windows.h>
21
22 extern "C" {
23
24 int setenv(const char* name, const char* value, int overwrite) {
25   if (overwrite == 0 && getenv(name) != nullptr) {
26     return 0;
27   }
28
29   if (*value != '\0') {
30     auto e = _putenv_s(name, value);
31     if (e != 0) {
32       errno = e;
33       return -1;
34     }
35     return 0;
36   }
37
38   // We are trying to set the value to an empty string, but
39   // _putenv_s deletes entries if the value is an empty string,
40   // and just calling SetEnvironmentVariableA doesn't update
41   // _environ, so we have to do these terrible things.
42   if (_putenv_s(name, "  ") != 0) {
43     errno = EINVAL;
44     return -1;
45   }
46
47   // Here lies the documentation we blatently ignore to make
48   // this work >_>...
49   *getenv(name) = '\0';
50   // This would result in a double null termination, which
51   // normally signifies the end of the environment variable
52   // list, so we stick a completely empty environment variable
53   // into the list instead.
54   *(getenv(name) + 1) = '=';
55
56   // If _wenviron is null, the wide environment has not been initialized
57   // yet, and we don't need to try to update it.
58   // We have to do this otherwise we'd be forcing the initialization and
59   // maintenance of the wide environment even though it's never actually
60   // used in most programs.
61   if (_wenviron != nullptr) {
62     wchar_t buf[_MAX_ENV + 1];
63     size_t len;
64     if (mbstowcs_s(&len, buf, _MAX_ENV + 1, name, _MAX_ENV) != 0) {
65       errno = EINVAL;
66       return -1;
67     }
68     *_wgetenv(buf) = u'\0';
69     *(_wgetenv(buf) + 1) = u'=';
70   }
71
72   // And now, we have to update the outer environment to have
73   // a proper empty value.
74   if (!SetEnvironmentVariableA(name, value)) {
75     errno = EINVAL;
76     return -1;
77   }
78   return 0;
79 }
80
81 int unsetenv(const char* name) {
82   if (_putenv_s(name, "") != 0) {
83     return -1;
84   }
85   return 0;
86 }
87 }
88 #endif