-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.cpp
113 lines (83 loc) · 3.87 KB
/
system.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdlib.h>
#include <string>
#include "system.hpp"
#include "../print/pdbg.hpp"
#include "../file/file.hpp"
//-----------------------------------------------------------------------------------------------------------------------------------------------
int sstd::system(const char* cmd){ return ::system(cmd ); }
int sstd::system(const std::string& cmd){ return ::system(cmd.c_str()); }
//---
int sstd::system_stdout(std::string& ret, const char* cmd){
std::string cmd_out = cmd + std::string(" >&1 2>/dev/null"); // >&1: get stdout, throw away stderr.
sstd::file fp;
if(!fp.popen(cmd_out.c_str(), "r")){ sstd::pdbg_err("sstd::file::popen() was failed.\n"); return -1; }
char buf[1024];
while(fp.fgets(buf, 1024) != NULL){ ret+=buf; }
return fp.close();
}
int sstd::system_stdout(std::string& ret, const std::string& cmd){
return sstd::system_stdout(ret, cmd.c_str());
}
//---
int sstd::system_stderr(std::string& ret, const char* cmd){
std::string cmd_out = cmd + std::string(" 2>&1 1>/dev/null"); // >&1: get stdout, throw away stderr.
sstd::file fp;
if(!fp.popen(cmd_out.c_str(), "r")){ sstd::pdbg_err("sstd::file::popen() was failed.\n"); return -1; }
char buf[1024];
while(fp.fgets(buf, 1024) != NULL){ ret+=buf; }
return fp.close();
}
int sstd::system_stderr(std::string& ret, const std::string& cmd){
return sstd::system_stderr(ret, cmd.c_str());
}
//---
int sstd::system_stdout_stderr(std::string& ret, const char* cmd){
std::string cmd_out_err = cmd + std::string(R"( 2>&1)"); // 2>&1: redirecting stderr to stdout.
sstd::file fp;
if(!fp.popen(cmd_out_err.c_str(), "r")){ sstd::pdbg_err("sstd::file::popen() was failed.\n"); return -1; }
char buf[1024];
while(fp.fgets(buf, 1024) != NULL){ ret+=buf; }
return fp.close();
}
int sstd::system_stdout_stderr(std::string& ret, const std::string& cmd){
return sstd::system_stdout_stderr(ret, cmd.c_str());
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
// Below functions are the not recommended to use. (Below functions will be delete on sstd ver 3.x.x).
std::string sstd::system_stdout(const char* cmd){
std::string cmd_out = cmd + std::string(" >&1 2>/dev/null"); // >&1: get stdout, throw away stderr.
sstd::file fp;
if(!fp.popen(cmd_out.c_str(), "r")){ sstd::pdbg_err("sstd::file::popen() was failed.\n"); return ""; }
std::string ret;
char buf[1024];
while(fp.fgets(buf, 1024) != NULL){ ret+=buf; }
return ret;
}
std::string sstd::system_stdout(const std::string& cmd){
return sstd::system_stdout(cmd.c_str());
}
std::string sstd::system_stderr(const char* cmd){
std::string cmd_out = cmd + std::string(" 2>&1 1>/dev/null"); // >&1: get stdout, throw away stderr.
sstd::file fp;
if(!fp.popen(cmd_out.c_str(), "r")){ sstd::pdbg_err("sstd::file::popen() was failed.\n"); return ""; }
std::string ret;
char buf[1024];
while(fp.fgets(buf, 1024) != NULL){ ret+=buf; }
return ret;
}
std::string sstd::system_stderr(const std::string& cmd){
return sstd::system_stderr(cmd.c_str());
}
std::string sstd::system_stdout_stderr(const char* cmd){
std::string cmd_out_err = cmd + std::string(R"( 2>&1)"); // 2>&1: redirecting stderr to stdout.
sstd::file fp;
if(!fp.popen(cmd_out_err.c_str(), "r")){ sstd::pdbg_err("sstd::file::popen() was failed.\n"); return ""; }
std::string ret;
char buf[1024];
while(fp.fgets(buf, 1024) != NULL){ ret+=buf; }
return ret;
}
std::string sstd::system_stdout_stderr(const std::string& cmd){
return sstd::system_stdout_stderr(cmd.c_str());
}
//-----------------------------------------------------------------------------------------------------------------------------------------------