From c8b02015d01b96eb9a0f89f24b75afe35cb6001e Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 30 Jul 2020 07:24:28 +0200 Subject: [PATCH] C++-ify x_unsetenv --- src/Util.cpp | 12 +++++++++++- src/Util.hpp | 3 +++ src/ccache.cpp | 4 ++-- src/legacy_util.cpp | 11 ----------- src/legacy_util.hpp | 1 - unittest/main.cpp | 2 +- unittest/test_Config.cpp | 2 +- 7 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/Util.cpp b/src/Util.cpp index c6294dcc6..e6c02e07b 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -1108,7 +1108,7 @@ setenv(const std::string& name, const std::string& value) #else char* string; asprintf(&string, "%s=%s", name.c_str(), value.c_str()); - putenv(string); // Leak to environment. + putenv(string); // Leak to environment. #endif } @@ -1268,6 +1268,16 @@ unlink_tmp(const std::string& path, UnlinkLog unlink_log) return success; } +void +unsetenv(const std::string& name) +{ +#ifdef HAVE_UNSETENV + ::unsetenv(name.c_str()); +#else + putenv(strdup(name.c_str())); // Leak to environment. +#endif +} + void wipe_path(const std::string& path) { diff --git a/src/Util.hpp b/src/Util.hpp index e01b8ffb2..c3485c5c8 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -387,6 +387,9 @@ bool unlink_safe(const std::string& path, bool unlink_tmp(const std::string& path, UnlinkLog unlink_log = UnlinkLog::log_failure); +// Unset environment variable `name`. +void unsetenv(const std::string& name); + // Remove `path` (and its contents if it's a directory). A non-existing path is // not considered an error. // diff --git a/src/ccache.cpp b/src/ccache.cpp index a47f3af95..e1e49cdc8 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -843,8 +843,8 @@ to_cache(Context& ctx, // emit a line like this: // // tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i - x_unsetenv("DEPENDENCIES_OUTPUT"); - x_unsetenv("SUNPRO_DEPENDENCIES"); + Util::unsetenv("DEPENDENCIES_OUTPUT"); + Util::unsetenv("SUNPRO_DEPENDENCIES"); if (ctx.config.run_second_cpp()) { args.push_back(ctx.args_info.input_file); diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index e528d07dc..e77ee159e 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -34,17 +34,6 @@ # include #endif -// This is like unsetenv. -void -x_unsetenv(const char* name) -{ -#ifdef HAVE_UNSETENV - unsetenv(name); -#else - putenv(strdup(name)); // Leak to environment. -#endif -} - #if !defined(_WIN32) && !defined(HAVE_LOCALTIME_R) // localtime_r replacement. (Mingw-w64 has an inline localtime_r which is not // detected by AC_CHECK_FUNCS.) diff --git a/src/legacy_util.hpp b/src/legacy_util.hpp index 7dabe62fe..89e0973af 100644 --- a/src/legacy_util.hpp +++ b/src/legacy_util.hpp @@ -22,7 +22,6 @@ #include -void x_unsetenv(const char* name); #ifndef HAVE_LOCALTIME_R struct tm* localtime_r(const time_t* timep, struct tm* result); #endif diff --git a/unittest/main.cpp b/unittest/main.cpp index 53887e294..645fc5896 100644 --- a/unittest/main.cpp +++ b/unittest/main.cpp @@ -31,7 +31,7 @@ main(int argc, char** argv) #ifdef _WIN32 Util::setenv("CCACHE_DETECT_SHEBANG", "1"); #endif - x_unsetenv("GCC_COLORS"); // Don't confuse argument processing tests. + Util::unsetenv("GCC_COLORS"); // Don't confuse argument processing tests. std::string dir_before = Util::get_actual_cwd(); std::string testdir = fmt::format("testdir.{}", getpid()); diff --git a/unittest/test_Config.cpp b/unittest/test_Config.cpp index c487e2f03..454fcc244 100644 --- a/unittest/test_Config.cpp +++ b/unittest/test_Config.cpp @@ -309,7 +309,7 @@ TEST_CASE("Config::update_from_environment") config.update_from_environment(); CHECK(config.compression()); - x_unsetenv("CCACHE_COMPRESS"); + Util::unsetenv("CCACHE_COMPRESS"); Util::setenv("CCACHE_NOCOMPRESS", "1"); config.update_from_environment(); -- 2.47.3