2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/portability/Environment.h>
24 int setenv(const char* name, const char* value, int overwrite) {
25 if (overwrite == 0 && getenv(name) != nullptr) {
30 auto e = _putenv_s(name, value);
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) {
47 // Here lies the documentation we blatently ignore to make
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) = '=';
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];
64 if (mbstowcs_s(&len, buf, _MAX_ENV + 1, name, _MAX_ENV) != 0) {
68 *_wgetenv(buf) = u'\0';
69 *(_wgetenv(buf) + 1) = u'=';
72 // And now, we have to update the outer environment to have
73 // a proper empty value.
74 if (!SetEnvironmentVariableA(name, value)) {
81 int unsetenv(const char* name) {
82 if (_putenv_s(name, "") != 0) {