Skip to content

Commit 29a3963

Browse files
kbleespatthoyts
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]>
1 parent 0b45507 commit 29a3963

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

compat/mingw.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ static int environ_size = 0;
809809
/* allocated size of environ array, in bytes */
810810
static int environ_alloc = 0;
811811

812-
static char *do_getenv(const char *name)
812+
char *mingw_getenv(const char *name)
813813
{
814814
char *value;
815815
int pos = bsearchenv(environ, name, environ_size - 1);
@@ -819,22 +819,6 @@ static char *do_getenv(const char *name)
819819
return value ? &value[1] : NULL;
820820
}
821821

822-
char *mingw_getenv(const char *name)
823-
{
824-
char *result = do_getenv(name);
825-
if (!result && !strcmp(name, "TMPDIR")) {
826-
/* on Windows it is TMP and TEMP */
827-
result = do_getenv("TMP");
828-
if (!result)
829-
result = do_getenv("TEMP");
830-
}
831-
else if (!result && !strcmp(name, "TERM")) {
832-
/* simulate TERM to enable auto-color (see color.c) */
833-
result = "winansi";
834-
}
835-
return result;
836-
}
837-
838822
int mingw_putenv(const char *namevalue)
839823
{
840824
ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
@@ -2133,6 +2117,21 @@ void mingw_startup()
21332117
/* sort environment for O(log n) getenv / putenv */
21342118
qsort(environ, i, sizeof(char*), compareenv);
21352119

2120+
/* fix Windows specific environment settings */
2121+
2122+
/* on Windows it is TMP and TEMP */
2123+
if (!getenv("TMPDIR")) {
2124+
const char *tmp = getenv("TMP");
2125+
if (!tmp)
2126+
tmp = getenv("TEMP");
2127+
if (tmp)
2128+
setenv("TMPDIR", tmp, 1);
2129+
}
2130+
2131+
/* simulate TERM to enable auto-color (see color.c) */
2132+
if (!getenv("TERM"))
2133+
setenv("TERM", "winansi", 1);
2134+
21362135
/* initialize critical section for waitpid pinfo_t list */
21372136
InitializeCriticalSection(&pinfo_cs);
21382137

0 commit comments

Comments
 (0)