-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.h
41 lines (40 loc) · 1.02 KB
/
shell.h
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
//
// Created by Curie on 2019/9/17.
//
#ifndef APPTOOLS_SHELL_H
#define APPTOOLS_SHELL_H
#include <cstdlib>
#include <string>
#include <cstdio>
#include "log.h"
namespace Shell{
const char * TAG = "Shell";
const unsigned RESULT_OK = 0;
bool exec(const char * script){
return system(script) == RESULT_OK;
}
char * execWith(const char * script){
if(script == nullptr){
Log::e(TAG,"script is null");
return nullptr;
}
string result;
FILE * file;
const int max_buffer_size = 256*2;
char buffer[max_buffer_size];
file = popen(script,"r");
if(file){
while(!feof(file)){
if(fgets(buffer,max_buffer_size,file) != nullptr){
result.append(buffer);
}
}
pclose(file);
return const_cast<char *>(result.c_str());
} else{
Log::e(TAG,"can not exec script");
}
return nullptr;
}
}
#endif //APPTOOLS_SHELL_H