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

@ -118,6 +118,9 @@ Environment variable functions
.. doxygenfunction:: putenv
:project: progguide
.. doxygenfunction:: unsetenv
:project: progguide
.. doxygenfunction:: list_pathenv
:project: progguide

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
------------------------------------------------------------------------- */

View File

@ -125,6 +125,13 @@ namespace platform {
int putenv(const std::string &vardef);
/*! Delete variable from the environment
*
* \param variable variable name
* \return -1 if failure otherwise 0 */
int unsetenv(const std::string &variable);
/*! Get list of entries in a path environment variable
*
* This provides a list of strings of the entries in an environment

View File

@ -363,11 +363,7 @@ TEST(LAMMPS_init, NoOpenMP)
FILE *fp = fopen("in.lammps_class_noomp", "w");
fputs("\n", fp);
fclose(fp);
#if defined(__WIN32)
_putenv("OMP_NUM_THREADS");
#else
unsetenv("OMP_NUM_THREADS");
#endif
platform::unsetenv("OMP_NUM_THREADS");
const char *args[] = {"LAMMPS_init", "-in", "in.lammps_class_noomp", "-log", "none", "-nocite"};
char **argv = (char **)args;

View File

@ -37,7 +37,7 @@ TEST(Platform, clock)
ASSERT_GT(ct_used, 1e-4);
}
TEST(Platform, putenv)
TEST(Platform, putenv_unsetenv)
{
const char *var = getenv("UNITTEST_VAR1");
ASSERT_EQ(var, nullptr);
@ -65,6 +65,14 @@ TEST(Platform, putenv)
ASSERT_THAT(var, StrEq("one=two"));
ASSERT_EQ(platform::putenv(""), -1);
ASSERT_EQ(platform::unsetenv(""), -1);
ASSERT_EQ(platform::unsetenv("UNITTEST_VAR3=two"), -1);
var = getenv("UNITTEST_VAR1");
ASSERT_NE(var, nullptr);
ASSERT_EQ(platform::unsetenv("UNITTEST_VAR1"), 0);
var = getenv("UNITTEST_VAR1");
ASSERT_EQ(var, nullptr);
}
TEST(Platform, list_pathenv)