Commit 6cc24510 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

SystemTools: use strings in environment functions

Change-Id: I39dd9c093d04744ffcbf4d331e7e1e0cecbb677d
parent 86e20d68
......@@ -385,6 +385,11 @@ const char* SystemTools::GetEnv(const char* key)
return getenv(key);
}
const char* SystemTools::GetEnv(const kwsys_stl::string& key)
{
return SystemTools::GetEnv(key.c_str());
}
bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result)
{
const char* v = getenv(key);
......@@ -399,6 +404,11 @@ bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result)
}
}
bool SystemTools::GetEnv(const kwsys_stl::string& key, kwsys_stl::string& result)
{
return SystemTools::GetEnv(key.c_str(), result);
}
//----------------------------------------------------------------------------
#if defined(__CYGWIN__) || defined(__GLIBC__)
......@@ -410,27 +420,28 @@ bool SystemTools::GetEnv(const char* key, kwsys_stl::string& result)
#if KWSYS_CXX_HAS_UNSETENV
/* unsetenv("A") removes A from the environment.
On older platforms it returns void instead of int. */
static int kwsysUnPutEnv(const char* env)
static int kwsysUnPutEnv(const kwsys_stl::string& env)
{
if(const char* eq = strchr(env, '='))
size_t pos = env.find('=');
if(pos != env.npos)
{
std::string name(env, eq-env);
std::string name = env.substr(0, pos);
unsetenv(name.c_str());
}
else
{
unsetenv(env);
unsetenv(env.c_str());
}
return 0;
}
#elif defined(KWSYS_PUTENV_EMPTY) || defined(KWSYS_PUTENV_NAME)
/* putenv("A=") or putenv("A") removes A from the environment. */
static int kwsysUnPutEnv(const char* env)
static int kwsysUnPutEnv(const kwsys_stl::string& env)
{
int err = 0;
const char* eq = strchr(env, '=');
size_t const len = eq? (size_t)(eq-env) : strlen(env);
size_t pos = env.find('=');
size_t const len = pos == env.npos ? env.size() : pos;
# ifdef KWSYS_PUTENV_EMPTY
size_t const sz = len + 2;
# else
......@@ -442,7 +453,7 @@ static int kwsysUnPutEnv(const char* env)
{
return -1;
}
strncpy(buf, env, len);
strncpy(buf, env.c_str(), len);
# ifdef KWSYS_PUTENV_EMPTY
buf[len] = '=';
buf[len+1] = 0;
......@@ -471,17 +482,17 @@ static int kwsysUnPutEnv(const char* env)
#else
/* Manipulate the "environ" global directly. */
static int kwsysUnPutEnv(const char* env)
static int kwsysUnPutEnv(const kwsys_stl::string& env)
{
const char* eq = strchr(env, '=');
size_t const len = eq? (size_t)(eq-env) : strlen(env);
size_t pos = env.find('=');
size_t const len = pos == env.npos ? env.size() : pos;
int in = 0;
int out = 0;
while(environ[in])
{
if(strlen(environ[in]) > len &&
environ[in][len] == '=' &&
strncmp(env, environ[in], len) == 0)
strncmp(env.c_str(), environ[in], len) == 0)
{
++in;
}
......@@ -504,12 +515,13 @@ static int kwsysUnPutEnv(const char* env)
/* setenv("A", "B", 1) will set A=B in the environment and makes its
own copies of the strings. */
bool SystemTools::PutEnv(const char* env)
bool SystemTools::PutEnv(const kwsys_stl::string& env)
{
if(const char* eq = strchr(env, '='))
size_t pos = env.find('=');
if(pos != env.npos)
{
std::string name(env, eq-env);
return setenv(name.c_str(), eq+1, 1) == 0;
std::string name = env.substr(0, pos);
return setenv(name.c_str(), env.c_str() + pos + 1, 1) == 0;
}
else
{
......@@ -517,7 +529,7 @@ bool SystemTools::PutEnv(const char* env)
}
}
bool SystemTools::UnPutEnv(const char* env)
bool SystemTools::UnPutEnv(const kwsys_stl::string& env)
{
return kwsysUnPutEnv(env) == 0;
}
......@@ -603,14 +615,14 @@ public:
static kwsysEnv kwsysEnvInstance;
bool SystemTools::PutEnv(const char* env)
bool SystemTools::PutEnv(const kwsys_stl::string& env)
{
return kwsysEnvInstance.Put(env);
return kwsysEnvInstance.Put(env.c_str());
}
bool SystemTools::UnPutEnv(const char* env)
bool SystemTools::UnPutEnv(const kwsys_stl::string& env)
{
return kwsysEnvInstance.UnPut(env);
return kwsysEnvInstance.UnPut(env.c_str());
}
#endif
......
......@@ -789,15 +789,17 @@ public:
* Read an environment variable
*/
static const char* GetEnv(const char* key);
static const char* GetEnv(const kwsys_stl::string& key);
static bool GetEnv(const char* key, kwsys_stl::string& result);
static bool GetEnv(const kwsys_stl::string& key, kwsys_stl::string& result);
/** Put a string into the environment
of the form var=value */
static bool PutEnv(const char* env);
static bool PutEnv(const kwsys_stl::string& env);
/** Remove a string from the environment.
Input is of the form "var" or "var=value" (value is ignored). */
static bool UnPutEnv(const char* env);
static bool UnPutEnv(const kwsys_stl::string& env);
/**
* Get current working directory CWD
......
......@@ -512,7 +512,7 @@ static bool CheckStringOperations()
//----------------------------------------------------------------------------
static bool CheckPutEnv(const char* env, const char* name, const char* value)
static bool CheckPutEnv(const kwsys_stl::string& env, const char* name, const char* value)
{
if(!kwsys::SystemTools::PutEnv(env))
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment