home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / SYSTEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.6 KB  |  108 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - system.c
  3.  *
  4.  * function(s)
  5.  *      system - issues an MS-DOS command
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18.  
  19. #include <dos.h>
  20. #include <stdlib.h>
  21. #include <_process.h>
  22. #include <errno.h>
  23. #include <string.h>
  24.  
  25. /*---------------------------------------------------------------------*
  26.  
  27. Name        system - issues an MS-DOS command
  28.  
  29. Usage        int system(const char *command);
  30.  
  31. Prototype in    stdlib.h
  32.  
  33. Description    system invokes the MS-DOS command processor to
  34.         execute a command given in the string command, as if the command
  35.         had been typed at the DOS prompt.
  36.  
  37.         The COMSPEC environment variable is used to find the
  38.         command processor file, so the file does not need to be in
  39.         the current directory.
  40.  
  41. Return value    system returns the exit status of COMMAND.COM when
  42.         the given command is completed.
  43.  
  44. *---------------------------------------------------------------------*/
  45. int    system(const char *cmd)
  46. {
  47.     register char    *cmdP;
  48.     int        cmdS;
  49.     register char    *envP;
  50.     void        *envSave;
  51.     char        *pathP;
  52.     int        rc;
  53.  
  54. /*    Get COMMAND.COM path
  55. */
  56.     if ((pathP = getenv("COMSPEC")) == NULL)
  57.     {
  58.         errno = ENOENT;
  59.         return -1;
  60.     }
  61.  
  62. /*    Build command line
  63. */
  64.     cmdS = 1 + 3 + strlen(cmd) + 1;
  65.     if (cmdS > 128 || (cmdP = malloc(cmdS)) == NULL)
  66.     {
  67.         errno = ENOMEM;
  68.         return (-1);
  69.     }
  70.  
  71.     if (cmdS == 5)
  72.     {
  73.         cmdP[0] = 0;
  74.         cmdP[1] = '\r';
  75.     }
  76.     else
  77.     {
  78.         *cmdP++ = cmdS - 2;
  79.         *cmdP++ = getswitchar();
  80.         cmdP = stpcpy(cmdP, "c ");
  81.         cmdP = stpcpy(cmdP, cmd);
  82.         *cmdP++ = '\r';
  83.         cmdP -= cmdS;
  84.     }
  85.  
  86. /*    Build environment
  87. */
  88.     if ((envP = __DOSenv(environ, pathP, &envSave)) == NULL) {
  89.         errno = ENOMEM;
  90.         free(cmdP);
  91.         return (-1);
  92.     }
  93.  
  94. /*    Flush all byte streams
  95. */
  96.     (*_exitbuf)();
  97.  
  98. /*    Now, call the low level _spawn function
  99. */
  100.     rc = _spawn(pathP, cmdP, envP);
  101.  
  102. /*    Release all buffers
  103. */
  104.     free(envSave);
  105.     free(cmdP);
  106.     return (rc);
  107. }
  108.