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

Commit 80e6f0f

Browse files
kbleeskasal
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 7f7675e commit 80e6f0f

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
@@ -785,7 +785,7 @@ static int environ_size = 0;
785785
/* allocated size of environ array, in bytes */
786786
static int environ_alloc = 0;
787787

788-
static char *do_getenv(const char *name)
788+
char *mingw_getenv(const char *name)
789789
{
790790
char *value;
791791
int pos = bsearchenv(environ, name, environ_size - 1);
@@ -795,22 +795,6 @@ static char *do_getenv(const char *name)
795795
return value ? &value[1] : NULL;
796796
}
797797

798-
char *mingw_getenv(const char *name)
799-
{
800-
char *result = do_getenv(name);
801-
if (!result && !strcmp(name, "TMPDIR")) {
802-
/* on Windows it is TMP and TEMP */
803-
result = do_getenv("TMP");
804-
if (!result)
805-
result = do_getenv("TEMP");
806-
}
807-
else if (!result && !strcmp(name, "TERM")) {
808-
/* simulate TERM to enable auto-color (see color.c) */
809-
result = "winansi";
810-
}
811-
return result;
812-
}
813-
814798
int mingw_putenv(const char *namevalue)
815799
{
816800
ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
@@ -2088,6 +2072,21 @@ void mingw_startup()
20882072
/* sort environment for O(log n) getenv / putenv */
20892073
qsort(environ, i, sizeof(char*), compareenv);
20902074

2075+
/* fix Windows specific environment settings */
2076+
2077+
/* on Windows it is TMP and TEMP */
2078+
if (!getenv("TMPDIR")) {
2079+
const char *tmp = getenv("TMP");
2080+
if (!tmp)
2081+
tmp = getenv("TEMP");
2082+
if (tmp)
2083+
setenv("TMPDIR", tmp, 1);
2084+
}
2085+
2086+
/* simulate TERM to enable auto-color (see color.c) */
2087+
if (!getenv("TERM"))
2088+
setenv("TERM", "winansi", 1);
2089+
20912090
/* initialize critical section for waitpid pinfo_t list */
20922091
InitializeCriticalSection(&pinfo_cs);
20932092

0 commit comments

Comments
 (0)