From 11000bfe64c56bb025773f35cf96eca514061d83 Mon Sep 17 00:00:00 2001
From: Ray Donnelly <mingw.android@gmail.com>
Date: Tue, 2 Jan 2018 15:45:22 +0000
Subject: [PATCH 05/17] Win32: Prevent conversion of R_ARCH to abs Windows path

---
 src/main/main.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/src/main/main.c b/src/main/main.c
index ae35b6dd0f..2550f44df8 100644
--- a/src/main/main.c
+++ b/src/main/main.c
@@ -793,6 +793,34 @@ void setup_Rmainloop(void)
 	} else setlocale(LC_MONETARY, Rlocale);
 	/* Windows does not have LC_MESSAGES */
 
+	/* Prevent conversion of R_ARCH to an absolute Windows path
+	   when calling from POSIX (sh.exe) to Windows (R.exe) as
+	   happens in configure.win scripts while building packages
+	   using MSYS2. MSYS2_ENV_CONV_EXCL was added for R initially,
+	   and follows from MSYS2_ARG_CONV_EXCL on which it was modelled.
+	*/
+
+	char MSYS2ECE_equals[1000+strlen("MSYS2_ENV_CONV_EXCL=")];
+	strcpy(MSYS2ECE_equals, "MSYS2_ENV_CONV_EXCL=");
+	char *MSYS2ECE = &MSYS2ECE_equals[strlen("MSYS2_ENV_CONV_EXCL=")];
+	p = getenv("MSYS2_ENV_CONV_EXCL");
+	/* The strstr will match other things that use R_ARCH as a substring
+	   .. let's assume none do. That part isn't strictly necessary. */
+	if (!p || !strstr(p, "R_ARCH")) {
+	    if (!p) {
+		sprintf(MSYS2ECE, "R_ARCH");
+		putenv(MSYS2ECE_equals);
+	    }
+	    else if (strlen(p) + strlen(";R_ARCH") < 1000) {
+		strncpy(MSYS2ECE, p ? p : "", 1000);
+		strcat(MSYS2ECE, ";R_ARCH");
+		MSYS2ECE[1000 - 1] = '\0';
+		putenv(MSYS2ECE_equals);
+	    }
+	    else
+		snprintf(deferred_warnings[ndeferred_warnings++], 250,
+			 "Adding R_ARCH to MSYS2_ENV_CONV_EXCL failed\n");
+	}
 	/* We set R_ARCH here: Unix does it in the shell front-end */
 	char Rarch[30];
 	strcpy(Rarch, "R_ARCH=/");
-- 
2.18.0

