Skip to content

Commit 2078f37

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 f962669 commit 2078f37

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
@@ -2211,15 +2211,31 @@ int handle_long_path(wchar_t *path, int len, int max_path, int expand)
22112211

22122212
static void setup_windows_environment()
22132213
{
2214+
char *tmp;
2215+
22142216
/* on Windows it is TMP and TEMP */
22152217
if (!getenv("TMPDIR")) {
2216-
const char *tmp = getenv("TMP");
2217-
if (!tmp)
2218+
if (!(tmp = getenv("TMP")))
22182219
tmp = getenv("TEMP");
22192220
if (tmp)
22202221
setenv("TMPDIR", tmp, 1);
22212222
}
22222223

2224+
if ((tmp = getenv("TMPDIR"))) {
2225+
/*
2226+
* Convert all dir separators to forward slashes,
2227+
* to help shell commands called from the Git
2228+
* executable (by not mistaking the dir separators
2229+
* for escape characters).
2230+
*/
2231+
for (; *tmp; tmp++)
2232+
if (*tmp == '\\')
2233+
*tmp = '/';
2234+
}
2235+
2236+
if (!getenv("TZ") && (tmp = getenv("MSYS2_TZ")))
2237+
setenv("TZ", tmp, 1);
2238+
22232239
/* simulate TERM to enable auto-color (see color.c) */
22242240
if (!getenv("TERM"))
22252241
setenv("TERM", "cygwin", 1);
@@ -2294,26 +2310,8 @@ void mingw_startup()
22942310
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
22952311
for (i = 1; i < argc; i++)
22962312
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
2297-
for (i = 0; wenv[i]; i++) {
2313+
for (i = 0; wenv[i]; i++)
22982314
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
2299-
if (!strncasecmp(environ[i], "MSYS2_TZ=", 9)) {
2300-
char *to_free = environ[i];
2301-
environ[i] = xstrdup(to_free + 6);
2302-
free(to_free);
2303-
}
2304-
if (!strncasecmp(environ[i], "TMP=", 4)) {
2305-
/*
2306-
* Convert all dir separators to forward slashes,
2307-
* to help shell commands called from the Git
2308-
* executable (by not mistaking the dir separators
2309-
* for escape characters).
2310-
*/
2311-
char *p;
2312-
for (p = environ[i]; *p; p++)
2313-
if (*p == '\\')
2314-
*p = '/';
2315-
}
2316-
}
23172315
environ[i] = NULL;
23182316
free(buffer);
23192317

0 commit comments

Comments
 (0)