Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.3 KiB

  1. /***
  2. *system.c - pass a command line to the shell
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines system() - passes a command to the shell
  8. *
  9. *Revision History:
  10. * 12-01-83 RN written
  11. * 10-23-86 SKS Fixed use of strtok(), added check for NULL rtn from getenv
  12. * 12-18-86 SKS PROTMODE symbol used for dual-modal version
  13. * 02-23-86 JCR Put in support for NULL command pointer (MSDOS only)
  14. * 04-13-86 JCR Added const to declaration
  15. * 06-30-87 JCR Re-wrote system to use spawnvpe, removed XENIX conditional
  16. * code, lots of general cleaning up.
  17. * 07-01-87 PHG removed P->PROTMODE compile switch hack
  18. * 09-22-87 SKS remove extern variable declarations, add ";" to assert()'s
  19. * 11-10-87 SKS Removed IBMC20 switch, change PROTMODE to OS2
  20. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  21. * 02-22-88 JCR Added cast to get rid of cl const warning
  22. * 09-05-88 SKS Treat EACCES the same as ENOENT -- keep trying
  23. * 03-08-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  24. * <cruntime.h>, removed some leftover DOS support and
  25. * fixed the copyright. Also, cleaned up the formatting
  26. * formatting a bit.
  27. * 07-23-90 SBM Compiles cleanly with -W3 (removed unreferenced
  28. * variable), removed redundant includes, replaced
  29. * <assertm.h> by <assert.h>, minor optimizations
  30. * 09-27-90 GJF New-style function declarator.
  31. * 01-17-91 GJF ANSI naming.
  32. * 02-14-90 SRW Use NULL instead of _environ to get default.
  33. * 02-23-93 SKS Remove reference to _osmode and use of "command.com"
  34. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  35. * 12-07-93 CFW Wide char enable.
  36. * 12-06-94 SKS Assume command.com for Win95, but cmd.exe for Win. NT.
  37. * 01-16-95 SKS Avoid calling access(NULL) if command==NULL and
  38. * %COMSPEC% is unset.
  39. * 02-06-95 CFW assert -> _ASSERTE.
  40. * 02-06-98 GJF Changes for Win64: added cast to int where necessary
  41. *
  42. *******************************************************************************/
  43. #include <cruntime.h>
  44. #include <process.h>
  45. #include <io.h>
  46. #include <stdlib.h>
  47. #include <errno.h>
  48. #include <tchar.h>
  49. #include <dbgint.h>
  50. /***
  51. *int system(command) - send the command line to a shell
  52. *
  53. *Purpose:
  54. * Executes a shell and passes the command line to it.
  55. * If command is NULL, determine if a command processor exists.
  56. * The command processor is described by the environment variable
  57. * COMSPEC. If that environment variable does not exist, try the
  58. * name "cmd.exe" for Windows NT and "command.com" for Windows '95.
  59. *
  60. *Entry:
  61. * char *command - command to pass to the shell (if NULL, just determine
  62. * if command processor exists)
  63. *
  64. *Exit:
  65. * if command != NULL returns status of the shell
  66. * if command == NULL returns non-zero if CP exists, zero if CP doesn't exist
  67. *
  68. *Exceptions:
  69. *
  70. *******************************************************************************/
  71. int __cdecl _tsystem (
  72. const _TSCHAR *command
  73. )
  74. {
  75. int catch;
  76. _TSCHAR *argv[4];
  77. argv[0] = _tgetenv(_T("COMSPEC"));
  78. /*
  79. * If command == NULL, return true IFF %COMSPEC%
  80. * is set AND the file it points to exists.
  81. */
  82. if (command == NULL) {
  83. return argv[0] == NULL ? 0 : (!_taccess(argv[0],0));
  84. }
  85. _ASSERTE(*command != _T('\0'));
  86. argv[1] = _T("/c");
  87. argv[2] = (_TSCHAR *) command;
  88. argv[3] = NULL;
  89. /* If there is a COMSPEC defined, try spawning the shell */
  90. if (argv[0]) /* Do not try to spawn the null string */
  91. if ((catch = (int)_tspawnve(_P_WAIT,argv[0],argv,NULL)) != -1
  92. || (errno != ENOENT && errno != EACCES))
  93. return(catch);
  94. /* No COMSPEC so set argv[0] to what COMSPEC should be. */
  95. argv[0] = ( _osver & 0x8000 ) ? _T("command.com") : _T("cmd.exe");
  96. /* Let the _spawnvpe routine do the path search and spawn. */
  97. return((int)_tspawnvpe(_P_WAIT,argv[0],argv,NULL));
  98. }