shell: added shp

This commit is contained in:
zawz 2019-11-05 09:08:57 +01:00
parent 020ee3bb2b
commit 4423eccdf8
2 changed files with 15 additions and 3 deletions

View file

@ -2,6 +2,7 @@
#define SHELL_HPP
#include <string>
#include <utility>
#include <stdio.h>
/*! \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 <a href="https://linux.die.net/man/3/systeml">system(), pclose()</a>
*/
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<std::string, int> 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

View file

@ -8,6 +8,11 @@
#include <signal.h>
std::string ztd::sh(const std::string& command, bool to_console)
{
return ztd::shp(command, to_console).first;
}
std::pair<std::string, int> 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)