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 <blees@dcon.de> Signed-off-by: Stepan Kasal <kasal@ucw.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jul 17, 2014 at 17:38 UTC 6dc715439b8e9ec85d6412750665431dd6a5afc6
1 file changed +12 -13
compat/mingw.c
+12 -13
@@ -1251,7 +1251,7 @@ static int do_putenv(char **env, const char *name, int size, int free_old)
1251 return size;
1252 }
1253
1254 -static char *do_getenv(const char *name)
1254 +char *mingw_getenv(const char *name)
1255 {
1256 char *value;
1257 int pos = bsearchenv(environ, name, environ_size - 1);
@@ -1261,18 +1261,6 @@ static char *do_getenv(const char *name)
1261 return value ? &value[1] : NULL;
1262 }
1263
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 -
1264 int mingw_putenv(const char *namevalue)
1265 {
1266 ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
@@ -2114,6 +2102,17 @@ void mingw_startup()
2102 /* sort environment for O(log n) getenv / putenv */
2103 qsort(environ, i, sizeof(char*), compareenv);
2104
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 +
2116 /* initialize critical section for waitpid pinfo_t list */
2117 InitializeCriticalSection(&pinfo_cs);
2118