From 69baa621a32a8da06565ca40917fff80d4a50d97 Mon Sep 17 00:00:00 2001 From: Karsten Blees Date: Sun, 15 Jan 2012 02:35:26 +0100 Subject: [PATCH] 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 --- compat/mingw.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/compat/mingw.c b/compat/mingw.c index 67c9e9c4f6..36899908ee 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -809,7 +809,7 @@ static int environ_size = 0; /* allocated size of environ array, in bytes */ static int environ_alloc = 0; -static char *do_getenv(const char *name) +char *mingw_getenv(const char *name) { char *value; int pos = bsearchenv(environ, name, environ_size - 1); @@ -819,22 +819,6 @@ static char *do_getenv(const char *name) return value ? &value[1] : NULL; } -char *mingw_getenv(const char *name) -{ - char *result = do_getenv(name); - if (!result && !strcmp(name, "TMPDIR")) { - /* on Windows it is TMP and TEMP */ - result = do_getenv("TMP"); - if (!result) - result = do_getenv("TEMP"); - } - else if (!result && !strcmp(name, "TERM")) { - /* simulate TERM to enable auto-color (see color.c) */ - result = "winansi"; - } - return result; -} - int mingw_putenv(const char *namevalue) { ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc); @@ -2125,6 +2109,21 @@ void mingw_startup() /* sort environment for O(log n) getenv / putenv */ qsort(environ, i, sizeof(char*), compareenv); + /* fix Windows specific environment settings */ + + /* on Windows it is TMP and TEMP */ + if (!getenv("TMPDIR")) { + const char *tmp = getenv("TMP"); + if (!tmp) + tmp = getenv("TEMP"); + if (tmp) + setenv("TMPDIR", tmp, 1); + } + + /* simulate TERM to enable auto-color (see color.c) */ + if (!getenv("TERM")) + setenv("TERM", "winansi", 1); + /* initialize critical section for waitpid pinfo_t list */ InitializeCriticalSection(&pinfo_cs); -- 2.11.4.GIT