Index: /trunk/src/corelib/io/qprocess_os2.cpp
===================================================================
--- /trunk/src/corelib/io/qprocess_os2.cpp	(revision 72)
+++ /trunk/src/corelib/io/qprocess_os2.cpp	(revision 73)
@@ -243,39 +243,9 @@
 }
 
-void QProcessPrivate::startProcess()
-{
-    Q_Q(QProcess);
-
-#if defined (QPROCESS_DEBUG)
-    qDebug("QProcessPrivate::startProcess()");
-#endif
-
-    // Initialize pipes
-    if (!createChannel(stdinChannel) ||
-        !createChannel(stdoutChannel) ||
-        !createChannel(stderrChannel))
-        return;
-
-    // Start the process (platform dependent)
-    q->setProcessState(QProcess::Starting);
-
-    // save & copy the stdin socket
-    int stdin_copy = ::dup(fileno(stdin));
-    ::dup2(stdinChannel.pipe[0], fileno(stdin));
-
-    // save & copy the stdout and stderr if asked to
-    int stdout_copy = -1, stderr_copy = -1;
-    if (processChannelMode != QProcess::ForwardedChannels) {
-        stdout_copy = ::dup(fileno(stdout));
-        ::dup2(stdoutChannel.pipe[1], fileno(stdout));
-
-        // merge stdout and stderr if asked to
-        stderr_copy = ::dup(fileno(stderr));
-        if (processChannelMode == QProcess::MergedChannels) {
-            ::dup2(fileno(stdout), fileno(stderr));
-        } else {
-            ::dup2(stderrChannel.pipe[1], fileno(stderr));
-        }
-    }
+static int qt_startProcess(const QString &program, const QStringList &arguments,
+                           const QString &workingDirectory,
+                           const QStringList *environment, bool detached = false)
+{
+    int mode = detached ? P_DETACH : P_NOWAIT;
 
     // Create argument list with right number of elements, and set the final
@@ -299,11 +269,11 @@
     int envc = 0;
     char **envv;
-    if (environment.count()) {
+    if (environment && environment->count()) {
         bool seenPATH = false;
         bool seenCOMSPEC = false;
 
-        envv = new char *[environment.count() + 1 + 2 /* may be PATH + COMSPEC */];
-        for (; envc < environment.count(); ++envc) {
-            QString item = environment.at(envc);
+        envv = new char *[environment->count() + 1 + 2 /* may be PATH + COMSPEC */];
+        for (; envc < environment->count(); ++envc) {
+            QString item = environment->at(envc);
             envv[envc] = qstrdup(item.toLocal8Bit().constData());
             if (!seenPATH)
@@ -340,5 +310,5 @@
 
     // start the program
-    int pid = spawnvpe(P_NOWAIT | P_DEFAULT, encodedProgramName, argv, envv);
+    int pid = spawnvpe(mode | P_DEFAULT, encodedProgramName, argv, envv);
 
     // if spawnvpe() couldn't find the executable, try .CMD and .BAT
@@ -368,4 +338,46 @@
     // restore the current directory
     QDir::setCurrent(curDir);
+
+    return pid;
+}
+
+void QProcessPrivate::startProcess()
+{
+    Q_Q(QProcess);
+
+#if defined (QPROCESS_DEBUG)
+    qDebug("QProcessPrivate::startProcess()");
+#endif
+
+    // Initialize pipes
+    if (!createChannel(stdinChannel) ||
+        !createChannel(stdoutChannel) ||
+        !createChannel(stderrChannel))
+        return;
+
+    // Start the process (platform dependent)
+    q->setProcessState(QProcess::Starting);
+
+    // save & copy the stdin socket
+    int stdin_copy = ::dup(fileno(stdin));
+    ::dup2(stdinChannel.pipe[0], fileno(stdin));
+
+    // save & copy the stdout and stderr if asked to
+    int stdout_copy = -1, stderr_copy = -1;
+    if (processChannelMode != QProcess::ForwardedChannels) {
+        stdout_copy = ::dup(fileno(stdout));
+        ::dup2(stdoutChannel.pipe[1], fileno(stdout));
+
+        // merge stdout and stderr if asked to
+        stderr_copy = ::dup(fileno(stderr));
+        if (processChannelMode == QProcess::MergedChannels) {
+            ::dup2(fileno(stdout), fileno(stderr));
+        } else {
+            ::dup2(stderrChannel.pipe[1], fileno(stderr));
+        }
+    }
+
+    int pid = qt_startProcess(program, arguments, workingDirectory,
+                              &environment);
 
     // restore stdin/stdout/stderr
@@ -776,121 +788,15 @@
 /*! \internal
  */
-bool QProcessPrivate::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid)
-{
-// @todo later
-#if 0
-    processManager()->start();
-
-    QByteArray encodedWorkingDirectory = QFile::encodeName(workingDirectory);
-
-    // To catch the startup of the child
-    int startedPipe[2];
-    ::pipe(startedPipe);
-    // To communicate the pid of the child
-    int pidPipe[2];
-    ::pipe(pidPipe);
-
-    pid_t childPid = qt_fork();
-    if (childPid == 0) {
-        struct sigaction noaction;
-        memset(&noaction, 0, sizeof(noaction));
-        noaction.sa_handler = SIG_IGN;
-        qt_native_sigaction(SIGPIPE, &noaction, 0);
-
-        ::setsid();
-
-        qt_native_close(startedPipe[0]);
-        qt_native_close(pidPipe[0]);
-
-        pid_t doubleForkPid = qt_fork();
-        if (doubleForkPid == 0) {
-            ::fcntl(startedPipe[1], F_SETFD, FD_CLOEXEC);
-            qt_native_close(pidPipe[1]);
-
-            if (!encodedWorkingDirectory.isEmpty())
-                qt_native_chdir(encodedWorkingDirectory.constData());
-
-            char **argv = new char *[arguments.size() + 2];
-            for (int i = 0; i < arguments.size(); ++i) {
-#ifdef Q_OS_MAC
-                argv[i + 1] = ::strdup(arguments.at(i).toUtf8().constData());
-#else
-                argv[i + 1] = ::strdup(arguments.at(i).toLocal8Bit().constData());
-#endif
-            }
-            argv[arguments.size() + 1] = 0;
-
-            if (!program.contains(QLatin1Char('/'))) {
-                const QString path = QString::fromLocal8Bit(::getenv("PATH"));
-                if (!path.isEmpty()) {
-                    QStringList pathEntries = path.split(QLatin1Char(':'));
-                    for (int k = 0; k < pathEntries.size(); ++k) {
-                        QByteArray tmp = QFile::encodeName(pathEntries.at(k));
-                        if (!tmp.endsWith('/')) tmp += '/';
-                        tmp += QFile::encodeName(program);
-                        argv[0] = tmp.data();
-                        qt_native_execv(argv[0], argv);
-                    }
-                }
-            } else {
-                QByteArray tmp = QFile::encodeName(program);
-                argv[0] = tmp.data();
-                qt_native_execv(argv[0], argv);
-            }
-
-            struct sigaction noaction;
-            memset(&noaction, 0, sizeof(noaction));
-            noaction.sa_handler = SIG_IGN;
-            qt_native_sigaction(SIGPIPE, &noaction, 0);
-
-            // '\1' means execv failed
-            char c = '\1';
-            qt_native_write(startedPipe[1], &c, 1);
-            qt_native_close(startedPipe[1]);
-            ::_exit(1);
-        } else if (doubleForkPid == -1) {
-            struct sigaction noaction;
-            memset(&noaction, 0, sizeof(noaction));
-            noaction.sa_handler = SIG_IGN;
-            qt_native_sigaction(SIGPIPE, &noaction, 0);
-
-            // '\2' means internal error
-            char c = '\2';
-            qt_native_write(startedPipe[1], &c, 1);
-        }
-
-        qt_native_close(startedPipe[1]);
-        qt_native_write(pidPipe[1], (const char *)&doubleForkPid, sizeof(pid_t));
-        qt_native_chdir("/");
-        ::_exit(1);
-    }
-
-    qt_native_close(startedPipe[1]);
-    qt_native_close(pidPipe[1]);
-
-    if (childPid == -1) {
-        qt_native_close(startedPipe[0]);
-        qt_native_close(pidPipe[0]);
+bool QProcessPrivate::startDetached(const QString &program, const QStringList &arguments,
+                                    const QString &workingDirectory, qint64 *pid)
+{
+    int startedPid = qt_startProcess(program, arguments, workingDirectory,
+                                     NULL, true /* detached */);
+
+    if (startedPid == -1)
         return false;
-    }
-
-    char reply = '\0';
-    int startResult = qt_native_read(startedPipe[0], &reply, 1);
-    int result;
-    qt_native_close(startedPipe[0]);
-    while (::waitpid(childPid, &result, 0) == -1 && errno == EINTR)
-    { }
-    bool success = (startResult != -1 && reply == '\0');
-    if (success && pid) {
-        pid_t actualPid = 0;
-        if (qt_native_read(pidPipe[0], (char *)&actualPid, sizeof(pid_t)) == sizeof(pid_t)) {
-            *pid = actualPid;
-        } else {
-            *pid = 0;
-        }
-    }
-    qt_native_close(pidPipe[0]);
-    return success;
-#endif
+
+    *pid = startedPid;
+    return true;
 }
 
