Skip to content

Commit

Permalink
mingw: support spawning programs containing spaces in their names
Browse files Browse the repository at this point in the history
On some older Windows versions (e.g. Windows 7), the CreateProcessW()
function does not really support spaces in its first argument,
lpApplicationName. But it supports passing NULL as lpApplicationName,
which makes it figure out the application from the (possibly quoted)
first argument of lpCommandLine.

Let's use that trick (if we are certain that the first argument matches
the executable's path) to support launching programs whose path contains
spaces.

We will abuse the test-fake-ssh.exe helper to verify that this works and
does not regress.

This fixes git-for-windows#692

Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
dscho committed Feb 7, 2019
1 parent 28e450f commit 72ed27f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
si.hStdError = winansi_get_osfhandle(fherr);

/* executables and the current directory don't support long paths */
if (xutftowcs_path(wcmd, cmd) < 0)
if (*argv && !strcmp(cmd, *argv))
wcmd[0] = L'\0';
else if (xutftowcs_path(wcmd, cmd) < 0)
return -1;
if (dir && xutftowcs_path(wdir, dir) < 0)
return -1;
Expand Down Expand Up @@ -1501,8 +1503,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
wenvblk = make_environment_block(deltaenv);

memset(&pi, 0, sizeof(pi));
ret = CreateProcessW(wcmd, wargs, NULL, NULL, TRUE, flags,
wenvblk, dir ? wdir : NULL, &si, &pi);
ret = CreateProcessW(*wcmd ? wcmd : NULL, wargs, NULL, NULL, TRUE,
flags, wenvblk, dir ? wdir : NULL, &si, &pi);

free(wenvblk);
free(wargs);
Expand Down
6 changes: 6 additions & 0 deletions t/t0061-run-command.sh
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,10 @@ test_expect_success MINGW 'verify curlies are quoted properly' '
test_cmp expect actual
'

test_expect_success MINGW 'can spawn with argv[0] containing spaces' '
cp "$GIT_BUILD_DIR/t/helper/test-fake-ssh$X" ./ &&
test_must_fail "$PWD/test-fake-ssh$X" 2>err &&
grep TRASH_DIRECTORY err
'

test_done

0 comments on commit 72ed27f

Please sign in to comment.