Skip to content

Commit

Permalink
Win32: patch Windows environment on startup
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
kblees authored and patthoyts committed Oct 22, 2012
1 parent 0b45507 commit 29a3963
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -2133,6 +2117,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);

Expand Down

0 comments on commit 29a3963

Please sign in to comment.