Skip to content

Commit 56cd5e4

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 1236744 commit 56cd5e4

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

22292229
static void setup_windows_environment()
22302230
{
2231+
char *tmp;
2232+
22312233
/* on Windows it is TMP and TEMP */
22322234
if (!getenv("TMPDIR")) {
2233-
const char *tmp = getenv("TMP");
2234-
if (!tmp)
2235+
if (!(tmp = getenv("TMP")))
22352236
tmp = getenv("TEMP");
22362237
if (tmp)
22372238
setenv("TMPDIR", tmp, 1);
22382239
}
22392240

2241+
if ((tmp = getenv("TMPDIR"))) {
2242+
/*
2243+
* Convert all dir separators to forward slashes,
2244+
* to help shell commands called from the Git
2245+
* executable (by not mistaking the dir separators
2246+
* for escape characters).
2247+
*/
2248+
for (; *tmp; tmp++)
2249+
if (*tmp == '\\')
2250+
*tmp = '/';
2251+
}
2252+
2253+
if (!getenv("TZ") && (tmp = getenv("MSYS2_TZ")))
2254+
setenv("TZ", tmp, 1);
2255+
22402256
/* simulate TERM to enable auto-color (see color.c) */
22412257
if (!getenv("TERM"))
22422258
setenv("TERM", "cygwin", 1);
@@ -2311,26 +2327,8 @@ void mingw_startup()
23112327
__argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
23122328
for (i = 1; i < argc; i++)
23132329
__argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
2314-
for (i = 0; wenv[i]; i++) {
2330+
for (i = 0; wenv[i]; i++)
23152331
environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
2316-
if (!strncasecmp(environ[i], "MSYS2_TZ=", 9)) {
2317-
char *to_free = environ[i];
2318-
environ[i] = xstrdup(to_free + 6);
2319-
free(to_free);
2320-
}
2321-
if (!strncasecmp(environ[i], "TMP=", 4)) {
2322-
/*
2323-
* Convert all dir separators to forward slashes,
2324-
* to help shell commands called from the Git
2325-
* executable (by not mistaking the dir separators
2326-
* for escape characters).
2327-
*/
2328-
char *p;
2329-
for (p = environ[i]; *p; p++)
2330-
if (*p == '\\')
2331-
*p = '/';
2332-
}
2333-
}
23342332
environ[i] = NULL;
23352333
free(buffer);
23362334

0 commit comments

Comments
 (0)