@@ -1014,7 +1014,7 @@ char *mingw_getcwd(char *pointer, int len)
10141014 * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
10151015 * (Parsing C++ Command-Line Arguments)
10161016 */
1017- static const char * quote_arg (const char * arg )
1017+ static const char * quote_arg_msvc (const char * arg )
10181018{
10191019 /* count chars to quote */
10201020 int len = 0 , n = 0 ;
@@ -1069,6 +1069,37 @@ static const char *quote_arg(const char *arg)
10691069 return q ;
10701070}
10711071
1072+ #include "quote.h"
1073+
1074+ static const char * quote_arg_msys2 (const char * arg )
1075+ {
1076+ struct strbuf buf = STRBUF_INIT ;
1077+ const char * p2 = arg , * p ;
1078+
1079+ for (p = arg ; * p ; p ++ ) {
1080+ int ws = isspace (* p );
1081+ if (!ws && * p != '\\' && * p != '"' && * p != '{' )
1082+ continue ;
1083+ if (!buf .len )
1084+ strbuf_addch (& buf , '"' );
1085+ if (p != p2 )
1086+ strbuf_add (& buf , p2 , p - p2 );
1087+ if (!ws && * p != '{' )
1088+ strbuf_addch (& buf , '\\' );
1089+ p2 = p ;
1090+ }
1091+
1092+ if (p == arg )
1093+ strbuf_addch (& buf , '"' );
1094+ else if (!buf .len )
1095+ return arg ;
1096+ else
1097+ strbuf_add (& buf , p2 , p - p2 ),
1098+
1099+ strbuf_addch (& buf , '"' );
1100+ return strbuf_detach (& buf , 0 );
1101+ }
1102+
10721103static const char * parse_interpreter (const char * cmd )
10731104{
10741105 static char buf [100 ];
@@ -1300,6 +1331,34 @@ struct pinfo_t {
13001331static struct pinfo_t * pinfo = NULL ;
13011332CRITICAL_SECTION pinfo_cs ;
13021333
1334+ static int is_msys2_sh (const char * cmd )
1335+ {
1336+ if (cmd && !strcmp (cmd , "sh" )) {
1337+ static int ret = -1 ;
1338+ char * p ;
1339+
1340+ if (ret >= 0 )
1341+ return ret ;
1342+
1343+ p = path_lookup (cmd , 0 );
1344+ if (!p )
1345+ ret = 0 ;
1346+ else {
1347+ size_t len = strlen (p );
1348+ ret = len > 15 &&
1349+ is_dir_sep (p [len - 15 ]) &&
1350+ !strncasecmp (p + len - 14 , "usr" , 3 ) &&
1351+ is_dir_sep (p [len - 11 ]) &&
1352+ !strncasecmp (p + len - 10 , "bin" , 3 ) &&
1353+ is_dir_sep (p [len - 7 ]) &&
1354+ !strcasecmp (p + len - 6 , "sh.exe" );
1355+ free (p );
1356+ }
1357+ return ret ;
1358+ }
1359+ return 0 ;
1360+ }
1361+
13031362static pid_t mingw_spawnve_fd (const char * cmd , const char * * argv , char * * deltaenv ,
13041363 const char * dir ,
13051364 int prepend_cmd , int fhin , int fhout , int fherr )
@@ -1311,6 +1370,8 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
13111370 unsigned flags = CREATE_UNICODE_ENVIRONMENT ;
13121371 BOOL ret ;
13131372 HANDLE cons ;
1373+ const char * (* quote_arg )(const char * arg ) =
1374+ is_msys2_sh (* argv ) ? quote_arg_msys2 : quote_arg_msvc ;
13141375
13151376 do_unset_environment_variables ();
13161377
0 commit comments