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

Commit 71d28d4

Browse files
kbleeskasal
authored andcommitted
Win32: factor out environment block creation
Signed-off-by: Karsten Blees <[email protected]>
1 parent a5261a8 commit 71d28d4

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
@@ -965,6 +965,36 @@ static char *path_lookup(const char *cmd, char **path, int exe_only)
965965
return prog;
966966
}
967967

968+
/*
969+
* Create environment block suitable for CreateProcess.
970+
*/
971+
static wchar_t *make_environment_block(char **env)
972+
{
973+
wchar_t *wenvblk = NULL;
974+
int count = 0;
975+
char **e, **tmpenv;
976+
int size = 0, wenvsz = 0, wenvpos = 0;
977+
978+
for (e = env; *e; e++)
979+
count++;
980+
981+
/* environment must be sorted */
982+
tmpenv = xmalloc(sizeof(*tmpenv) * (count + 1));
983+
memcpy(tmpenv, env, sizeof(*tmpenv) * (count + 1));
984+
qsort(tmpenv, count, sizeof(*tmpenv), compareenv);
985+
986+
/* create environment block from temporary environment */
987+
for (e = tmpenv; *e; e++) {
988+
size = 2 * strlen(*e) + 2; /* +2 for final \0 */
989+
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
990+
wenvpos += xutftowcs(&wenvblk[wenvpos], *e, size) + 1;
991+
}
992+
/* add final \0 terminator */
993+
wenvblk[wenvpos] = 0;
994+
free(tmpenv);
995+
return wenvblk;
996+
}
997+
968998
struct pinfo_t {
969999
struct pinfo_t *next;
9701000
pid_t pid;
@@ -1041,29 +1071,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **env,
10411071
xutftowcs(wargs, args.buf, 2 * args.len + 1);
10421072
strbuf_release(&args);
10431073

1044-
if (env) {
1045-
int count = 0;
1046-
char **e, **sorted_env;
1047-
int size = 0, wenvsz = 0, wenvpos = 0;
1048-
1049-
for (e = env; *e; e++)
1050-
count++;
1051-
1052-
/* environment must be sorted */
1053-
sorted_env = xmalloc(sizeof(*sorted_env) * (count + 1));
1054-
memcpy(sorted_env, env, sizeof(*sorted_env) * (count + 1));
1055-
qsort(sorted_env, count, sizeof(*sorted_env), compareenv);
1056-
1057-
/* create environment block from temporary environment */
1058-
for (e = sorted_env; *e; e++) {
1059-
size = 2 * strlen(*e) + 2; /* +2 for final \0 */
1060-
ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
1061-
wenvpos += xutftowcs(&wenvblk[wenvpos], *e, size) + 1;
1062-
}
1063-
/* add final \0 terminator */
1064-
wenvblk[wenvpos] = 0;
1065-
free(sorted_env);
1066-
}
1074+
if (env)
1075+
wenvblk = make_environment_block(env);
10671076

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

0 commit comments

Comments
 (0)