[C++] Handle multi-word SHELL in $(shell)
diff --git a/fileutil.cc b/fileutil.cc
index 42e81a2..a2a0d09 100644
--- a/fileutil.cc
+++ b/fileutil.cc
@@ -32,6 +32,7 @@
 #include <unordered_map>
 
 #include "log.h"
+#include "strutil.h"
 
 bool Exists(StringPiece filename) {
   CHECK(filename.size() < PATH_MAX);
@@ -62,6 +63,13 @@
 int RunCommand(const string& shell, const string& cmd,
                RedirectStderr redirect_stderr,
                string* s) {
+  string cmd_escaped = cmd;
+  EscapeShell(&cmd_escaped);
+  string cmd_with_shell = shell + " -c \"" + cmd_escaped + "\"";
+  const char* argv[] = {
+    "/bin/sh", "-c", cmd_with_shell.c_str(), NULL
+  };
+
   int pipefd[2];
   if (pipe(pipefd) != 0)
     PERROR("pipe failed");
@@ -106,9 +114,6 @@
       PERROR("dup2 failed");
     close(pipefd[1]);
 
-    const char* argv[] = {
-      shell.c_str(), "-c", cmd.c_str(), NULL
-    };
     execvp(argv[0], const_cast<char**>(argv));
     PLOG("execvp for %s failed", argv[0]);
     kill(getppid(), SIGTERM);