home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / wait.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  92 lines

  1. /***
  2. *wait.c - wait for child process to terminate
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _wait() - wait for child process to terminate
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <oscalls.h>
  13. #include <process.h>
  14. #include <errno.h>
  15. #include <internal.h>
  16. #include <stdlib.h>
  17.  
  18. /***
  19. *int _cwait(stat_loc, process_id, action_code) - wait for specific child
  20. *       process
  21. *
  22. *Purpose:
  23. *       The function _cwait() suspends the calling-process until the specified
  24. *       child-process terminates.  If the specifed child-process terminated
  25. *       prior to the call to _cwait(), return is immediate.
  26. *
  27. *Entry:
  28. *       int *stat_loc - pointer to where status is stored or NULL
  29. *       process_id - specific process id to be interrogated (0 means any)
  30. *       action_code - specific action to perform on process ID
  31. *                   either _WAIT_CHILD or _WAIT_GRANDCHILD
  32. *
  33. *Exit:
  34. *       process ID of terminated child or -1 on error
  35. *
  36. *       *stat_loc is updated to contain the following:
  37. *       Normal termination: lo-byte = 0, hi-byte = child exit code
  38. *       Abnormal termination: lo-byte = term status, hi-byte = 0
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43.  
  44. int __cdecl _cwait (
  45.         int *stat_loc,
  46.         int process_id,
  47.         int action_code
  48.         )
  49. {
  50.         int retval;
  51.         int retstatus;
  52.         unsigned long oserror;
  53.  
  54.         DBG_UNREFERENCED_PARAMETER(action_code);
  55.  
  56.         /* Explicitly check for process_id being -1 or -2. In Windows NT,
  57.          * -1 is a handle on the current process, -2 is a handle on the
  58.          * current thread, and it is perfectly legal to to wait (forever)
  59.          * on either */
  60.         if ( (process_id == -1) || (process_id == -2) ) {
  61.             errno = ECHILD;
  62.             return -1;
  63.         }
  64.  
  65.         /* wait for child process, then fetch its exit code */
  66.         if ( (WaitForSingleObject((HANDLE)process_id, (DWORD)(-1L)) == 0) &&
  67.           GetExitCodeProcess((HANDLE)process_id, (LPDWORD)&retstatus) ) {
  68.             retval = process_id;
  69.         }
  70.         else {
  71.             /* one of the API calls failed. map the error and set up to
  72.                return failure. note the invalid handle error is mapped in-
  73.                line to ECHILD */
  74.             if ( (oserror = GetLastError()) == ERROR_INVALID_HANDLE ) {
  75.                 errno = ECHILD;
  76.                 _doserrno = oserror;
  77.             }
  78.             else
  79.                 _dosmaperr(GetLastError());
  80.  
  81.             retval = -1;
  82.             retstatus = -1;
  83.         }
  84.  
  85.         CloseHandle((HANDLE)process_id);
  86.  
  87.         if (stat_loc != NULL)
  88.             *stat_loc = retstatus;
  89.  
  90.         return retval;
  91. }
  92.