From 603a0f733f5b343f52f1354483a9f9e1b53604c2 Mon Sep 17 00:00:00 2001 From: James Ross-Gowan Date: Tue, 24 Mar 2015 15:40:35 +1100 Subject: [PATCH] subprocess-win: clarify argument escaping logic This bit always seemed confusing to me. --- osdep/subprocess-win.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/osdep/subprocess-win.c b/osdep/subprocess-win.c index 7ea6d39cba..6c9ccf2382 100644 --- a/osdep/subprocess-win.c +++ b/osdep/subprocess-win.c @@ -54,22 +54,22 @@ static void write_arg(bstr *cmdline, char *arg) for (int pos = 0; arg[pos]; pos++) { switch (arg[pos]) { case '\\': - // Count backslashes that appear in a row + // Count consecutive backslashes num_slashes++; break; case '"': + // Write the argument up to the point before the quote bstr_xappend(NULL, cmdline, (struct bstr){arg, pos}); - - // Double preceding slashes - for (int i = 0; i < num_slashes; i++) - bstr_xappend(NULL, cmdline, bstr0("\\")); - - // Escape the following quote - bstr_xappend(NULL, cmdline, bstr0("\\")); - arg += pos; pos = 0; + + // Double backslashes preceding the quote + for (int i = 0; i < num_slashes; i++) + bstr_xappend(NULL, cmdline, bstr0("\\")); num_slashes = 0; + + // Escape the quote itself + bstr_xappend(NULL, cmdline, bstr0("\\")); break; default: num_slashes = 0; @@ -79,7 +79,7 @@ static void write_arg(bstr *cmdline, char *arg) // Write the rest of the argument bstr_xappend(NULL, cmdline, bstr0(arg)); - // Double slashes that appear at the end of the string + // Double backslashes at the end of the argument for (int i = 0; i < num_slashes; i++) bstr_xappend(NULL, cmdline, bstr0("\\"));