implement and use a platform neutral abstraction of unsetenv(3)

This commit is contained in:
Axel Kohlmeyer
2021-11-03 10:53:10 -04:00
parent 515ef7bece
commit 50f39cd752
5 changed files with 41 additions and 9 deletions

View File

@ -446,11 +446,11 @@ int platform::putenv(const std::string &vardef)
auto found = vardef.find_first_of('=');
#ifdef _WIN32
// must assign a value to variable with _putenv()
// must assign a value to variable with _putenv_s()
if (found == std::string::npos)
return _putenv(utils::strdup(vardef + "=1"));
return _putenv_s(vardef.c_str(), "1");
else
return _putenv(utils::strdup(vardef));
return _putenv_s(vardef.substr(0, found).c_str(), vardef.substr(found+1).c_str());
#else
if (found == std::string::npos)
return setenv(vardef.c_str(), "", 1);
@ -460,6 +460,24 @@ int platform::putenv(const std::string &vardef)
return -1;
}
/* ----------------------------------------------------------------------
unset environment variable
------------------------------------------------------------------------- */
int platform::unsetenv(const std::string &variable)
{
if (variable.size() == 0) return -1;
#ifdef _WIN32
// emulate POSIX semantics by returning -1 on trying to unset non-existing variable
const char *ptr = getenv(variable.c_str());
if (!ptr) return -1;
// empty _putenv_s() definition deletes variable
return _putenv_s(variable.c_str(),"");
#else
return ::unsetenv(variable.c_str());
#endif
}
/* ----------------------------------------------------------------------
split a "path" environment variable into a list
------------------------------------------------------------------------- */