Windows implementation of system() uses either a Unix sh or the Windows cmd.exe, depending on the command line
This commit is contained in:
parent
2a5b282143
commit
e5acc71bae
1 changed files with 23 additions and 11 deletions
|
@ -110,18 +110,29 @@ Happily, the Inform tools make very little use of this.
|
||||||
= (very early code)
|
= (very early code)
|
||||||
int Platform::system(const char *cmd) {
|
int Platform::system(const char *cmd) {
|
||||||
char cmd_line[10*MAX_PATH];
|
char cmd_line[10*MAX_PATH];
|
||||||
char *pcl;
|
|
||||||
const char *pc;
|
|
||||||
|
|
||||||
/* Run system commands with a Unix-like shell */
|
/* Check if the command should be executed with the Windows cmd interpreter
|
||||||
strcpy(cmd_line, "sh -c \"");
|
or a Unix-like shell, depending on whether or not the executable to run is
|
||||||
for (pc = cmd, pcl = cmd_line+strlen(cmd_line); *pc != 0; ++pc, ++pcl) {
|
given with a quoted path. */
|
||||||
if ((*pc == '\\') || (*pc == '\"'))
|
int unix = (cmd[0] != '\"');
|
||||||
*(pcl++) = '\\';
|
if (unix) {
|
||||||
*pcl = *pc;
|
/* For a Unix shell command, escape any double quotes and backslashes. */
|
||||||
|
char *pcl;
|
||||||
|
const char *pc;
|
||||||
|
strcpy(cmd_line, "sh -c \"");
|
||||||
|
for (pc = cmd, pcl = cmd_line+strlen(cmd_line); *pc != 0; ++pc, ++pcl) {
|
||||||
|
if ((*pc == '\\') || (*pc == '\"'))
|
||||||
|
*(pcl++) = '\\';
|
||||||
|
*pcl = *pc;
|
||||||
|
}
|
||||||
|
*(pcl++) = '\"';
|
||||||
|
*(pcl++) = 0;
|
||||||
|
} else {
|
||||||
|
/* Otherwise, run with the Windows command interpreter. */
|
||||||
|
strcpy(cmd_line, "cmd /s /c \"");
|
||||||
|
strcat(cmd_line, cmd);
|
||||||
|
strcat(cmd_line, "\"");
|
||||||
}
|
}
|
||||||
*(pcl++) = '\"';
|
|
||||||
*(pcl++) = 0;
|
|
||||||
|
|
||||||
STARTUPINFOA start;
|
STARTUPINFOA start;
|
||||||
memset(&start, 0, sizeof start);
|
memset(&start, 0, sizeof start);
|
||||||
|
@ -131,7 +142,8 @@ int Platform::system(const char *cmd) {
|
||||||
|
|
||||||
PROCESS_INFORMATION process;
|
PROCESS_INFORMATION process;
|
||||||
if (CreateProcessA(0, cmd_line, 0, 0, FALSE, CREATE_NO_WINDOW, 0, 0, &start, &process) == 0) {
|
if (CreateProcessA(0, cmd_line, 0, 0, FALSE, CREATE_NO_WINDOW, 0, 0, &start, &process) == 0) {
|
||||||
fprintf(stderr, "A Unix-like shell 'sh' (e.g. Cygwin or MinGW) must be in the path to run commands.\n");
|
if (unix)
|
||||||
|
fprintf(stderr, "A Unix-like shell \"sh\" (such as that from Cygwin) must be in the path.\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue