mingw: move MSys2 specific environment tweaks to setup_windows_environment

Lets keep the environment initialization and conversion section as lean as
possible and move recently added tweaks to setup_windows_environment().

This fixes the following potential problems:

 * Prevent duplicate TZ variables if both TZ and MSYS2_TZ are set.
 * Some of the higher level x* APIs from wrapper.c require a working
   getenv(), using e.g. xstrdup() during initialization is dangerous.
 * Slashifying the Windows TMP variable may break native Windows programs,
   use POSIX TMPDIR instead.
 * Properly slashify TMPDIR even if it is already set, and also if we only
   have TEMP, but not TMP.
 * Reduce complexity from O(n) to O(log n).

Signed-off-by: Karsten Blees <blees@dcon.de>
This commit is contained in:
Karsten Blees
2015-04-06 21:47:07 +02:00
committed by Johannes Schindelin
parent 7fe262b99e
commit 626d1dfc54

View File

@@ -2211,15 +2211,31 @@ int handle_long_path(wchar_t *path, int len, int max_path, int expand)
static void setup_windows_environment()
{
char *tmp;
/* on Windows it is TMP and TEMP */
if (!getenv("TMPDIR")) {
const char *tmp = getenv("TMP");
if (!tmp)
if (!(tmp = getenv("TMP")))
tmp = getenv("TEMP");
if (tmp)
setenv("TMPDIR", tmp, 1);
}
if ((tmp = getenv("TMPDIR"))) {
/*
* Convert all dir separators to forward slashes,
* to help shell commands called from the Git
* executable (by not mistaking the dir separators
* for escape characters).
*/
for (; *tmp; tmp++)
if (*tmp == '\\')
*tmp = '/';
}
if (!getenv("TZ") && (tmp = getenv("MSYS2_TZ")))
setenv("TZ", tmp, 1);
/* simulate TERM to enable auto-color (see color.c) */
if (!getenv("TERM"))
setenv("TERM", "cygwin", 1);
@@ -2294,26 +2310,8 @@ void mingw_startup()
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
for (i = 1; i < argc; i++)
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
for (i = 0; wenv[i]; i++) {
for (i = 0; wenv[i]; i++)
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
if (!strncasecmp(environ[i], "MSYS2_TZ=", 9)) {
char *to_free = environ[i];
environ[i] = xstrdup(to_free + 6);
free(to_free);
}
if (!strncasecmp(environ[i], "TMP=", 4)) {
/*
* Convert all dir separators to forward slashes,
* to help shell commands called from the Git
* executable (by not mistaking the dir separators
* for escape characters).
*/
char *p;
for (p = environ[i]; *p; p++)
if (*p == '\\')
*p = '/';
}
}
environ[i] = NULL;
free(buffer);