grep: allow -E and -n to be turned on by default via configuration
[gitweb.git] / run-command.c
index 61b153987b0efb9d9c5c53e2a4aa6893a9362f01..8619c769a93c48e724957c019af5765ebee6af9f 100644 (file)
@@ -67,7 +67,13 @@ static int child_notifier = -1;
 
 static void notify_parent(void)
 {
-       write(child_notifier, "", 1);
+       /*
+        * execvp failed.  If possible, we'd like to let start_command
+        * know, so failures like ENOENT can be handled right away; but
+        * otherwise, finish_command will still report the error.
+        */
+       if (write(child_notifier, "", 1))
+               ; /* yes, dear gcc -D_FORTIFY_SOURCE, there was an error. */
 }
 
 static NORETURN void die_child(const char *err, va_list params)
@@ -77,9 +83,10 @@ static NORETURN void die_child(const char *err, va_list params)
        if (len > sizeof(msg))
                len = sizeof(msg);
 
-       write(child_err, "fatal: ", 7);
-       write(child_err, msg, len);
-       write(child_err, "\n", 1);
+       if (write(child_err, "fatal: ", 7) ||
+           write(child_err, msg, len) ||
+           write(child_err, "\n", 1))
+               ; /* yes, gcc -D_FORTIFY_SOURCE, we know there was an error. */
        exit(128);
 }
 #endif
@@ -192,6 +199,7 @@ int start_command(struct child_process *cmd)
        }
 
        trace_argv_printf(cmd->argv, "trace: run_command:");
+       fflush(NULL);
 
 #ifndef WIN32
 {
@@ -199,7 +207,6 @@ int start_command(struct child_process *cmd)
        if (pipe(notify_pipe))
                notify_pipe[0] = notify_pipe[1] = -1;
 
-       fflush(NULL);
        cmd->pid = fork();
        if (!cmd->pid) {
                /*
@@ -340,8 +347,6 @@ int start_command(struct child_process *cmd)
        else if (cmd->out > 1)
                fhout = dup(cmd->out);
 
-       if (cmd->dir)
-               die("chdir in start_command() not implemented");
        if (cmd->env)
                env = make_augmented_environ(cmd->env);
 
@@ -351,7 +356,7 @@ int start_command(struct child_process *cmd)
                cmd->argv = prepare_shell_cmd(cmd->argv);
        }
 
-       cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env,
+       cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env, cmd->dir,
                                  fhin, fhout, fherr);
        failed_errno = errno;
        if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
@@ -383,6 +388,8 @@ int start_command(struct child_process *cmd)
                        close(cmd->out);
                if (need_err)
                        close_pair(fderr);
+               else if (cmd->err)
+                       close(cmd->err);
                errno = failed_errno;
                return -1;
        }