{"thread":{"id":"10488","subject":"[MinGW PATCH] Rework quote_arg()","startedAt":"2007-10-27T13:30:40Z","lastAt":"2007-10-27T13:30:40Z","messageCount":1,"participants":["Nguyễn Thái Ngọc Duy"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"57358","messageId":"20071027133040.GA32312@laptop","threadId":"10488","inReplyTo":null,"subject":"[MinGW PATCH] Rework quote_arg()","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2007-10-27T13:30:40Z","receivedAt":"2007-10-27T13:30:40Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"MS Windows command line is handled in a weird way. This patch addresses:\n - Quote empty arguments\n - Only escape backslashes and double quotation marks inside quoted arguments\n - Quote arguments if they have asterisk or question marks to prevent expansion\n\nThe last one is not documented in the link provided in the patch. I encountered\nthat behavior on cmd.exe, Windows XP. MSYS not tested.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n compat/mingw.c |   31 ++++++++++++++++++++++++++++---\n 1 files changed, 28 insertions(+), 3 deletions(-)\n\ndiff --git a/compat/mingw.c b/compat/mingw.c\nindex 22b5e10..90dc080 100644\n--- a/compat/mingw.c\n+++ b/compat/mingw.c\n@@ -303,6 +303,7 @@ void openlog(const char *ident, int option, int facility)\n {\n }\n \n+/* See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx (Parsing C++ Command-Line Arguments */\n static const char *quote_arg(const char *arg)\n {\n \t/* count chars to quote */\n@@ -310,11 +311,23 @@ static const char *quote_arg(const char *arg)\n \tint force_quotes = 0;\n \tchar *q, *d;\n \tconst char *p = arg;\n+\tif (!*p) force_quotes = 1;\n \twhile (*p) {\n-\t\tif (isspace(*p))\n+\t\tif (isspace(*p) || *p == '*' || *p == '?')\n \t\t\tforce_quotes = 1;\n-\t\telse if (*p == '\"' || *p == '\\\\')\n+\t\telse if (*p == '\"')\n \t\t\tn++;\n+\t\telse if (*p == '\\\\') {\n+\t\t\tint count = 0;\n+\t\t\twhile (*p == '\\\\') {\n+\t\t\t\tcount++;\n+\t\t\t\tp++;\n+\t\t\t\tlen++;\n+\t\t\t}\n+\t\t\tif (*p == '\"')\n+\t\t\t\tn += count*2 + 1;\n+\t\t\tcontinue;\n+\t\t}\n \t\tlen++;\n \t\tp++;\n \t}\n@@ -325,8 +338,20 @@ static const char *quote_arg(const char *arg)\n \td = q = xmalloc(len+n+3);\n \t*d++ = '\"';\n \twhile (*arg) {\n-\t\tif (*arg == '\"' || *arg == '\\\\')\n+\t\tif (*arg == '\"')\n \t\t\t*d++ = '\\\\';\n+\t\telse if (*arg == '\\\\') {\n+\t\t\tint count = 0;\n+\t\t\twhile (*arg == '\\\\') {\n+\t\t\t\tcount++;\n+\t\t\t\t*d++ = *arg++;\n+\t\t\t}\n+\t\t\tif (*arg == '\"') {\n+\t\t\t\twhile (count-- > 0)\n+\t\t\t\t\t*d++ = '\\\\';\n+\t\t\t\t*d++ = '\\\\';\n+\t\t\t}\n+\t\t}\n \t\t*d++ = *arg++;\n \t}\n \t*d++ = '\"';\n-- \n1.5.3.rc4.3.gab089\n"}]}