Implement clearenv in the portability headers
[folly.git] / folly / portability / Stdlib.cpp
1 /*
2  * Copyright 2017 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/Stdlib.h>
18
19 #ifdef _WIN32
20 #include <cstring>
21 #include <errno.h>
22
23 #include <folly/portability/Fcntl.h>
24 #include <folly/portability/SysStat.h>
25 #include <folly/portability/Windows.h>
26
27 extern "C" {
28 char* mktemp(char* tn) { return _mktemp(tn); }
29
30 // While yes, this is for a directory, due to this being windows,
31 // a file and directory can't have the same name, resulting in this
32 // still working just fine.
33 char* mkdtemp(char* tn) {
34   char* ptr = nullptr;
35   auto len = strlen(tn);
36   int ret = 0;
37   do {
38     strcpy(tn + len - 6, "XXXXXX");
39     ptr = mktemp(tn);
40     if (ptr == nullptr || *ptr == '\0') {
41       return nullptr;
42     }
43     ret = mkdir(ptr, 0700);
44     if (ret != 0 && errno != EEXIST) {
45       return nullptr;
46     }
47   } while (ret != 0);
48   return tn;
49 }
50
51 int mkstemp(char* tn) {
52   char* ptr = nullptr;
53   auto len = strlen(tn);
54   int ret = 0;
55   do {
56     strcpy(tn + len - 6, "XXXXXX");
57     ptr = mktemp(tn);
58     if (ptr == nullptr || *ptr == '\0') {
59       return -1;
60     }
61     ret = open(ptr, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
62     if (ret == -1 && errno != EEXIST) {
63       return -1;
64     }
65   } while (ret == -1);
66   return ret;
67 }
68
69 char* realpath(const char* path, char* resolved_path) {
70   // I sure hope the caller gave us _MAX_PATH space in the buffer....
71   return _fullpath(resolved_path, path, _MAX_PATH);
72 }
73
74 int setenv(const char* name, const char* value, int overwrite) {
75   if (overwrite == 0 && getenv(name) != nullptr) {
76     return 0;
77   }
78
79   if (*value != '\0') {
80     auto e = _putenv_s(name, value);
81     if (e != 0) {
82       errno = e;
83       return -1;
84     }
85     return 0;
86   }
87
88   // We are trying to set the value to an empty string, but
89   // _putenv_s deletes entries if the value is an empty string,
90   // and just calling SetEnvironmentVariableA doesn't update
91   // _environ, so we have to do these terrible things.
92   if (_putenv_s(name, "  ") != 0) {
93     errno = EINVAL;
94     return -1;
95   }
96
97   // Here lies the documentation we blatently ignore to make
98   // this work >_>...
99   *getenv(name) = '\0';
100   // This would result in a double null termination, which
101   // normally signifies the end of the environment variable
102   // list, so we stick a completely empty environment variable
103   // into the list instead.
104   *(getenv(name) + 1) = '=';
105
106   // If _wenviron is null, the wide environment has not been initialized
107   // yet, and we don't need to try to update it.
108   // We have to do this otherwise we'd be forcing the initialization and
109   // maintenance of the wide environment even though it's never actually
110   // used in most programs.
111   if (_wenviron != nullptr) {
112     wchar_t buf[_MAX_ENV + 1];
113     size_t len;
114     if (mbstowcs_s(&len, buf, _MAX_ENV + 1, name, _MAX_ENV) != 0) {
115       errno = EINVAL;
116       return -1;
117     }
118     *_wgetenv(buf) = u'\0';
119     *(_wgetenv(buf) + 1) = u'=';
120   }
121
122   // And now, we have to update the outer environment to have
123   // a proper empty value.
124   if (!SetEnvironmentVariableA(name, value)) {
125     errno = EINVAL;
126     return -1;
127   }
128   return 0;
129 }
130
131 int unsetenv(const char* name) {
132   if (_putenv_s(name, "") != 0) {
133     return -1;
134   }
135   return 0;
136 }
137 }
138 #endif
139
140 #if !__linux__ && !FOLLY_MOBILE
141 #include <string>
142 #include <vector>
143
144 extern "C" int clearenv() {
145   std::vector<std::string> data;
146   for (auto it = environ; it && *it; ++it) {
147     std::string entry(*it);
148     auto equalsPosition = entry.find('=');
149     if (equalsPosition == std::string::npos || equalsPosition == 0) {
150       // It's either a drive setting (if on Windows), or something clowny is
151       // going on in the environment.
152       continue;
153     } else {
154       data.emplace_back(entry.substr(0, equalsPosition));
155     }
156   }
157
158   for (auto s : data) {
159     if (unsetenv(s.c_str()) != 0)
160       return -1;
161   }
162
163   return 0;
164 }
165 #endif