1 /*-----------------------------------------------------------------------*
5 * systemx - issues an MS-DOS command
6 * and returns the command return code
7 *-----------------------------------------------------------------------*/
14 /* ------------- function prototypes ------------- */
15 static char *BDOSenv(char **envV
, char *pathP
, void **envSave
);
16 int cdecl far
_spawnx(char far
*__path
, char far
*__cmd
, char far
*__env
);
17 /* ------------- external variables -------------- */
18 extern void cdecl (*_exitbuf
)(void);
20 /* ------------------ systemx -------------------- */
22 systemx(const char far
*cmd
)
31 // Get COMMAND.COM path from COMSPEC
33 if ((pathP
= getenv("COMSPEC")) == NULL
) {
38 // Build command line "LEN /C cmd\r"
40 cmdS
= 1 + 3 + strlen(cmd
) + 1;
45 if ((cmdP
= malloc(cmdS
)) == NULL
) {
55 *cmdP
++ = getswitchar();
56 cmdP
= _stpcpy(cmdP
, "c ");
57 cmdP
= _stpcpy(cmdP
, cmd
);
62 // Build environment for cmd
64 if ((envP
= BDOSenv(environ
, pathP
, &envSave
)) == NULL
) {
70 // Flush all byte streams before calling cmd
74 // Now, call the low level _spawnx function
76 rc
= _spawnx(pathP
, cmdP
, envP
);
78 // Release all buffers, and exit
85 /* ------------------ BDOSenv -------------------- */
86 //Description This function allocates a buffer and fill it with all the
87 // environment strings one after the others. If the pathP
88 // variable is nonzero, then it is appended to the end of the
89 // end of the environment assumed it is for a spawn or exec
92 //Return value BDOSenv returns a pointer to the environment buffer if
93 // successful, and NULL on error.
94 //----------------------------------------------------
96 BDOSenv(char **envV
, char *pathP
, void **envSave
)
102 // Compute the environment size including the NULL string at the
103 // end of the environment. (Environment size < 32 Kbytes)
106 if ((envW
= envV
) != NULL
)
107 for (envS
= 0; *envW
&& **envW
; envS
+= strlen(*envW
++) + 1) ;
110 envS
+= 2 + strlen(pathP
) + 1;
116 if ((bufP
= malloc(envS
+ 15)) != NULL
) {
118 // The environment MUST be paragraph aligned
122 (*((unsigned *)&(bufP
))) &= 0xFFF0;
124 // Concatenate all environment strings
126 if ((envW
= envV
) != NULL
&& *envW
!= NULL
)
127 while (*envW
&& **envW
) {
128 bufP
= _stpcpy(bufP
, *envW
++);
134 // Append program name to the environment
137 *((short *)bufP
)++ = 1;
138 bufP
= _stpcpy(bufP
, pathP
);