-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.cc
38 lines (26 loc) · 861 Bytes
/
shell.cc
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
#include <array>
#include <memory>
#include <config.h>
#include <journal.h>
#include "shell.h"
namespace sys {
namespace shell {
auto execute( std::string_view cmd ) -> std::string {
using namespace std;
LOG_DEBUG( APP_TAG, "Execute: %1", cmd );
array<char, 512> buffer;
string result;
shared_ptr<FILE> pipe( popen( cmd.data( ), "r" ), pclose );
if ( !pipe )
return result;
while ( !feof( pipe.get( ) ) ) {
if ( fgets( data( buffer ), size( buffer ), pipe.get( ) ) != nullptr )
result += data( buffer );
}
return result;
}
auto run( std::string_view cmd ) -> int {
return system( cmd.data( ) );
}
} // namespace shell
} // namespace sys