Fix for executing external apps on win32 with a space in the filename path.

View: New views
2 Messages — Rating Filter:   Alert me  

Fix for executing external apps on win32 with a space in the filename path.

by Blaine Whittle-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This is a resubmission of a patch I made last year, it's been applied and tested against R13B.
Original submission http://www.erlang.org/cgi-bin/ezmlm-cgi?3:mss:268:200808:lkalfgpohefpiajoncnd
After reviewing my original post, it appears that the test case I listed is incorrect.   Passing a command line wrapped with double quotes to os:cmd works as expected, for example this command should work in R13B (assuming Visual Studio 2005 installed on the machine)
os:cmd("\"C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv.com\" /?").
The real issue is with open_port.   For example, passing the same double-quote-delimited command line to open_port on R13B results in an einval exception
open_port({spawn, "\"C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv.com\" /?"},[{line, 60}, exit_status, hide, stderr_to_stdout]).

** exception error: einval
     in function  open_port/2
        called as open_port({spawn,"\"C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv.com\" /?"},
                            [{line,60},exit_status,hide,stderr_to_stdout])
The following patch fixes the open_port case
$ diff -u erts/emulator/sys/win32/sys.c $ERL_TOP/erts/emulator/sys/win32/sys.c
--- erts/emulator/sys/win32/sys.c       2009-03-12 04:16:34.000000000 -0800
+++ /cygdrive/c/dev/otp_src_R13B/erts/emulator/sys/win32/sys.c  2009-06-09 17:13:20.489037600 -0700
@@ -1237,6 +1237,8 @@
     char execPath[MAX_PATH];
     int cmdlength;
     char* thecommand;
+       char* fixedcommand;
+       char* pch;
     HANDLE hProcess = GetCurrentProcess();

     siStartInfo.cb = sizeof(STARTUPINFO);
@@ -1254,10 +1256,25 @@
     strncpy(thecommand, origcmd, cmdlength);
     thecommand[cmdlength] = '\0';
     DEBUGF(("spawn command: %s\n", thecommand));
-
-    applType = ApplicationType(thecommand, execPath);
-    DEBUGF(("ApplicationType returned for (%s) is %d\n", thecommand, applType));
+
+       fixedcommand = (char *) erts_alloc(ERTS_ALC_T_TMP, cmdlength+1);
+       fixedcommand[0] = '\0';
+       pch = strtok(thecommand,"\"");
+       if (pch == NULL) {
+               strncpy(fixedcommand, origcmd, cmdlength);
+               fixedcommand[cmdlength] = '\0';
+       }
+       else {
+               while (pch != NULL) {
+                       strncat(fixedcommand, pch, strlen(pch));
+                       pch = strtok(NULL, "\"");
+               }
+       }
+
+    applType = ApplicationType(fixedcommand, execPath);
+    DEBUGF(("ApplicationType returned for (%s) is %d\n", fixedcommand, applType));
     erts_free(ERTS_ALC_T_TMP, (void *) thecommand);
+    erts_free(ERTS_ALC_T_TMP, (void *) fixedcommand);
     if (applType == APPL_NONE) {
        return FALSE;
     }


Re: Fix for executing external apps on win32 with a space in the filename path.

by Björn Gustavsson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Jun 10, 2009 at 11:33 PM, Blaine Whittle<BWhittle@...> wrote:
> This is a resubmission of a patch I made last year, it's been applied and tested against R13B.
> Original submission http://www.erlang.org/cgi-bin/ezmlm-cgi?3:mss:268:200808:lkalfgpohefpiajoncnd
> After reviewing my original post, it appears that the test case I listed is incorrect.   Passing a command line wrapped with double quotes to os:cmd works as expected, for example this command should work in R13B (assuming Visual Studio 2005 installed on the machine)
> os:cmd("\"C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv.com\" /?").
> The real issue is with open_port.   For example, passing the same double-quote-delimited command line to open_port on R13B results in an einval exception
> open_port({spawn, "\"C:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\devenv.com\" /?"},[{line, 60}, exit_status, hide, stderr_to_stdout]).

Thanks for pointing out the problem. We will correct it in R13B02.

Note that you don't have to quote the name of the executable in R13B01
if you use the newly introduced spawn_executable option for
open_port/2 described here:

http://www.erlang.org/doc/man/erlang.html#open_port-2

/Bjorn

--
Björn Gustavsson, Erlang/OTP, Ericsson AB

________________________________________________________________
erlang-patches mailing list. See http://www.erlang.org/faq.html
erlang-patches (at) erlang.org