protected function exec($cmd, Result $result)
    {
        //Tmp file to store stderr
        $tempStdErr = tempnam($this->tmpDir, 'cmr');

        if (!$tempStdErr) {
            throw new Exception("Could not create temporary file on $tempStdErr");
        }

        $otp = array();
        $exitCode = null;

        if ($this->stdIn !== null) {
            $filename = $this->createFauxStdIn($this->stdIn);

            if ($this->os->isWindowsLike()) {
                $cat = "type $filename";
            } else {
                $cat = "cat $filename";
            }

            $cmd = "$cat | $cmd";
        }

        //Set CWD
        $prevCwd = getcwd();
        if ($this->cwd) {
            chdir($this->cwd);
        }

        $result->setLastLine(trim(exec("$cmd 2> $tempStdErr", $otp, $exitCode)));

        $result->setStdIn($this->stdIn)
            ->setStdOut(implode(PHP_EOL, $otp))
            ->setStdErr(file_get_contents($tempStdErr))
            ->setExitCode($exitCode);

        // restore CWD
        chdir($prevCwd);

        return $result;
    }