-
Notifications
You must be signed in to change notification settings - Fork 1
/
global.cpp
41 lines (35 loc) · 1000 Bytes
/
global.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
#include "global.h"
//------------------------------------------------------
//Classes
//------------------------------------------------------
Dietpi_Core cDietPi_Core;
Dietpi_Software cDietPi_Software;
Web_Dash cWeb_Dash;
//------------------------------------------------------
//Global functions
//------------------------------------------------------
string exec(const char* cmd) {
int iBufferSize = 128;
char buffer[iBufferSize];
string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, iBufferSize, pipe) != NULL)
{
result += buffer;
}
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
//Remove new lines from end of string
if ( ! result.empty() && result[result.length()-1] == '\n')
{
result.erase(result.length()-1);
}
return result;
}