Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT sources DOT redhat DOT com Delivered-To: mailing list cygwin AT sources DOT redhat DOT com Date: Mon, 20 Nov 2000 17:15:43 -0500 From: Christopher Faylor To: cygwin AT cygwin DOT com Subject: Re: [PATCH] Fix for backslash quoting in argument list passing (spawn_guts) Message-ID: <20001120171543.C10710@redhat.com> Reply-To: cygwin AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com References: <20001120212239 DOT A79433 AT free DOT bsdshell DOT net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.3.11i In-Reply-To: <20001120212239.A79433@free.bsdshell.net>; from bogus@bsdshell.net on Mon, Nov 20, 2000 at 09:22:39PM +0100 On Mon, Nov 20, 2000 at 09:22:39PM +0100, Pierre Bogossian wrote: >>I've included what I think is a much simpler patch below. It seems to >>work correctly using a vc compiled program which echos its args. > >The problem with your patch is that it can double backslashes even if >they don't precede a '"', and that shouldn't be done ! >Here is what I get with your patch: > >/tmp $ ./print_args_cygwin.exe 'a a\\a' >1: a a\\a >/tmp $ ./print_args_nocygwin.exe 'a a\\a' >1: a a\\\\a > >Another issue is that backslashes that are just before the closing '"' >have to be doubled too: > >/tmp $ ./print_args_cygwin.exe 'a a\' >1: a a\ >/tmp $ ./print_args_nocygwin.exe 'a a\' >1: a a" > >My print_args_nocygwin.exe is compiled with gcc, but I guess the >result is the same with vc. Ok. How about this, then? cgf Index: spawn.cc =================================================================== RCS file: /cvs/uberbaum/winsup/cygwin/spawn.cc,v retrieving revision 1.63 diff -u -r1.63 spawn.cc --- spawn.cc 2000/11/15 21:04:02 1.63 +++ spawn.cc 2000/11/20 22:00:20 @@ -480,12 +480,31 @@ else { one_line.add ("\"", 1); + /* Handle embedded special characters " and \. + A " is always preceded by a \. + A \ is not special unless it precedes a ". If it does, + then all preceding \'s must be doubled to avoid having + the Windows command line parser interpret the \ as quoting + the ". This rule applies to a string of \'s before the end + of the string, since cygwin/windows uses a " to delimit the + argument. */ for (; (p = strpbrk (a, "\"\\")); a = ++p) { one_line.add (a, p - a); - if ((*p == '\\' && p[1] == '"') || *p == '"') - one_line.add ("\\", 1); - one_line.add (p, 1); + /* Find length of string of backslashes */ + int n = strspn (p, "\\"); + if (!n) + one_line.add ("\\\"", 2); /* No backslashes, so it must be a ". + The " has to be protected with a backslash. */ + else + { + one_line.add (p, n); /* Add the run of backslashes */ + /* Need to double up all of the preceding + backslashes if they precede a quote or EOS. */ + if (!p[n] || p[n] == '"') + one_line.add (p, n); + p += n - 1; /* Point to last backslash */ + } } if (*a) one_line.add (a); -- Want to unsubscribe from this list? Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com