diff options
Diffstat (limited to 'src/MTS_System.cpp')
-rw-r--r-- | src/MTS_System.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/src/MTS_System.cpp b/src/MTS_System.cpp index 31976e1..8704a34 100644 --- a/src/MTS_System.cpp +++ b/src/MTS_System.cpp @@ -194,9 +194,36 @@ int32_t System::execute(const std::string& cmd, const std::vector<std::string>& return code; } +int32_t System::executeEnv(const std::string& cmd, const std::vector<std::string>& argv, std::string& result) { + // Ported directly from System::cmd + std::string output; + const int max_buffer = 256; + char buffer[max_buffer]; + int32_t code = -1; + ChildHandle child; + + if (executeBackgroundEnv(cmd, argv, PipeType::READ, child)) { + while (!feof(child.stream)) { + if (fgets(buffer, max_buffer, child.stream) != NULL) { + output.append(buffer); + } + } + code = closeBackground(child); + } + + result = output; + + return code; +} + bool System::executeBackground(const std::string& cmd, const std::vector<std::string>& argv, System::PipeType type, System::ChildHandle& child) { std::vector<char*> arguments = castArgVector(cmd, argv); - return executeBackground(cmd, arguments.data(), type, child); + return executeBackground(cmd, arguments.data(), false, type, child); +} + +bool System::executeBackgroundEnv(const std::string& cmd, const std::vector<std::string>& argv, System::PipeType type, System::ChildHandle& child) { + std::vector<char*> arguments = castArgVector(cmd, argv); + return executeBackground(cmd, arguments.data(), true, type, child); } int System::closeBackground(System::ChildHandle& child) { @@ -212,7 +239,9 @@ int System::closeBackground(System::ChildHandle& child) { return pstat; } -bool System::executeBackground(const std::string& cmd, char* const argv[], System::PipeType type, ChildHandle& child) { +extern char **environ; + +bool System::executeBackground(const std::string& cmd, char* const argv[], bool env, System::PipeType type, ChildHandle& child) { pid_t childPid; FILE* stream = NULL; posix_spawn_file_actions_t actions; @@ -262,7 +291,7 @@ bool System::executeBackground(const std::string& cmd, char* const argv[], Syste break; } - if (posix_spawnp(&childPid, cmd.c_str(), &actions, NULL, argv, NULL) != 0) { + if (posix_spawnp(&childPid, cmd.c_str(), &actions, NULL, argv, env ? environ : NULL) != 0) { // failed to spawn the process break; } |