| 1 | n/a | /* |
|---|
| 2 | n/a | * w9xpopen.c |
|---|
| 3 | n/a | * |
|---|
| 4 | n/a | * Serves as an intermediate stub Win32 console application to |
|---|
| 5 | n/a | * avoid a hanging pipe when redirecting 16-bit console based |
|---|
| 6 | n/a | * programs (including MS-DOS console based programs and batch |
|---|
| 7 | n/a | * files) on Window 95 and Windows 98. |
|---|
| 8 | n/a | * |
|---|
| 9 | n/a | * This program is to be launched with redirected standard |
|---|
| 10 | n/a | * handles. It will launch the command line specified 16-bit |
|---|
| 11 | n/a | * console based application in the same console, forwarding |
|---|
| 12 | n/a | * its own redirected standard handles to the 16-bit child. |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | * AKA solution to the problem described in KB: Q150956. |
|---|
| 15 | n/a | */ |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | #define WIN32_LEAN_AND_MEAN |
|---|
| 18 | n/a | #include <windows.h> |
|---|
| 19 | n/a | #include <stdio.h> |
|---|
| 20 | n/a | #include <stdlib.h> /* for malloc and its friends */ |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | const char *usage = |
|---|
| 23 | n/a | "This program is used by Python's os.popen function\n" |
|---|
| 24 | n/a | "to work around a limitation in Windows 95/98. It is\n" |
|---|
| 25 | n/a | "not designed to be used as a stand-alone program."; |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | int main(int argc, char *argv[]) |
|---|
| 28 | n/a | { |
|---|
| 29 | n/a | BOOL bRet; |
|---|
| 30 | n/a | STARTUPINFO si; |
|---|
| 31 | n/a | PROCESS_INFORMATION pi; |
|---|
| 32 | n/a | DWORD exit_code=0; |
|---|
| 33 | n/a | size_t cmdlen = 0; |
|---|
| 34 | n/a | int i; |
|---|
| 35 | n/a | char *cmdline, *cmdlinefill; |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | if (argc < 2) { |
|---|
| 38 | n/a | if (GetFileType(GetStdHandle(STD_INPUT_HANDLE))==FILE_TYPE_CHAR) |
|---|
| 39 | n/a | /* Attached to a console, and therefore not executed by Python |
|---|
| 40 | n/a | Display a message box for the inquisitive user |
|---|
| 41 | n/a | */ |
|---|
| 42 | n/a | MessageBox(NULL, usage, argv[0], MB_OK); |
|---|
| 43 | n/a | else { |
|---|
| 44 | n/a | /* Eeek - executed by Python, but args are screwed! |
|---|
| 45 | n/a | Write an error message to stdout so there is at |
|---|
| 46 | n/a | least some clue for the end user when it appears |
|---|
| 47 | n/a | in their output. |
|---|
| 48 | n/a | A message box would be hidden and blocks the app. |
|---|
| 49 | n/a | */ |
|---|
| 50 | n/a | fprintf(stdout, "Internal popen error - no args specified\n%s\n", usage); |
|---|
| 51 | n/a | } |
|---|
| 52 | n/a | return 1; |
|---|
| 53 | n/a | } |
|---|
| 54 | n/a | /* Build up the command-line from the args. |
|---|
| 55 | n/a | Args with a space are quoted, existing quotes are escaped. |
|---|
| 56 | n/a | To keep things simple calculating the buffer size, we assume |
|---|
| 57 | n/a | every character is a quote - ie, we allocate double what we need |
|---|
| 58 | n/a | in the worst case. As this is only double the command line passed |
|---|
| 59 | n/a | to us, there is a good chance this is reasonably small, so the total |
|---|
| 60 | n/a | allocation will almost always be < 512 bytes. |
|---|
| 61 | n/a | */ |
|---|
| 62 | n/a | for (i=1;i<argc;i++) |
|---|
| 63 | n/a | cmdlen += strlen(argv[i])*2 + 3; /* one space, maybe 2 quotes */ |
|---|
| 64 | n/a | cmdline = cmdlinefill = (char *)malloc(cmdlen+1); |
|---|
| 65 | n/a | if (cmdline == NULL) |
|---|
| 66 | n/a | return -1; |
|---|
| 67 | n/a | for (i=1;i<argc;i++) { |
|---|
| 68 | n/a | const char *arglook; |
|---|
| 69 | n/a | int bQuote = strchr(argv[i], ' ') != NULL; |
|---|
| 70 | n/a | if (bQuote) |
|---|
| 71 | n/a | *cmdlinefill++ = '"'; |
|---|
| 72 | n/a | /* escape quotes */ |
|---|
| 73 | n/a | for (arglook=argv[i];*arglook;arglook++) { |
|---|
| 74 | n/a | if (*arglook=='"') |
|---|
| 75 | n/a | *cmdlinefill++ = '\\'; |
|---|
| 76 | n/a | *cmdlinefill++ = *arglook; |
|---|
| 77 | n/a | } |
|---|
| 78 | n/a | if (bQuote) |
|---|
| 79 | n/a | *cmdlinefill++ = '"'; |
|---|
| 80 | n/a | *cmdlinefill++ = ' '; |
|---|
| 81 | n/a | } |
|---|
| 82 | n/a | *cmdlinefill = '\0'; |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | /* Make child process use this app's standard files. */ |
|---|
| 85 | n/a | ZeroMemory(&si, sizeof si); |
|---|
| 86 | n/a | si.cb = sizeof si; |
|---|
| 87 | n/a | si.dwFlags = STARTF_USESTDHANDLES; |
|---|
| 88 | n/a | si.hStdInput = GetStdHandle(STD_INPUT_HANDLE); |
|---|
| 89 | n/a | si.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE); |
|---|
| 90 | n/a | si.hStdError = GetStdHandle(STD_ERROR_HANDLE); |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | bRet = CreateProcess( |
|---|
| 93 | n/a | NULL, cmdline, |
|---|
| 94 | n/a | NULL, NULL, |
|---|
| 95 | n/a | TRUE, 0, |
|---|
| 96 | n/a | NULL, NULL, |
|---|
| 97 | n/a | &si, &pi |
|---|
| 98 | n/a | ); |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | free(cmdline); |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | if (bRet) { |
|---|
| 103 | n/a | if (WaitForSingleObject(pi.hProcess, INFINITE) != WAIT_FAILED) { |
|---|
| 104 | n/a | GetExitCodeProcess(pi.hProcess, &exit_code); |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | CloseHandle(pi.hProcess); |
|---|
| 107 | n/a | CloseHandle(pi.hThread); |
|---|
| 108 | n/a | return exit_code; |
|---|
| 109 | n/a | } |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | return 1; |
|---|
| 112 | n/a | } |
|---|