diff --git a/include/shell.hpp b/include/shell.hpp index 9b40448..82fe357 100644 --- a/include/shell.hpp +++ b/include/shell.hpp @@ -2,6 +2,7 @@ #define SHELL_HPP #include +#include #include /*! \file shell.hpp @@ -15,10 +16,17 @@ namespace ztd @param command Shell command to execute @param to_console Output to console @return Output of the command - @see system(), pclose() */ std::string sh(const std::string& command, bool to_console=false); + //! @brief Execute a shell command and retrieve its output and return value + /*! + @param command Shell command to execute + @param to_console Output to console + @return @b first Output of command\n@b second Return value of command + */ + std::pair shp(const std::string& command, bool to_console=false); + //! @brief popen C function with added pid functionality /*! @param pid Pointer to an @a int in which the process's pid will be stored diff --git a/src/shell.cpp b/src/shell.cpp index 8b2bd31..2a2876e 100644 --- a/src/shell.cpp +++ b/src/shell.cpp @@ -8,6 +8,11 @@ #include std::string ztd::sh(const std::string& command, bool to_console) +{ + return ztd::shp(command, to_console).first; +} + +std::pair ztd::shp(const std::string& command, bool to_console) { std::string ret; FILE *stream = popen(command.c_str(), "r"); @@ -21,8 +26,7 @@ std::string ztd::sh(const std::string& command, bool to_console) } ret += buff; } - pclose(stream); - return ret; + return std::make_pair(ret, pclose(stream)); } FILE* ztd::popen2(const char* command, const char* type, int* pid)