Implement clearenv in the portability headers
authorChristopher Dykes <cdykes@fb.com>
Tue, 11 Apr 2017 19:26:53 +0000 (12:26 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 11 Apr 2017 19:36:04 +0000 (12:36 -0700)
Summary:
It doesn't exist on OSX or Windows, so implement it.

Closes https://github.com/facebook/folly/issues/567

Reviewed By: yfeldblum

Differential Revision: D4783463

fbshipit-source-id: 0a2586aced7123b797a8e55a3e86124b449634e4

folly/portability/Stdlib.cpp
folly/portability/Stdlib.h

index 013a7d32539d0a2752d46db9a9d40831121572b9..e790a3ea27eaa6906cba5eb993007604a932eb13 100644 (file)
@@ -136,3 +136,30 @@ int unsetenv(const char* name) {
 }
 }
 #endif
+
+#if !__linux__ && !FOLLY_MOBILE
+#include <string>
+#include <vector>
+
+extern "C" int clearenv() {
+  std::vector<std::string> data;
+  for (auto it = environ; it && *it; ++it) {
+    std::string entry(*it);
+    auto equalsPosition = entry.find('=');
+    if (equalsPosition == std::string::npos || equalsPosition == 0) {
+      // It's either a drive setting (if on Windows), or something clowny is
+      // going on in the environment.
+      continue;
+    } else {
+      data.emplace_back(entry.substr(0, equalsPosition));
+    }
+  }
+
+  for (auto s : data) {
+    if (unsetenv(s.c_str()) != 0)
+      return -1;
+  }
+
+  return 0;
+}
+#endif
index 333453da781d450ca6fa52afed858b26f9fb49c9..1124273b607215902155775d8197ba5e29886c5a 100644 (file)
@@ -47,4 +47,8 @@ char*** _NSGetEnviron(void);
 #endif
 #define environ (*_NSGetEnviron())
 #endif
+
+#if !__linux__ && !FOLLY_MOBILE
+int clearenv();
+#endif
 }