| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#define STRICT |
| 16 |
|
| 17 |
#include <windows.h> |
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
static HANDLE CreateTempFile(void); |
| 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 |
int |
| 59 |
main() |
| 60 |
{ |
| 61 |
DWORD dwRead, dwWrite; |
| 62 |
char *cmdLine; |
| 63 |
HANDLE hStdInput, hStdOutput, hStdError; |
| 64 |
HANDLE hFileInput, hFileOutput, hFileError; |
| 65 |
STARTUPINFO si; |
| 66 |
PROCESS_INFORMATION pi; |
| 67 |
char buf[8192]; |
| 68 |
DWORD result; |
| 69 |
|
| 70 |
hFileInput = INVALID_HANDLE_VALUE; |
| 71 |
hFileOutput = INVALID_HANDLE_VALUE; |
| 72 |
hFileError = INVALID_HANDLE_VALUE; |
| 73 |
result = 1; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
cmdLine = strchr(GetCommandLine(), ' '); |
| 86 |
if (cmdLine == NULL) { |
| 87 |
return 1; |
| 88 |
} |
| 89 |
cmdLine++; |
| 90 |
|
| 91 |
hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 92 |
hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 93 |
hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 94 |
|
| 95 |
if (GetFileType(hStdInput) == FILE_TYPE_PIPE) { |
| 96 |
hFileInput = CreateTempFile(); |
| 97 |
if (hFileInput == INVALID_HANDLE_VALUE) { |
| 98 |
goto cleanup; |
| 99 |
} |
| 100 |
while (ReadFile(hStdInput, buf, sizeof(buf), &dwRead, NULL) != FALSE) { |
| 101 |
if (dwRead == 0) { |
| 102 |
break; |
| 103 |
} |
| 104 |
if (WriteFile(hFileInput, buf, dwRead, &dwWrite, NULL) == FALSE) { |
| 105 |
goto cleanup; |
| 106 |
} |
| 107 |
} |
| 108 |
SetFilePointer(hFileInput, 0, 0, FILE_BEGIN); |
| 109 |
SetStdHandle(STD_INPUT_HANDLE, hFileInput); |
| 110 |
} |
| 111 |
if (GetFileType(hStdOutput) == FILE_TYPE_PIPE) { |
| 112 |
hFileOutput = CreateTempFile(); |
| 113 |
if (hFileOutput == INVALID_HANDLE_VALUE) { |
| 114 |
goto cleanup; |
| 115 |
} |
| 116 |
SetStdHandle(STD_OUTPUT_HANDLE, hFileOutput); |
| 117 |
} |
| 118 |
if (GetFileType(hStdError) == FILE_TYPE_PIPE) { |
| 119 |
hFileError = CreateTempFile(); |
| 120 |
if (hFileError == INVALID_HANDLE_VALUE) { |
| 121 |
goto cleanup; |
| 122 |
} |
| 123 |
SetStdHandle(STD_ERROR_HANDLE, hFileError); |
| 124 |
} |
| 125 |
|
| 126 |
ZeroMemory(&si, sizeof(si)); |
| 127 |
si.cb = sizeof(si); |
| 128 |
if (CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, |
| 129 |
&pi) == FALSE) { |
| 130 |
goto cleanup; |
| 131 |
} |
| 132 |
|
| 133 |
WaitForInputIdle(pi.hProcess, 5000); |
| 134 |
WaitForSingleObject(pi.hProcess, INFINITE); |
| 135 |
GetExitCodeProcess(pi.hProcess, &result); |
| 136 |
CloseHandle(pi.hProcess); |
| 137 |
CloseHandle(pi.hThread); |
| 138 |
|
| 139 |
if (hFileOutput != INVALID_HANDLE_VALUE) { |
| 140 |
SetFilePointer(hFileOutput, 0, 0, FILE_BEGIN); |
| 141 |
while (ReadFile(hFileOutput, buf, sizeof(buf), &dwRead, NULL) != FALSE) { |
| 142 |
if (dwRead == 0) { |
| 143 |
break; |
| 144 |
} |
| 145 |
if (WriteFile(hStdOutput, buf, dwRead, &dwWrite, NULL) == FALSE) { |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
if (hFileError != INVALID_HANDLE_VALUE) { |
| 151 |
SetFilePointer(hFileError, 0, 0, FILE_BEGIN); |
| 152 |
while (ReadFile(hFileError, buf, sizeof(buf), &dwRead, NULL) != FALSE) { |
| 153 |
if (dwRead == 0) { |
| 154 |
break; |
| 155 |
} |
| 156 |
if (WriteFile(hStdError, buf, dwRead, &dwWrite, NULL) == FALSE) { |
| 157 |
break; |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
cleanup: |
| 163 |
if (hFileInput != INVALID_HANDLE_VALUE) { |
| 164 |
CloseHandle(hFileInput); |
| 165 |
} |
| 166 |
if (hFileOutput != INVALID_HANDLE_VALUE) { |
| 167 |
CloseHandle(hFileOutput); |
| 168 |
} |
| 169 |
if (hFileError != INVALID_HANDLE_VALUE) { |
| 170 |
CloseHandle(hFileError); |
| 171 |
} |
| 172 |
CloseHandle(hStdInput); |
| 173 |
CloseHandle(hStdOutput); |
| 174 |
CloseHandle(hStdError); |
| 175 |
ExitProcess(result); |
| 176 |
return 1; |
| 177 |
} |
| 178 |
|
| 179 |
static HANDLE |
| 180 |
CreateTempFile() |
| 181 |
{ |
| 182 |
char name[MAX_PATH]; |
| 183 |
SECURITY_ATTRIBUTES sa; |
| 184 |
|
| 185 |
if (GetTempPath(sizeof(name), name) == 0) { |
| 186 |
return INVALID_HANDLE_VALUE; |
| 187 |
} |
| 188 |
if (GetTempFileName(name, "tcl", 0, name) == 0) { |
| 189 |
return INVALID_HANDLE_VALUE; |
| 190 |
} |
| 191 |
|
| 192 |
sa.nLength = sizeof(sa); |
| 193 |
sa.lpSecurityDescriptor = NULL; |
| 194 |
sa.bInheritHandle = TRUE; |
| 195 |
return CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, &sa, |
| 196 |
CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, |
| 197 |
NULL); |
| 198 |
} |