Skip to content

Commit 68ed56d

Browse files
kbleesdscho
authored andcommitted
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 <[email protected]>
1 parent cfa3a1b commit 68ed56d

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

compat/mingw.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,15 +2095,31 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
20952095

20962096
static void setup_windows_environment()
20972097
{
2098+
char *tmp;
2099+
20982100
/* on Windows it is TMP and TEMP */
20992101
if (!getenv("TMPDIR")) {
2100-
const char *tmp = getenv("TMP");
2101-
if (!tmp)
2102+
if (!(tmp = getenv("TMP")))
21022103
tmp = getenv("TEMP");
21032104
if (tmp)
21042105
setenv("TMPDIR", tmp, 1);
21052106
}
21062107

2108+
if ((tmp = getenv("TMPDIR"))) {
2109+
/*
2110+
* Convert all dir separators to forward slashes,
2111+
* to help shell commands called from the Git
2112+
* executable (by not mistaking the dir separators
2113+
* for escape characters).
2114+
*/
2115+
for (; *tmp; tmp++)
2116+
if (*tmp == '\\')
2117+
*tmp = '/';
2118+
}
2119+
2120+
if (!getenv("TZ") && (tmp = getenv("MSYS2_TZ")))
2121+
setenv("TZ", tmp, 1);
2122+
21072123
/* simulate TERM to enable auto-color (see color.c) */
21082124
if (!getenv("TERM"))
21092125
setenv("TERM", "cygwin", 1);
@@ -2178,26 +2194,8 @@ void mingw_startup()
21782194
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
21792195
for (i = 1; i < argc; i++)
21802196
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
2181-
for (i = 0; wenv[i]; i++) {
2197+
for (i = 0; wenv[i]; i++)
21822198
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
2183-
if (!strncasecmp(environ[i], "MSYS2_TZ=", 9)) {
2184-
char *to_free = environ[i];
2185-
environ[i] = xstrdup(to_free + 6);
2186-
free(to_free);
2187-
}
2188-
if (!strncasecmp(environ[i], "TMP=", 4)) {
2189-
/*
2190-
* Convert all dir separators to forward slashes,
2191-
* to help shell commands called from the Git
2192-
* executable (by not mistaking the dir separators
2193-
* for escape characters).
2194-
*/
2195-
char *p;
2196-
for (p = environ[i]; *p; p++)
2197-
if (*p == '\\')
2198-
*p = '/';
2199-
}
2200-
}
22012199
environ[i] = NULL;
22022200
free(buffer);
22032201

0 commit comments

Comments
 (0)