home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / CLIBSRC2.ZIP / SYSTEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  3.4 KB  |  134 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - system.c
  3.  *
  4.  * function(s)
  5.  *    system - issues an MS-DOS command
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <dos.h>
  18. #include <stdlib.h>
  19. #include <_process.h>
  20. #include <errno.h>
  21. #include <string.h>
  22.  
  23. /*---------------------------------------------------------------------*
  24.  
  25. Name            system - issues an MS-DOS command
  26.  
  27. Usage           int system(const char *command);
  28.  
  29. Prototype       in stdlib.h
  30.  
  31. Description     system invokes the MS-DOS command processor to
  32.                 execute a command given in the string command, as if the command
  33.                 had been typed at the DOS prompt.
  34.  
  35.                 The COMSPEC environment variable is used to find the
  36.                 command processor file, so the file does not need to be in
  37.                 the current directory.
  38.  
  39. Return value    If command is a NULL pointer then system() returns nonzero if
  40.                 a command processor is available.  If command is not a NULL pointer,
  41.                 system() returns zero if the command processor was successfully
  42.                 started.  If an error occurred, a -1 is returned and errno is set to
  43.                 ENOENT, ENOMEM, E2BIG, or ENOEXEC.
  44.  
  45.                 ENOENT  - command processor not found
  46.                 ENOMEM  - not enough memory
  47.                 E2BIG   - argument list too long
  48.                 ENOEXEC - the command processor is not a valid executable
  49.  
  50. *---------------------------------------------------------------------*/
  51. int _FARFUNC system(const char _FAR *cmd)
  52. {
  53.     register char   *cmdP;
  54.     int             cmdS;
  55.  
  56.     register char   *envP;
  57.     void            *envSave;
  58.     char            *pathP;
  59.     int             rc;
  60.  
  61. //  Check whether user just wants to test if command processor is available.
  62. //
  63.     if (cmd == NULL)
  64.         {
  65.         if ((pathP = getenv("COMSPEC")) == NULL)
  66.             {
  67.             errno = ENOENT;
  68.             return 0;
  69.             }
  70.         else
  71.             return 1;
  72.         }
  73.  
  74. //  Get COMMAND.COM path
  75. //
  76.     if ((pathP = getenv("COMSPEC")) == NULL)
  77.         {
  78.         errno = ENOENT;
  79.         return (-1);
  80.         }
  81.  
  82. //  Build command line
  83. //
  84.     cmdS = 1 + 3 + strlen(cmd) + 1;
  85.     if (cmdS > 128)
  86.         {
  87.         errno = E2BIG;
  88.         return (-1);
  89.         }
  90.     if ((cmdP = malloc(cmdS)) == NULL)
  91.         {
  92.         errno = ENOMEM;
  93.         return (-1);
  94.         }
  95.  
  96.     if (cmdS == 5)
  97.         {
  98.         cmdP[0] = 0;
  99.         cmdP[1] = '\r';
  100.         }
  101.     else
  102.         {
  103.         *cmdP++ = cmdS - 2;
  104.         *cmdP++ = getswitchar();
  105.         cmdP = _stpcpy(cmdP, "c ");
  106.         cmdP = _stpcpy(cmdP, cmd);
  107.         *cmdP++ = '\r';
  108.         cmdP -= cmdS;
  109.         }
  110.  
  111. //  Build environment
  112. //
  113.     if ((envP = __DOSenv(environ, pathP, &envSave)) == NULL)
  114.         {
  115.         errno = ENOMEM;
  116.         free(cmdP);
  117.         return (-1);
  118.         }
  119.  
  120. //  Flush all byte streams
  121. //
  122.     (*_exitbuf)();
  123.  
  124. //  Now, call the low level _spawn function
  125. //
  126.     rc = _spawn(pathP, cmdP, envP);
  127.  
  128. //  Release all buffers
  129. //
  130.     free(envSave);
  131.     free(cmdP);
  132.     return ((rc == -1) ? -1 : 0);
  133. }
  134.