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

Commit df0e998

Browse files
kbleesgitster
authored andcommitted
Win32: factor out environment block creation
Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Stepan Kasal <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 26c7b21 commit df0e998

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

compat/mingw.c

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,36 @@ static int compareenv(const void *a, const void *b)
906906
return strcasecmp(*ea, *eb);
907907
}
908908

909+
/*
910+
* Create environment block suitable for CreateProcess.
911+
*/
912+
static wchar_t *make_environment_block(char **env)
913+
{
914+
wchar_t *wenvblk = NULL;
915+
int count = 0;
916+
char **e, **tmpenv;
917+
int size = 0, wenvsz = 0, wenvpos = 0;
918+
919+
for (e = env; *e; e++)
920+
count++;
921+
922+
/* environment must be sorted */
923+
tmpenv = xmalloc(sizeof(*tmpenv) * (count + 1));
924+
memcpy(tmpenv, env, sizeof(*tmpenv) * (count + 1));
925+
qsort(tmpenv, count, sizeof(*tmpenv), compareenv);
926+
927+
/* create environment block from temporary environment */
928+
for (e = tmpenv; *e; e++) {
929+
size = 2 * strlen(*e) + 2; /* +2 for final \0 */
930+
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
931+
wenvpos += xutftowcs(&wenvblk[wenvpos], *e, size) + 1;
932+
}
933+
/* add final \0 terminator */
934+
wenvblk[wenvpos] = 0;
935+
free(tmpenv);
936+
return wenvblk;
937+
}
938+
909939
struct pinfo_t {
910940
struct pinfo_t *next;
911941
pid_t pid;
@@ -982,29 +1012,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
9821012
xutftowcs(wargs, args.buf, 2 * args.len + 1);
9831013
strbuf_release(&args);
9841014

985-
if (env) {
986-
int count = 0;
987-
char **e, **sorted_env;
988-
int size = 0, wenvsz = 0, wenvpos = 0;
989-
990-
for (e = env; *e; e++)
991-
count++;
992-
993-
/* environment must be sorted */
994-
sorted_env = xmalloc(sizeof(*sorted_env) * (count + 1));
995-
memcpy(sorted_env, env, sizeof(*sorted_env) * (count + 1));
996-
qsort(sorted_env, count, sizeof(*sorted_env), compareenv);
997-
998-
/* create environment block from temporary environment */
999-
for (e = sorted_env; *e; e++) {
1000-
size = 2 * strlen(*e) + 2; /* +2 for final \0 */
1001-
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
1002-
wenvpos += xutftowcs(&wenvblk[wenvpos], *e, size) + 1;
1003-
}
1004-
/* add final \0 terminator */
1005-
wenvblk[wenvpos] = 0;
1006-
free(sorted_env);
1007-
}
1015+
if (env)
1016+
wenvblk = make_environment_block(env);
10081017

10091018
memset(&pi, 0, sizeof(pi));
10101019
ret = CreateProcessW(wcmd, wargs, NULL, NULL, TRUE, flags,

0 commit comments

Comments
 (0)