Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 410b91b

Browse files
kbleeskasal
authored andcommitted
Win32: Unicode environment (outgoing)
Convert environment from UTF-8 to UTF-16 when creating other processes. Signed-off-by: Karsten Blees <[email protected]>
1 parent f38d7ae commit 410b91b

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

compat/mingw.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,9 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
933933
{
934934
STARTUPINFOW si;
935935
PROCESS_INFORMATION pi;
936-
struct strbuf envblk, args;
937-
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs;
938-
unsigned flags;
936+
struct strbuf args;
937+
wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
938+
unsigned flags = CREATE_UNICODE_ENVIRONMENT;
939939
BOOL ret;
940940

941941
/* Determine whether or not we are associated to a console */
@@ -952,15 +952,14 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
952952
* instead of CREATE_NO_WINDOW to make ssh
953953
* recognize that it has no console.
954954
*/
955-
flags = DETACHED_PROCESS;
955+
flags |= DETACHED_PROCESS;
956956
} else {
957957
/* There is already a console. If we specified
958958
* DETACHED_PROCESS here, too, Windows would
959959
* disassociate the child from the console.
960960
* The same is true for CREATE_NO_WINDOW.
961961
* Go figure!
962962
*/
963-
flags = 0;
964963
CloseHandle(cons);
965964
}
966965
memset(&si, 0, sizeof(si));
@@ -999,6 +998,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
999998
if (env) {
1000999
int count = 0;
10011000
char **e, **sorted_env;
1001+
int size = 0, wenvsz = 0, wenvpos = 0;
10021002

10031003
for (e = env; *e; e++)
10041004
count++;
@@ -1008,20 +1008,22 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
10081008
memcpy(sorted_env, env, sizeof(*sorted_env) * (count + 1));
10091009
qsort(sorted_env, count, sizeof(*sorted_env), env_compare);
10101010

1011-
strbuf_init(&envblk, 0);
1011+
/* create environment block from temporary environment */
10121012
for (e = sorted_env; *e; e++) {
1013-
strbuf_addstr(&envblk, *e);
1014-
strbuf_addch(&envblk, '\0');
1013+
size = 2 * strlen(*e) + 2; /* +2 for final \0 */
1014+
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
1015+
wenvpos += xutftowcs(&wenvblk[wenvpos], *e, size) + 1;
10151016
}
1017+
/* add final \0 terminator */
1018+
wenvblk[wenvpos] = 0;
10161019
free(sorted_env);
10171020
}
10181021

10191022
memset(&pi, 0, sizeof(pi));
10201023
ret = CreateProcessW(wcmd, wargs, NULL, NULL, TRUE, flags,
1021-
env ? envblk.buf : NULL, dir ? wdir : NULL, &si, &pi);
1024+
wenvblk, dir ? wdir : NULL, &si, &pi);
10221025

1023-
if (env)
1024-
strbuf_release(&envblk);
1026+
free(wenvblk);
10251027
free(wargs);
10261028

10271029
if (!ret) {

0 commit comments

Comments
 (0)