diff --git a/include/gz/utils/Subprocess.hh b/include/gz/utils/Subprocess.hh index 787daf5..3812a3c 100644 --- a/include/gz/utils/Subprocess.hh +++ b/include/gz/utils/Subprocess.hh @@ -34,10 +34,19 @@ namespace utils class Subprocess { + public: Subprocess(const std::vector &_commandLine): + commandLine(_commandLine), + environment({}), + inheritEnvironment(true) + { + this->Create(); + } + public: Subprocess(const std::vector &_commandLine, - const std::vector &_environment = {}): + const std::vector &_environment): commandLine(_commandLine), - environment(_environment) + environment(_environment), + inheritEnvironment(false) { this->Create(); } @@ -57,21 +66,26 @@ class Subprocess commandLineCstr.push_back(nullptr); std::vector 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) @@ -157,6 +171,8 @@ class Subprocess protected: std::vector environment; + protected: bool inheritEnvironment; + protected: subprocess_s * process {nullptr}; }; } // namespace utils