-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_system.cpp
117 lines (105 loc) · 2.7 KB
/
my_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
114
115
116
117
#ifdef _WIN32
#include <windows.h>
#include <winuser.h>
#include <string>
#include <stdio.h>
unsigned int my_system(const char *cmd,char *result,int maxLen){
PROCESS_INFORMATION pi;
STARTUPINFO siStartInfo;
SECURITY_ATTRIBUTES saAttr;
std::string Output, tmp;
char command_line[2048];
DWORD dwRead;
char* buf; int len;
HANDLE hRead, hWrite;
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&hRead, &hWrite, &saAttr, 0))
{
return 0;
}
memset(&pi, 0, sizeof(pi));
sprintf(command_line, "wspawn -h %d %s", (unsigned int)hWrite,cmd);
ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
siStartInfo.cb = sizeof(STARTUPINFO);
siStartInfo.wShowWindow = SW_HIDE;
siStartInfo.dwFlags = STARTF_USESHOWWINDOW;
if (!CreateProcess( NULL, command_line, NULL, NULL, TRUE,
0, NULL, NULL, &siStartInfo, &pi))
{
return 0;
}
if(!ReadFile( hRead, &len, sizeof(int), &dwRead, NULL) || dwRead == 0)
return 0;
while(len)
{
buf = new char[len + 1];
memset(buf, 0, len + 1);
if(!ReadFile( hRead, buf, len, &dwRead, NULL) || dwRead == 0)
return 0;
tmp = buf;
//tmp.Replace("\n", "\r\n");
Output += tmp;
delete[] buf;
if(!ReadFile( hRead, &len, sizeof(int), &dwRead, NULL) || dwRead == 0)
return 0;
}
//printf("%d \n%s\n",Output.length(),Output.c_str());
_snprintf(result,maxLen,"%s",Output.c_str());
WaitForSingleObject(pi.hProcess, 30000);
CloseHandle(hRead);
CloseHandle(hWrite);
return Output.size();
}
#else
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
unsigned int my_system(const char* pCmd, char* pResult, int size) {
int fd[2];
int pid;
int count;
int left;
char* p = 0;
int maxlen = size - 1;
memset(pResult, 0, size);
if(pipe(fd))
{
printf("pipe error\n");
return 0;
}
if((pid = fork()) == 0)
{// chile process
int fd2[2];
if(pipe(fd2))
{
printf("pipe2 error\n");
return 0;
}
close(1);
dup2(fd2[1],1);
close(fd[0]);
close(fd2[1]);
system(pCmd);
read(fd2[0], pResult, maxlen);
pResult[strlen(pResult)-1] = 0;
write(fd[1], pResult, strlen(pResult));
close(fd2[0]);
exit(0);
}
// parent process
close(fd[1]);
p = pResult;
left = maxlen;
while((count = read(fd[0], p, left))) {
p += count;
left -= count;
if(left == 0)
break;
}
close(fd[0]);
return 1;
}
#endif