| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#define WIN32_LEAN_AND_MEAN |
| 18 |
#include <windows.h> |
| 19 |
#include <stdio.h> |
| 20 |
#include <stdlib.h> |
| 21 |
|
| 22 |
const char *usage = |
| 23 |
"This program is used by Python's os.popen function\n" |
| 24 |
"to work around a limitation in Windows 95/98. It is\n" |
| 25 |
"not designed to be used as a stand-alone program."; |
| 26 |
|
| 27 |
int main(int argc, char *argv[]) |
| 28 |
{ |
| 29 |
BOOL bRet; |
| 30 |
STARTUPINFO si; |
| 31 |
PROCESS_INFORMATION pi; |
| 32 |
DWORD exit_code=0; |
| 33 |
size_t cmdlen = 0; |
| 34 |
int i; |
| 35 |
char *cmdline, *cmdlinefill; |
| 36 |
|
| 37 |
if (argc < 2) { |
| 38 |
if (GetFileType(GetStdHandle(STD_INPUT_HANDLE))==FILE_TYPE_CHAR) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
MessageBox(NULL, usage, argv[0], MB_OK); |
| 43 |
else { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
fprintf(stdout, "Internal popen error - no args specified\n%s\n", usage); |
| 51 |
} |
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
for (i=1;i<argc;i++) |
| 63 |
cmdlen += strlen(argv[i])*2 + 3; |
| 64 |
cmdline = cmdlinefill = (char *)malloc(cmdlen+1); |
| 65 |
if (cmdline == NULL) |
| 66 |
return -1; |
| 67 |
for (i=1;i<argc;i++) { |
| 68 |
const char *arglook; |
| 69 |
int bQuote = strchr(argv[i], ' ') != NULL; |
| 70 |
if (bQuote) |
| 71 |
*cmdlinefill++ = '"'; |
| 72 |
|
| 73 |
for (arglook=argv[i];*arglook;arglook++) { |
| 74 |
if (*arglook=='"') |
| 75 |
*cmdlinefill++ = '\\'; |
| 76 |
*cmdlinefill++ = *arglook; |
| 77 |
} |
| 78 |
if (bQuote) |
| 79 |
*cmdlinefill++ = '"'; |
| 80 |
*cmdlinefill++ = ' '; |
| 81 |
} |
| 82 |
*cmdlinefill = '\0'; |
| 83 |
|
| 84 |
|
| 85 |
ZeroMemory(&si, sizeof si); |
| 86 |
si.cb = sizeof si; |
| 87 |
si.dwFlags = STARTF_USESTDHANDLES; |
| 88 |
si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
| 89 |
si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
| 90 |
si.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
| 91 |
|
| 92 |
bRet = CreateProcess( |
| 93 |
NULL, cmdline, |
| 94 |
NULL, NULL, |
| 95 |
TRUE, 0, |
| 96 |
NULL, NULL, |
| 97 |
&si, &pi |
| 98 |
); |
| 99 |
|
| 100 |
free(cmdline); |
| 101 |
|
| 102 |
if (bRet) { |
| 103 |
if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) { |
| 104 |
GetExitCodeProcess(pi.hProcess, &exit_code); |
| 105 |
} |
| 106 |
CloseHandle(pi.hProcess); |
| 107 |
CloseHandle(pi.hThread); |
| 108 |
return exit_code; |
| 109 |
} |
| 110 |
|
| 111 |
return 1; |
| 112 |
} |