]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify x_setenv
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 30 Jul 2020 05:21:45 +0000 (07:21 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 30 Jul 2020 19:04:01 +0000 (21:04 +0200)
src/Util.cpp
src/Util.hpp
src/argprocessing.cpp
src/ccache.cpp
src/legacy_util.cpp
src/legacy_util.hpp
unittest/main.cpp
unittest/test_Config.cpp
unittest/test_Util.cpp

index fd19e1a4cabf5d9332150a6479192d396d03759a..c6294dcc6133421e76d97ddba70f1dba91cfe957 100644 (file)
@@ -1100,6 +1100,18 @@ send_to_stderr(const std::string& text, bool strip_colors)
   }
 }
 
+void
+setenv(const std::string& name, const std::string& value)
+{
+#ifdef HAVE_SETENV
+  ::setenv(name.c_str(), value.c_str(), true);
+#else
+  char* string;
+  asprintf(&string, "%s=%s", name.c_str(), value.c_str());
+  putenv(string); // Leak to environment.
+#endif
+}
+
 std::vector<string_view>
 split_into_views(string_view input, const char* separators)
 {
index ef9a3a247cda7485a62ac13365e95d25f06f146f..e01b8ffb2a1975c72d7f27fa983d2c0c1b254d5d 100644 (file)
@@ -338,6 +338,9 @@ bool same_program_name(const std::string& program_name,
 // `strip_colors` is true. Throws `Error` on error.
 void send_to_stderr(const std::string& text, bool strip_colors);
 
+// Set environment variable `name` to `value`.
+void setenv(const std::string& name, const std::string& value);
+
 // Split `input` into words at any of the characters listed in `separators`.
 // These words are a view into `input`; empty words are omitted. `separators`
 // must neither be the empty string nor a nullptr.
index 981eb31a1c8199b21009e3c12bb90feb9354f806..773bdedc00059ba3ab70ee0c971d11d48a5acc67 100644 (file)
@@ -905,18 +905,18 @@ handle_dependency_environment_variables(Context& ctx,
     std::string relpath_both =
       fmt::format("{} {}", args_info.output_dep, relpath_obj);
     if (using_sunpro_dependencies) {
-      x_setenv("SUNPRO_DEPENDENCIES", relpath_both.c_str());
+      Util::setenv("SUNPRO_DEPENDENCIES", relpath_both);
     } else {
-      x_setenv("DEPENDENCIES_OUTPUT", relpath_both.c_str());
+      Util::setenv("DEPENDENCIES_OUTPUT", relpath_both);
     }
   } else {
     // It's the "file" form.
     state.dependency_implicit_target_specified = true;
     // Ensure that the compiler gets a relative path.
     if (using_sunpro_dependencies) {
-      x_setenv("SUNPRO_DEPENDENCIES", args_info.output_dep.c_str());
+      Util::setenv("SUNPRO_DEPENDENCIES", args_info.output_dep);
     } else {
-      x_setenv("DEPENDENCIES_OUTPUT", args_info.output_dep.c_str());
+      Util::setenv("DEPENDENCIES_OUTPUT", args_info.output_dep);
     }
   }
 }
index a5d36099e190f6994136bb43eab60af03f5d5f92..a47f3af95b4526a75d1bed0944f3da8ffd5636e2 100644 (file)
@@ -1871,7 +1871,7 @@ set_up_uncached_err()
     failed(STATS_ERROR);
   }
 
-  x_setenv("UNCACHED_ERR_FD", fmt::format("{}", uncached_fd).c_str());
+  Util::setenv("UNCACHED_ERR_FD", fmt::format("{}", uncached_fd));
 }
 
 static void
index aa962abcfd6d4acb6ebc79130982041f0daea265..e528d07dcf68c8ff48dfe7983d0a8c65a1da67bb 100644 (file)
 #  include <sys/time.h>
 #endif
 
-// This is like setenv.
-void
-x_setenv(const char* name, const char* value)
-{
-#ifdef HAVE_SETENV
-  setenv(name, value, true);
-#else
-  char* string;
-  asprintf(&string, "%s=%s", name, value);
-  putenv(string);       // Leak to environment.
-#endif
-}
-
 // This is like unsetenv.
 void
 x_unsetenv(const char* name)
index 8b39c63f2ce537d15dd52feb7e3d70f3f2b57ffc..7dabe62fe52c2ae118ad558acb06ee364e558c35 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <string>
 
-void x_setenv(const char* name, const char* value);
 void x_unsetenv(const char* name);
 #ifndef HAVE_LOCALTIME_R
 struct tm* localtime_r(const time_t* timep, struct tm* result);
index 863301a6bb24a998557b23b7a5d05bef91156327..53887e294b6103979be4c53c32473349a1a85261 100644 (file)
@@ -29,7 +29,7 @@ int
 main(int argc, char** argv)
 {
 #ifdef _WIN32
-  x_setenv("CCACHE_DETECT_SHEBANG", "1");
+  Util::setenv("CCACHE_DETECT_SHEBANG", "1");
 #endif
   x_unsetenv("GCC_COLORS"); // Don't confuse argument processing tests.
 
index 882033b9a878650dec1eb49608cd2468d72f4791..c487e2f035dd895e5261960a947c2d90372567f3 100644 (file)
@@ -92,7 +92,7 @@ TEST_CASE("Config::update_from_file")
   TestContext test_context;
 
   const char user[] = "rabbit";
-  x_setenv("USER", user);
+  Util::setenv("USER", user);
 
 #ifndef _WIN32
   std::string base_dir = fmt::format("/{0}/foo/{0}", user);
@@ -305,13 +305,13 @@ TEST_CASE("Config::update_from_environment")
 {
   Config config;
 
-  x_setenv("CCACHE_COMPRESS", "1");
+  Util::setenv("CCACHE_COMPRESS", "1");
   config.update_from_environment();
   CHECK(config.compression());
 
   x_unsetenv("CCACHE_COMPRESS");
 
-  x_setenv("CCACHE_NOCOMPRESS", "1");
+  Util::setenv("CCACHE_NOCOMPRESS", "1");
   config.update_from_environment();
   CHECK(!config.compression());
 }
index 60a0304fcc20641db6f4f032d0b4fa896f0396ac..fb76f43b70a79f87b72cf2a9fcf6a135292b09b9 100644 (file)
@@ -161,7 +161,7 @@ TEST_CASE("Util::ends_with")
 
 TEST_CASE("Util::expand_environment_variables")
 {
-  x_setenv("FOO", "bar");
+  Util::setenv("FOO", "bar");
 
   CHECK(Util::expand_environment_variables("") == "");
   CHECK(Util::expand_environment_variables("$FOO") == "bar");