Patch: Win32 Process Implementation

Mohan Embar gnustuff@thisiscool.com
Thu Feb 20 10:09:00 GMT 2003


Hi People,

Here is a patch to Ranjit's Win32 process implementation which
corrects the fact that both the executable path and its arguments
need to be quoted if they contain embedded spaces. The patch
unconditionally quotes these. In addition, any embedded quotes
present in command line arguments to the executable are stripped -
this behavior seems to be consistent with what I observed in
Sun's JDK (1.4.1_01).

I've attached a test case and the pre- and post-bugfix output
as well as the output when run under Sun's java.

This patch corrects the problem I was observing here:

http://gcc.gnu.org/ml/java/2003-02/msg00306.html

which wasn't due to backslashes, but rather, the fact that
my executable was in "\Program Files\thisiscool-gcc".

With this patch and my _Jv_ThisExecutable() implementation,
(cf. the above post) for which I haven't submitted a formal
patch yet, Ranjit's stack traces work no matter where you launch
the executable from.

I used Ranjit's email format - let me know if this format
is okay.

-- Mohan
http://www.thisiscool.com/
http://www.animalsong.org/


ChangeLog:
2003-02-20  Mohan Embar  <gnustuff@thisiscool.com>

	* java/lang/natWin32Process.cc
	addArg(): helper function which adds an argument to
	the command line, double-quoting it but removing
	embedded quotes. This is consistent JDK 1.4
	java::lang::ConcreteProcess::startProcess: use addArg()
	for the executable name and its arguments

---------------------------------- 8< ----------------------------------
Index: java/lang/natWin32Process.cc
===================================================================
--- java/lang/natWin32Process.cc	2003-02-20 01:29:08.000000000 -0600
+++ java/lang/natWin32Process.cc	2003-02-20 03:33:12.000000000 -0600
@@ -122,4 +122,19 @@
 }
 
+static void addArg(char*& rpszDst, const char* pcszSrc)
+{
+  // Adds the current argument to the command line,
+  // quoting the argument as well as removing any
+  // quotes from within the argument.
+  *rpszDst++ = '\"';
+  char c;
+  while ((c = *pcszSrc++)) // note = and not ==
+  {
+    if (c != '\"')
+      *rpszDst++ = c;
+  }
+  *rpszDst++ = '\"';
+}
+
 void
 java::lang::ConcreteProcess::startProcess (jstringArray progarray,
@@ -137,19 +152,21 @@
 
   for (int i = 0; i < progarray->length; ++i)
-    cmdLineLen += (_Jv_GetStringUTFLength (elts[i]) + 1);
+    cmdLineLen += (_Jv_GetStringUTFLength (elts[i]) + 3);
 
   char *cmdLine = (char *) _Jv_Malloc (cmdLineLen + 1);
+  char *cmdLineCurPos = cmdLine;
 
-  int j = 0;
   for (int i = 0; i < progarray->length; ++i)
     {
+      if (i>0)
+        *cmdLineCurPos++ = ' ';
       jsize s = _Jv_GetStringUTFLength (elts[i]);
-      _Jv_GetStringUTFRegion (elts[i], 0, s, (cmdLine + j));
-
-      j += s;
-      *(cmdLine + j) = ' ';
-      j++;
+      char* pszThisArg = (char*) _Jv_Malloc(s+1);
+      _Jv_GetStringUTFRegion (elts[i], 0, s, pszThisArg);
+      pszThisArg[s] = '\0';
+      addArg(cmdLineCurPos, pszThisArg);
+      _Jv_Free(pszThisArg);
     }
-  *(cmdLine + j) = '\0';
+  *cmdLineCurPos = '\0';
 
   // Get the environment, if any.
---------------------------------- 8< ----------------------------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PrintArgs.zip
Type: application/zip
Size: 16152 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/java-patches/attachments/20030220/69d2d735/attachment.zip>


More information about the Java-patches mailing list