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

  1. /***
  2. *system.c - pass a command line to the shell
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines system() - passes a command to the shell
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <process.h>
  13. #include <io.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include <tchar.h>
  17. #include <dbgint.h>
  18.  
  19. /***
  20. *int system(command) - send the command line to a shell
  21. *
  22. *Purpose:
  23. *       Executes a shell and passes the command line to it.
  24. *       If command is NULL, determine if a command processor exists.
  25. *       The command processor is described by the environment variable
  26. *       COMSPEC.  If that environment variable does not exist, try the
  27. *       name "cmd.exe" for Windows NT and "command.com" for Windows '95.
  28. *
  29. *Entry:
  30. *       char *command - command to pass to the shell (if NULL, just determine
  31. *                       if command processor exists)
  32. *
  33. *Exit:
  34. *       if command != NULL  returns status of the shell
  35. *       if command == NULL  returns non-zero if CP exists, zero if CP doesn't exist
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. int __cdecl _tsystem (
  42.         const _TSCHAR *command
  43.         )
  44. {
  45.         int catch;
  46.         _TSCHAR *argv[4];
  47.  
  48.         argv[0] = _tgetenv(_T("COMSPEC"));
  49.  
  50.         /*
  51.          * If command == NULL, return true IFF %COMSPEC%
  52.          * is set AND the file it points to exists.
  53.          */
  54.  
  55.         if (command == NULL) {
  56.                 return argv[0] == NULL ? 0 : (!_taccess(argv[0],0));
  57.         }
  58.  
  59.         _ASSERTE(*command != _T('\0'));
  60.  
  61.         argv[1] = _T("/c");
  62.         argv[2] = (_TSCHAR *) command;
  63.         argv[3] = NULL;
  64.  
  65.         /* If there is a COMSPEC defined, try spawning the shell */
  66.  
  67.         if (argv[0])    /* Do not try to spawn the null string */
  68.                 if ((catch = _tspawnve(_P_WAIT,argv[0],argv,NULL)) != -1
  69.                 || (errno != ENOENT && errno != EACCES))
  70.                         return(catch);
  71.  
  72.         /* No COMSPEC so set argv[0] to what COMSPEC should be. */
  73.         argv[0] = ( _osver & 0x8000 ) ? _T("command.com") : _T("cmd.exe");
  74.  
  75.         /* Let the _spawnvpe routine do the path search and spawn. */
  76.  
  77.         return(_tspawnvpe(_P_WAIT,argv[0],argv,NULL));
  78. }
  79.