/PC/w9xpopen.c

http://unladen-swallow.googlecode.com/ · C · 112 lines · 76 code · 4 blank · 32 comment · 10 complexity · f8e123978233bbfc00f4ad6a728416d2 MD5 · raw file

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