Skip to content

Commit 0d6d673

Browse files
committed
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 e678595 commit 0d6d673

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

22212221
static void setup_windows_environment()
22222222
{
2223+
char *tmp;
2224+
22232225
/* on Windows it is TMP and TEMP */
22242226
if (!getenv("TMPDIR")) {
2225-
const char *tmp = getenv("TMP");
2226-
if (!tmp)
2227+
if (!(tmp = getenv("TMP")))
22272228
tmp = getenv("TEMP");
22282229
if (tmp)
22292230
setenv("TMPDIR", tmp, 1);
22302231
}
22312232

2233+
if ((tmp = getenv("TMPDIR"))) {
2234+
/*
2235+
* Convert all dir separators to forward slashes,
2236+
* to help shell commands called from the Git
2237+
* executable (by not mistaking the dir separators
2238+
* for escape characters).
2239+
*/
2240+
for (; *tmp; tmp++)
2241+
if (*tmp == '\\')
2242+
*tmp = '/';
2243+
}
2244+
2245+
if (!getenv("TZ") && (tmp = getenv("MSYS2_TZ")))
2246+
setenv("TZ", tmp, 1);
2247+
22322248
/* simulate TERM to enable auto-color (see color.c) */
22332249
if (!getenv("TERM"))
22342250
setenv("TERM", "cygwin", 1);
@@ -2303,26 +2319,8 @@ void mingw_startup()
23032319
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
23042320
for (i = 1; i < argc; i++)
23052321
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
2306-
for (i = 0; wenv[i]; i++) {
2322+
for (i = 0; wenv[i]; i++)
23072323
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
2308-
if (!strncasecmp(environ[i], "MSYS2_TZ=", 9)) {
2309-
char *to_free = environ[i];
2310-
environ[i] = xstrdup(to_free + 6);
2311-
free(to_free);
2312-
}
2313-
if (!strncasecmp(environ[i], "TMP=", 4)) {
2314-
/*
2315-
* Convert all dir separators to forward slashes,
2316-
* to help shell commands called from the Git
2317-
* executable (by not mistaking the dir separators
2318-
* for escape characters).
2319-
*/
2320-
char *p;
2321-
for (p = environ[i]; *p; p++)
2322-
if (*p == '\\')
2323-
*p = '/';
2324-
}
2325-
}
23262324
environ[i] = NULL;
23272325
free(buffer);
23282326

0 commit comments

Comments
 (0)