1 | #ifndef TRITON_TOOLS_SYS_EXEC_HPP |
---|---|
2 | #define TRITON_TOOLS_SYS_EXEC_HPP |
3 | |
4 | #include <cstdio> |
5 | #include <iostream> |
6 | #include <memory> |
7 | #include <stdexcept> |
8 | #include <string> |
9 | |
10 | namespace triton |
11 | { |
12 | namespace tools |
13 | { |
14 | |
15 | |
16 | #ifdef _WIN32 |
17 | #define popen _popen |
18 | #define pclose _pclose |
19 | #endif |
20 | |
21 | #ifndef WEXITSTATUS |
22 | #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) & 255) |
23 | #endif |
24 | |
25 | int exec(const std::string& cmd, std::string& result) { |
26 | char buffer[128]; |
27 | FILE* pipe = popen(cmd.c_str(), "r"); |
28 | if (!pipe) |
29 | return 0; |
30 | result.clear(); |
31 | try { |
32 | while (fgets(buffer, sizeof buffer, pipe) != NULL) |
33 | result += buffer; |
34 | } catch (...) { |
35 | pclose(pipe); |
36 | return 0; |
37 | } |
38 | int status = pclose(pipe); |
39 | return WEXITSTATUS(status); |
40 | |
41 | } |
42 | |
43 | } |
44 | } |
45 | |
46 | #endif |
47 |