Skip to content
Snippets Groups Projects
Commit 86edb666 authored by Brad King's avatar Brad King Committed by Kitware Robot
Browse files

Merge topic 'simplify-env-var-determination'


5731f6d5 cm{Unset,Set}Command: use std::string to determine the env variable name

Acked-by: default avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !1350
parents 7237d948 5731f6d5
No related branches found
No related tags found
No related merge requests found
......@@ -2,8 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmSetCommand.h"
#include <string.h>
#include "cmAlgorithms.h"
#include "cmMakefile.h"
#include "cmState.h"
......@@ -22,19 +20,15 @@ bool cmSetCommand::InitialPass(std::vector<std::string> const& args,
}
// watch for ENV signatures
const char* variable = args[0].c_str(); // VAR is always first
if (cmHasLiteralPrefix(variable, "ENV{") && strlen(variable) > 5) {
auto const& variable = args[0]; // VAR is always first
if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
// what is the variable name
char* varName = new char[strlen(variable)];
strncpy(varName, variable + 4, strlen(variable) - 5);
varName[strlen(variable) - 5] = '\0';
std::string putEnvArg = varName;
putEnvArg += "=";
auto const& varName = variable.substr(4, variable.size() - 5);
std::string putEnvArg = varName + "=";
// what is the current value if any
std::string currValue;
const bool currValueSet = cmSystemTools::GetEnv(varName, currValue);
delete[] varName;
// will it be set to something, then set it
if (args.size() > 1 && !args[1].empty()) {
......
......@@ -2,8 +2,6 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmUnsetCommand.h"
#include <string.h>
#include "cmAlgorithms.h"
#include "cmMakefile.h"
#include "cmSystemTools.h"
......@@ -19,19 +17,16 @@ bool cmUnsetCommand::InitialPass(std::vector<std::string> const& args,
return false;
}
const char* variable = args[0].c_str();
auto const& variable = args[0];
// unset(ENV{VAR})
if (cmHasLiteralPrefix(variable, "ENV{") && strlen(variable) > 5) {
if (cmHasLiteralPrefix(variable, "ENV{") && variable.size() > 5) {
// what is the variable name
char* envVarName = new char[strlen(variable)];
strncpy(envVarName, variable + 4, strlen(variable) - 5);
envVarName[strlen(variable) - 5] = '\0';
auto const& envVarName = variable.substr(4, variable.size() - 5);
#ifdef CMAKE_BUILD_WITH_CMAKE
cmSystemTools::UnsetEnv(envVarName);
cmSystemTools::UnsetEnv(envVarName.c_str());
#endif
delete[] envVarName;
return true;
}
// unset(VAR)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment