Skip to content

Commit

Permalink
Make the single argument constructor inherit the env
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <mjcarroll@intrinsic.ai>
  • Loading branch information
mjcarroll committed Oct 31, 2023
1 parent 36172b7 commit 4d1e541
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions include/gz/utils/Subprocess.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,19 @@ namespace utils

class Subprocess
{
public: Subprocess(const std::vector<std::string> &_commandLine):
commandLine(_commandLine),
environment({}),
inheritEnvironment(true)
{
this->Create();
}

public: Subprocess(const std::vector<std::string> &_commandLine,
const std::vector<std::string> &_environment = {}):
const std::vector<std::string> &_environment):
commandLine(_commandLine),
environment(_environment)
environment(_environment),
inheritEnvironment(false)
{
this->Create();
}
Expand All @@ -57,21 +66,26 @@ class Subprocess
commandLineCstr.push_back(nullptr);

std::vector<const char*> environmentCstr;
for (const auto &val : this->environment)
if (!this->inheritEnvironment)
{
environmentCstr.push_back(val.c_str());
for (const auto &val : this->environment)
{
environmentCstr.push_back(val.c_str());
}
environmentCstr.push_back(nullptr);
}
environmentCstr.push_back(nullptr);

int ret = -1;
if (this->environment.size())
if (!this->inheritEnvironment && this->environment.size())
{
ret = subprocess_create_ex(commandLineCstr.data(),
0, environmentCstr.data(), this->process);
}
else
{
ret = subprocess_create(commandLineCstr.data(), 0, this->process);
ret = subprocess_create(commandLineCstr.data(),
subprocess_option_inherit_environment,
this->process);
}

if (0 != ret)
Expand Down Expand Up @@ -157,6 +171,8 @@ class Subprocess

protected: std::vector<std::string> environment;

protected: bool inheritEnvironment;

protected: subprocess_s * process {nullptr};
};
} // namespace utils
Expand Down

0 comments on commit 4d1e541

Please sign in to comment.