From 6a2ef269f37820db40b0d1cd19d7fdb7696c0d2a Mon Sep 17 00:00:00 2001 From: Serhii Kostiuk Date: Thu, 9 Apr 2020 16:43:06 +0300 Subject: Execute system commands without invoking system shell Changes after a code review: - fixed documentation for the closeBackground function - renamed ```app``` parameter to ```cmd``` --- src/MTS_System.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/MTS_System.cpp b/src/MTS_System.cpp index 6acf065..31976e1 100644 --- a/src/MTS_System.cpp +++ b/src/MTS_System.cpp @@ -172,7 +172,7 @@ int32_t System::readFile(const std::string& path, std::string& result) { return 0; } -int32_t System::execute(const std::string& app, const std::vector& argv, std::string& result) { +int32_t System::execute(const std::string& cmd, const std::vector& argv, std::string& result) { // Ported directly from System::cmd std::string output; const int max_buffer = 256; @@ -180,7 +180,7 @@ int32_t System::execute(const std::string& app, const std::vector& int32_t code = -1; ChildHandle child; - if (executeBackground(app, argv, PipeType::READ, child)) { + if (executeBackground(cmd, argv, PipeType::READ, child)) { while (!feof(child.stream)) { if (fgets(buffer, max_buffer, child.stream) != NULL) { output.append(buffer); @@ -194,9 +194,9 @@ int32_t System::execute(const std::string& app, const std::vector& return code; } -bool System::executeBackground(const std::string& app, const std::vector& argv, System::PipeType type, System::ChildHandle& child) { - std::vector arguments = castArgVector(app, argv); - return executeBackground(app, arguments.data(), type, child); +bool System::executeBackground(const std::string& cmd, const std::vector& argv, System::PipeType type, System::ChildHandle& child) { + std::vector arguments = castArgVector(cmd, argv); + return executeBackground(cmd, arguments.data(), type, child); } int System::closeBackground(System::ChildHandle& child) { @@ -212,7 +212,7 @@ int System::closeBackground(System::ChildHandle& child) { return pstat; } -bool System::executeBackground(const std::string& app, char* const argv[], System::PipeType type, ChildHandle& child) { +bool System::executeBackground(const std::string& cmd, char* const argv[], System::PipeType type, ChildHandle& child) { pid_t childPid; FILE* stream = NULL; posix_spawn_file_actions_t actions; @@ -262,7 +262,7 @@ bool System::executeBackground(const std::string& app, char* const argv[], Syste break; } - if (posix_spawnp(&childPid, app.c_str(), &actions, NULL, argv, NULL) != 0) { + if (posix_spawnp(&childPid, cmd.c_str(), &actions, NULL, argv, NULL) != 0) { // failed to spawn the process break; } @@ -294,11 +294,11 @@ char* System::castArgument(const std::string& arg) { return const_cast(arg.c_str()); } -std::vector System::castArgVector(const std::string& app, const std::vector& argv) { +std::vector System::castArgVector(const std::string& cmd, const std::vector& argv) { std::vector result; // Append application name as argument zero - result.push_back(castArgument(app)); + result.push_back(castArgument(cmd)); // Append all other arguments for (const std::string& arg : argv) { -- cgit v1.2.3