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

  1. /***
  2. *spawnle.c - spawn a child process with given environment
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _spawnle() - spawn a child process with given environ
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stddef.h>
  13. #include <process.h>
  14. #include <stdarg.h>
  15. #include <internal.h>
  16. #include <malloc.h>
  17. #include <tchar.h>
  18. #include <dbgint.h>
  19.  
  20. /***
  21. *int _spawnle(modeflag, pathname, arglist) - spawn a child process with env.
  22. *
  23. *Purpose:
  24. *       Spawns a child process with given parameters and environment.
  25. *       formats the parameters and calls _spawnve to do the actual work.
  26. *       NOTE - at least one argument must be present.  This argument is always,
  27. *       by convention, the name of the file being spawned.
  28. *
  29. *Entry:
  30. *       int modeflag   - mode of spawn (WAIT, NOWAIT, OVERLAY)
  31. *                        only WAIT, and OVERLAY currently implemented
  32. *       _TSCHAR *pathname - name of file to spawn
  33. *       _TSCHAR *arglist  - argument list, environment is at the end
  34. *       call as _spawnle(modeflag, path, arg0, arg1, ..., argn, NULL, envp);
  35. *
  36. *Exit:
  37. *       returns exit code of spawned process
  38. *       if fails, return -1
  39. *
  40. *Exceptions:
  41. *
  42. *******************************************************************************/
  43.  
  44. int __cdecl _tspawnle (
  45.         int modeflag,
  46.         const _TSCHAR *pathname,
  47.         const _TSCHAR *arglist,
  48.         ...
  49.         )
  50. {
  51. #ifdef _M_IX86
  52.  
  53.         REG1 const _TSCHAR **argp;
  54.  
  55.         _ASSERTE(pathname != NULL);
  56.         _ASSERTE(*pathname != _T('\0'));
  57.         _ASSERTE(arglist != NULL);
  58.         _ASSERTE(*arglist != _T('\0'));
  59.  
  60.         /* walk the arglist until the terminating NULL pointer is found.  The
  61.          * next location holds the environment table pointer.
  62.          */
  63.  
  64.         argp = &arglist;
  65.         while (*argp++)
  66.                 ;
  67.  
  68.         return(_tspawnve(modeflag,pathname,&arglist,(_TSCHAR **)*argp));
  69.  
  70. #else  /* _M_IX86 */
  71.  
  72.         va_list vargs;
  73.         _TSCHAR * argbuf[64];
  74.         _TSCHAR ** argv;
  75.         _TSCHAR ** envp;
  76.         int result;
  77.  
  78.         _ASSERTE(pathname != NULL);
  79.         _ASSERTE(*pathname != _T('\0'));
  80.         _ASSERTE(arglist != NULL);
  81.         _ASSERTE(*arglist != _T('\0'));
  82.  
  83.         va_start(vargs, arglist);
  84. #ifdef WPRFLAG
  85.         argv = _wcapture_argv(&vargs, arglist, argbuf, 64);
  86. #else  /* WPRFLAG */
  87.         argv = _capture_argv(&vargs, arglist, argbuf, 64);
  88. #endif  /* WPRFLAG */
  89.         envp = va_arg(vargs, _TSCHAR **);
  90.         va_end(vargs);
  91.  
  92.         result = _tspawnve(modeflag,pathname,argv,envp);
  93.         if (argv && argv != argbuf)
  94.             _free_crt(argv);
  95.         return result;
  96.  
  97. #endif  /* _M_IX86 */
  98. }
  99.