From a5a611496aa06ce04fe4a4411cdafbf5ab8e8ec6 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Tue, 11 Apr 2017 12:26:53 -0700 Subject: [PATCH] Implement clearenv in the portability headers 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 | 27 +++++++++++++++++++++++++++ folly/portability/Stdlib.h | 4 ++++ 2 files changed, 31 insertions(+) diff --git a/folly/portability/Stdlib.cpp b/folly/portability/Stdlib.cpp index 013a7d32..e790a3ea 100644 --- a/folly/portability/Stdlib.cpp +++ b/folly/portability/Stdlib.cpp @@ -136,3 +136,30 @@ int unsetenv(const char* name) { } } #endif + +#if !__linux__ && !FOLLY_MOBILE +#include +#include + +extern "C" int clearenv() { + std::vector 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 diff --git a/folly/portability/Stdlib.h b/folly/portability/Stdlib.h index 333453da..1124273b 100644 --- a/folly/portability/Stdlib.h +++ b/folly/portability/Stdlib.h @@ -47,4 +47,8 @@ char*** _NSGetEnviron(void); #endif #define environ (*_NSGetEnviron()) #endif + +#if !__linux__ && !FOLLY_MOBILE +int clearenv(); +#endif } -- 2.34.1