home *** CD-ROM | disk | FTP | other *** search
- /*-----------------------------------------------------------------------*
- * filename - system.c
- *
- * function(s)
- * system - issues an MS-DOS command
- *-----------------------------------------------------------------------*/
-
- /*[]------------------------------------------------------------[]*/
- /*| |*/
- /*| Turbo C Run Time Library - Version 3.0 |*/
- /*| |*/
- /*| |*/
- /*| Copyright (c) 1987,1988,1990 by Borland International |*/
- /*| All Rights Reserved. |*/
- /*| |*/
- /*[]------------------------------------------------------------[]*/
-
-
- #include <dos.h>
- #include <stdlib.h>
- #include <_process.h>
- #include <errno.h>
- #include <string.h>
-
- /*---------------------------------------------------------------------*
-
- Name system - issues an MS-DOS command
-
- Usage int system(const char *command);
-
- Prototype in stdlib.h
-
- Description system invokes the MS-DOS command processor to
- execute a command given in the string command, as if the command
- had been typed at the DOS prompt.
-
- The COMSPEC environment variable is used to find the
- command processor file, so the file does not need to be in
- the current directory.
-
- Return value system returns the exit status of COMMAND.COM when
- the given command is completed.
-
- *---------------------------------------------------------------------*/
- int system(const char *cmd)
- {
- register char *cmdP;
- int cmdS;
- register char *envP;
- void *envSave;
- char *pathP;
- int rc;
-
- /* Get COMMAND.COM path
- */
- if ((pathP = getenv("COMSPEC")) == NULL)
- {
- errno = ENOENT;
- return -1;
- }
-
- /* Build command line
- */
- cmdS = 1 + 3 + strlen(cmd) + 1;
- if (cmdS > 128 || (cmdP = malloc(cmdS)) == NULL)
- {
- errno = ENOMEM;
- return (-1);
- }
-
- if (cmdS == 5)
- {
- cmdP[0] = 0;
- cmdP[1] = '\r';
- }
- else
- {
- *cmdP++ = cmdS - 2;
- *cmdP++ = getswitchar();
- cmdP = stpcpy(cmdP, "c ");
- cmdP = stpcpy(cmdP, cmd);
- *cmdP++ = '\r';
- cmdP -= cmdS;
- }
-
- /* Build environment
- */
- if ((envP = __DOSenv(environ, pathP, &envSave)) == NULL) {
- errno = ENOMEM;
- free(cmdP);
- return (-1);
- }
-
- /* Flush all byte streams
- */
- (*_exitbuf)();
-
- /* Now, call the low level _spawn function
- */
- rc = _spawn(pathP, cmdP, envP);
-
- /* Release all buffers
- */
- free(envSave);
- free(cmdP);
- return (rc);
- }
-