home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / source code / Dos / system.c < prev   
Encoding:
C/C++ Source or Header  |  1995-12-17  |  3.0 KB  |  104 lines  |  [TEXT/R*ch]

  1. /*---------------------------------------------------------------------------*
  2.  * Emulation of system() under Windows.  (MS QuickWin)
  3.  *
  4.  * Copyright 1994, David Gottner
  5.  *
  6.  *                    All Rights Reserved
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice, this permission notice and
  11.  * the following disclaimer notice appear unmodified in all copies.
  12.  *
  13.  * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL I
  15.  * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
  16.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
  17.  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  18.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Nevertheless, I would like to know about bugs in this library or
  21.  * suggestions for improvment.  Send bug reports and feedback to
  22.  * davegottner@delphi.com.
  23.  *---------------------------------------------------------------------------*/
  24.  
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28.  
  29. #ifdef _WINDOWS
  30. #include <windows.h>
  31. #include <toolhelp.h>
  32.  
  33.  
  34. /*---------------------------------------------------------------------------*
  35.  * IMPORTANT NOTE:
  36.  *     Even though this is a QuickWin app, you must compile this module
  37.  *     with the flags -GA -GEd in place of -Mq, so that the callback loads
  38.  *     the data segment correctly.
  39.  *
  40.  * These variables are set by the callback notification function
  41.  * and accessed by system()
  42.  */
  43. static BOOL taskRunning   = FALSE;
  44. static int  taskExitValue = 0;
  45.  
  46.  
  47.  
  48. BOOL CALLBACK __export ChildProcNotifiee(WORD reason, DWORD info)
  49. {
  50.     if (reason == NFY_EXITTASK) {
  51.         taskRunning = FALSE;
  52.         taskExitValue = LOWORD(info);
  53.     }
  54.  
  55.     return FALSE;
  56. }
  57.  
  58.  
  59.  
  60. int system(const char *s)
  61. {
  62.     TASKENTRY    taskinfo;
  63.     HINSTANCE    ourInstance;
  64.     HTASK        ourTask;
  65.     BOOL        task_found;
  66.     FARPROC        notify_cb;
  67.  
  68.     ourInstance = WinExec(s, SW_SHOWNORMAL);
  69.     if (ourInstance < HINSTANCE_ERROR)
  70.         return ((int) ourInstance) + 1;
  71.  
  72.     /*** Get the TASK corresponding with this instance ***/
  73.     taskinfo.dwSize = sizeof(TASKENTRY);
  74.     task_found = TaskFirst(&taskinfo);
  75.     while (task_found) {
  76.         if (taskinfo.hInst == ourInstance) {
  77.             ourTask = taskinfo.hTask;
  78.             break;
  79.         }
  80.  
  81.         task_found = TaskNext(&taskinfo);
  82.     }
  83.  
  84.     /*** Install the notify callback so we can watch the child ***/
  85.     taskRunning = TRUE;
  86.     if (!task_found || !NotifyRegister(ourTask, ChildProcNotifiee, NF_NORMAL)) {
  87.         MessageBox(NULL,
  88.                     "A notify callback could not be installed."
  89.                     "  This is likely a problem with the DOS module.",
  90.                     "Internal Error",
  91.                     MB_TASKMODAL | MB_ICONEXCLAMATION | MB_OK);
  92.         TerminateApp(ourTask, NO_UAE_BOX);
  93.         taskRunning = FALSE;
  94.     }
  95.  
  96.     /*** Wait for completion; child will update taskRunning & taskExitValue ***/
  97.     while (taskRunning)
  98.         _wyield();
  99.  
  100.     return taskExitValue << 8;
  101. }
  102.  
  103. #endif /* _WINDOWS */
  104.