Skip to content

Commit 6dc7154

Browse files
kbleesgitster
authored andcommitted
Win32: patch Windows environment on startup
Fix Windows specific environment settings on startup rather than checking for special values on every getenv call. As a side effect, this makes the patched environment (i.e. with properly initialized TMPDIR and TERM) available to child processes. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Stepan Kasal <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 343ff06 commit 6dc7154

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

compat/mingw.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ static int do_putenv(char **env, const char *name, int size, int free_old)
12511251
return size;
12521252
}
12531253

1254-
static char *do_getenv(const char *name)
1254+
char *mingw_getenv(const char *name)
12551255
{
12561256
char *value;
12571257
int pos = bsearchenv(environ, name, environ_size - 1);
@@ -1261,18 +1261,6 @@ static char *do_getenv(const char *name)
12611261
return value ? &value[1] : NULL;
12621262
}
12631263

1264-
char *mingw_getenv(const char *name)
1265-
{
1266-
char *result = do_getenv(name);
1267-
if (!result && !strcmp(name, "TMPDIR")) {
1268-
/* on Windows it is TMP and TEMP */
1269-
result = do_getenv("TMP");
1270-
if (!result)
1271-
result = do_getenv("TEMP");
1272-
}
1273-
return result;
1274-
}
1275-
12761264
int mingw_putenv(const char *namevalue)
12771265
{
12781266
ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
@@ -2114,6 +2102,17 @@ void mingw_startup()
21142102
/* sort environment for O(log n) getenv / putenv */
21152103
qsort(environ, i, sizeof(char*), compareenv);
21162104

2105+
/* fix Windows specific environment settings */
2106+
2107+
/* on Windows it is TMP and TEMP */
2108+
if (!mingw_getenv("TMPDIR")) {
2109+
const char *tmp = mingw_getenv("TMP");
2110+
if (!tmp)
2111+
tmp = mingw_getenv("TEMP");
2112+
if (tmp)
2113+
setenv("TMPDIR", tmp, 1);
2114+
}
2115+
21172116
/* initialize critical section for waitpid pinfo_t list */
21182117
InitializeCriticalSection(&pinfo_cs);
21192118

0 commit comments

Comments
 (0)