aboutsummaryrefslogtreecommitdiff
path: root/src/Exec.cpp
blob: b245fd45e8660325610dddc89c0c4e4d87811899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "../include/Exec.hpp"

namespace sibs
{
    Result<ExecResult> exec(const char *cmd, bool print)
    {
        char buffer[128];
        std::string result;
        FILE *pipe = popen(cmd, "r");
        if(!pipe)
            return Result<ExecResult>::Err("popen() failed");

        while(!feof(pipe))
        {
            if(fgets(buffer, 128, pipe))
            {
                result += buffer;
                if(print)
                    printf("%s", buffer);
            }
        }

        ExecResult execResult;
        execResult.execStdout = result;
        execResult.exitCode = WEXITSTATUS(pclose(pipe));
        return Result<ExecResult>::Ok(execResult);
    }
}