Danny Backx wrote:
Pedro,

One of the todo's in your rshd.c source is to process blanks in path
names.

The attachment has a patch for this. Can I commit ?

------------------------------------------------------------------------

*** rshd.c-1    2007-07-01 12:00:36.000000000 +0200
--- rshd.c      2007-07-01 12:27:40.000000000 +0200
***************
*** 514,519 ****
--- 514,520 ----
          exit(0);
argslen = 0;
+ #if 0
    args = program;
    /* TODO: program paths with embedded spaces?  */
    args = strchr (args, ' ');
***************
*** 522,527 ****
--- 523,550 ----
    else
      args = "";
    argslen = strlen (args);
+ #else
+   /* Find the first blank that's not preceded by a backslash */
+   for (args=program; *args; args++) {
+         if (isspace(args[1]) && args[0] != '\\') {
+                 args[1] = '\0';
+                 args+=2;
+                 break;
+         }
+   }
+   /* Now, in program, remove backslashes followed by spaces */
+   char *p, *q;
+   for (p=q=program; *p; p++) {
+         if (isspace(p[1]) && p[0] == '\\')
+                 /* Skip backslash */;
+         else {
+                 *q++ = *p;
+         }
+   }
+   *q = '\0';
+   if (*args && args != program)
+         argslen = strlen(args);
+ #endif
    wargs = alloca ((argslen + 1) * sizeof (wchar_t));
    mbstowcs (wargs, args, argslen + 1);

Thanks for looking into it!

That's a bit weird quoting, isn't it?

If we go the escaping with '\\' route,

1) It would escape this:
  '\\path\\to\\prog\\ name.exe args'
into this:
  '\path\to\prog\ name.exe' + 'args'
while it should be:
  '\path\to\prog\ ' + 'name.exe args'

(yep, a file named ' ').

It feels un-win32 to double-escape every path separator,
but we can always do it the unix way:
  /path/to/prog\ name.exe args

I just tried looking at what cmd.exe does to handle
the escaping, and it seems the only way to do it, is
by quoting the path, like:

"\path\to\prog name.exe" args

or:

\path\to\"prog name.exe" args

The second patch (quote.diff) teaches create_child
to parse double quotes.  The methods are pretty much incompatible,
I don't know which is better, but this being WinCE -> win32, the second
should be more natural, right?.

2) If there are no args and nothing to escape, you will be left with:

args = program;
argslen = 0;

... and this will set wargs to the wrong thing:

wargs = alloca ((argslen + 1) * sizeof (wchar_t));
mbstowcs (wargs, args, argslen + 1);

What do you think of the attached patches?

Cheers,
Pedro Alves

A few nit picky notes:
- The whole file follows the GNU coding conventions.  Please
keep the formatting.   In emacs it's easy, just ctrl-c . gnu,
and tab away.  I don't know in vi.
- Declarations only at the start of a block in C, please.
- There is a ChangeLog - use it.

2007-07-01  Pedro Alves  <[EMAIL PROTECTED]>
	    Danny Backx  <[EMAIL PROTECTED]>

	* rshd.c (create_child): Handle path escaping.

Index: rshd.c
===================================================================
--- rshd.c	(revision 1034)
+++ rshd.c	(working copy)
@@ -29,6 +29,7 @@
 #include <stdarg.h>
 #include <malloc.h>
 #include <getopt.h>
+#include <ctype.h>
 
 #include <windows.h>
 #include <winsock2.h>
@@ -510,15 +511,34 @@ create_child (char *program, HANDLE *rea
       return NULL;
     }
 
-  argslen = 0;
-  args = program;
-  /* TODO: program paths with embedded spaces?  */
-  args = strchr (args, ' ');
-  if (args != NULL)
-    *args++ = '\0';
-  else
-    args = "";
+  args = "";
+
+  {
+    /* Extract path to program.  The path ends at the first unescaped
+       space, or at the end of the string.  */
+    char *p, *q;
+    int esc = 0;
+    for (p = q = program; *p; p++)
+      {
+	if (!esc && isspace (*p))
+	  {
+	    *p++ = '\0';
+	    args = p;
+	    break;
+	  }
+	else if (!esc && *p == '\\')
+	  esc = 1;
+	else
+	  {
+	    *q++ = *p;
+	    esc = 0;
+	  }
+      }
+    *q = '\0';
+  }
+
   argslen = strlen (args);
+
   wargs = alloca ((argslen + 1) * sizeof (wchar_t));
   mbstowcs (wargs, args, argslen + 1);
 
2007-07-01  Pedro Alves  <[EMAIL PROTECTED]>

	* rshd.c: Include ctype.h.
	 (create_child): Handle double quoting.

Index: rshd.c
===================================================================
--- rshd.c	(revision 1034)
+++ rshd.c	(working copy)
@@ -29,6 +29,7 @@
 #include <stdarg.h>
 #include <malloc.h>
 #include <getopt.h>
+#include <ctype.h>
 
 #include <windows.h>
 #include <winsock2.h>
@@ -510,15 +511,34 @@ create_child (char *program, HANDLE *rea
       return NULL;
     }
 
-  argslen = 0;
-  args = program;
-  /* TODO: program paths with embedded spaces?  */
-  args = strchr (args, ' ');
-  if (args != NULL)
-    *args++ = '\0';
-  else
-    args = "";
+  args = "";
+
+  {
+    /* Extract path to program.  If the path has embedded spaces, the
+       user must double quote it.  The path ends at the first unquoted
+       space, or at the end of the string.  */
+    char *p, *q;
+    int dquote = 0;
+    for (p = q = program; *p; p++)
+      {
+	if (!dquote && isspace (*p))
+	  {
+	    *p++ = '\0';
+	    args = p;
+	    break;
+	  }
+	else if (!dquote && *p == '"')
+	  dquote = 1;
+	else if (dquote && *p == '"')
+	  dquote = 0;
+	else
+	  *q++ = *p;
+      }
+    *q = '\0';
+  }
+
   argslen = strlen (args);
+
   wargs = alloca ((argslen + 1) * sizeof (wchar_t));
   mbstowcs (wargs, args, argslen + 1);
 
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Cegcc-devel mailing list
Cegcc-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cegcc-devel

Reply via email to