* process.c (rb_proc_exec): strip trailing spaces.  [ruby-dev:24143]

* win32/win32.c (CreateChild): ditto.


Index: process.c
===================================================================
RCS file: /cvs/ruby/src/ruby/process.c,v
retrieving revision 1.109
diff -u -2 -p -r1.109 process.c
--- process.c	17 Aug 2004 09:02:39 -0000	1.109
+++ process.c	25 Aug 2004 14:03:39 -0000
@@ -1028,6 +1028,15 @@ rb_proc_exec(str)
 #else
     for (s=str; *s; s++) {
+	if (ISSPACE(*s)) {
+	    const char *p, *nl = NULL;
+	    for (p = s; ISSPACE(*p); p++) {
+		if (*p == '\n') nl = p;
+	    }
+	    if (!*p) break;
+	    if (nl) goto via_shell;
+	}
 	if (*s != ' ' && !ISALPHA(*s) && strchr("*?{}[]<>()~&|\\$;'`\"\n",*s)) {
 	    int status;
+	  via_shell:
 #if defined(MSDOS)
 	    before_exec();
@@ -1057,5 +1066,6 @@ rb_proc_exec(str)
     a = argv = ALLOCA_N(char*, (s-str)/2+2);
     ss = ALLOCA_N(char, s-str+1);
-    strcpy(ss, str);
+    memcpy(ss, str, s-str);
+    ss[s-str] = '\0';
     if (*a++ = strtok(ss, " \t")) {
 	while (t = strtok(NULL, " \t")) {
Index: win32/win32.c
===================================================================
RCS file: /cvs/ruby/src/ruby/win32/win32.c,v
retrieving revision 1.120
diff -U2 -p -d -r1.120 win32.c
--- win32/win32.c	18 Aug 2004 02:22:59 -0000	1.120
+++ win32/win32.c	25 Aug 2004 06:24:54 -0000
@@ -937,8 +937,20 @@ CreateChild(const char *cmd, const char 
     else {
 	int redir = -1;
+	int len = 0;
+	while (ISSPACE(*cmd)) cmd++;
+	for (prog = cmd; *prog; prog = CharNext(prog)) {
+	    if (ISSPACE(*prog)) {
+		len = prog - cmd;
+		do ++prog; while (ISSPACE(*prog));
+		if (!*prog) break;
+	    }
+	    else {
+		len = 0;
+	    }
+	}
+	if (!len) len = strlen(cmd);
 	if ((shell = getenv("RUBYSHELL")) && (redir = has_redirection(cmd))) {
-	    char *tmp = ALLOCA_N(char, strlen(shell) + strlen(cmd) +
-				 sizeof (" -c ") + 2);
-	    sprintf(tmp, "%s -c \"%s\"", shell, cmd);
+	    char *tmp = ALLOCA_N(char, strlen(shell) + len + sizeof(" -c ") + 2);
+	    sprintf(tmp, "%s -c \"%.*s\"", shell, len, cmd);
 	    cmd = tmp;
 	}
@@ -946,7 +958,6 @@ CreateChild(const char *cmd, const char 
 		 ((redir < 0 ? has_redirection(cmd) : redir) ||
 		  isInternalCmd(cmd, shell))) {
-	    char *tmp = ALLOCA_N(char, strlen(shell) + strlen(cmd) +
-				 sizeof (" /c "));
-	    sprintf(tmp, "%s /c %s", shell, cmd);
+	    char *tmp = ALLOCA_N(char, strlen(shell) + len + sizeof(" /c "));
+	    sprintf(tmp, "%s /c %.*s", shell, len, cmd);
 	    cmd = tmp;
 	}
@@ -959,7 +970,15 @@ CreateChild(const char *cmd, const char 
 		    break;
 		}
-		if (strchr(".:*?\"/\\", *prog)) break;
+		if (strchr(".:*?\"/\\", *prog)) {
+		    if (cmd[len]) {
+			char *tmp = ALLOCA_N(char, len + 1);
+			memcpy(tmp, cmd, len);
+			tmp[len] = 0;
+			cmd = tmp;
+		    }
+		    break;
+		}
 		if (ISSPACE(*prog) || strchr("<>|", *prog)) {
-		    int len = prog - cmd;
+		    len = prog - cmd;
 		    p = ALLOCA_N(char, len + 1);
 		    memcpy(p, cmd, len);
