diff options
author | Tom Hubrecht <github@mail.hubrecht.ovh> | 2024-05-28 10:50:54 +0200 |
---|---|---|
committer | Tom Hubrecht <github@mail.hubrecht.ovh> | 2024-05-28 11:29:29 +0200 |
commit | 6fd6795bc4faed644c5f3abafdcb21638a119342 (patch) | |
tree | 29c0dfa1f90a44af6ca22a1985b45b13505f83c7 /src/libutil/util.cc | |
parent | 0b91a4b0ec79c27ee36d8a7e2afd7737cb825b65 (diff) |
util.{hh,cc}: Split out environment-variables.{hh,cc}
Change-Id: Icff0aa33fda5147bd5dbe256a0b9d6a6c8a2c3f6
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r-- | src/libutil/util.cc | 46 |
1 files changed, 1 insertions, 45 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 2c0fcc897..97d53ca63 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -4,6 +4,7 @@ #include "serialise.hh" #include "cgroup.hh" #include "signals.hh" +#include "environment-variables.hh" #include <array> #include <cctype> @@ -43,57 +44,12 @@ #endif -extern char * * environ __attribute__((weak)); - - #ifdef NDEBUG #error "Lix may not be built with assertions disabled (i.e. with -DNDEBUG)." #endif namespace nix { -std::optional<std::string> getEnv(const std::string & key) -{ - char * value = getenv(key.c_str()); - if (!value) return {}; - return std::string(value); -} - -std::optional<std::string> getEnvNonEmpty(const std::string & key) { - auto value = getEnv(key); - if (value == "") return {}; - return value; -} - -std::map<std::string, std::string> getEnv() -{ - std::map<std::string, std::string> env; - for (size_t i = 0; environ[i]; ++i) { - auto s = environ[i]; - auto eq = strchr(s, '='); - if (!eq) - // invalid env, just keep going - continue; - env.emplace(std::string(s, eq), std::string(eq + 1)); - } - return env; -} - - -void clearEnv() -{ - for (auto & name : getEnv()) - unsetenv(name.first.c_str()); -} - -void replaceEnv(const std::map<std::string, std::string> & newEnv) -{ - clearEnv(); - for (auto & newEnvVar : newEnv) - setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1); -} - - Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks) { if (path.empty() || path[0] != '/') { |