<commit_before>#include "cache.h"
#include "run-command.h"
#include "exec_cmd.h"

int start_command(struct child_process *cmd)
{
	int need_in = !cmd->no_stdin && cmd->in < 0;
	int fdin[2];

	if (need_in) {
		if (pipe(fdin) < 0)
			return -ERR_RUN_COMMAND_PIPE;
		cmd->in = fdin[1];
		cmd->close_in = 1;
	}

	cmd->pid = fork();
	if (cmd->pid < 0) {
		if (need_in) {
			close(fdin[0]);
			close(fdin[1]);
		}
		return -ERR_RUN_COMMAND_FORK;
	}

	if (!cmd->pid) {
		if (cmd->no_stdin) {
			int fd = open("/dev/null", O_RDWR);
			dup2(fd, 0);
			close(fd);
		} else if (need_in) {
			dup2(fdin[0], 0);
			close(fdin[0]);
			close(fdin[1]);
		} else if (cmd->in) {
			dup2(cmd->in, 0);
			close(cmd->in);
		}

		if (cmd->stdout_to_stderr)
			dup2(2, 1);
		if (cmd->git_cmd) {
			execv_git_cmd(cmd->argv);
		} else {
			execvp(cmd->argv[0], (char *const*) cmd->argv);
		}
		die("exec %s failed.", cmd->argv[0]);
	}

	if (need_in)
		close(fdin[0]);
	else if (cmd->in)
		close(cmd->in);

	return 0;
}

int finish_command(struct child_process *cmd)
{
	if (cmd->close_in)
		close(cmd->in);

	for (;;) {
		int status, code;
		pid_t waiting = waitpid(cmd->pid, &status, 0);<commit_msg>Simplify closing two fds at once in run-command.c<commit_after>#include "cache.h"
#include "run-command.h"
#include "exec_cmd.h"

static inline void close_pair(int fd[2])
{
	close(fd[0]);
	close(fd[1]);
}

int start_command(struct child_process *cmd)
{
	int need_in = !cmd->no_stdin && cmd->in < 0;
	int fdin[2];

	if (need_in) {
		if (pipe(fdin) < 0)
			return -ERR_RUN_COMMAND_PIPE;
		cmd->in = fdin[1];
		cmd->close_in = 1;
	}

	cmd->pid = fork();
	if (cmd->pid < 0) {
		if (need_in)
			close_pair(fdin);
		return -ERR_RUN_COMMAND_FORK;
	}

	if (!cmd->pid) {
		if (cmd->no_stdin) {
			int fd = open("/dev/null", O_RDWR);
			dup2(fd, 0);
			close(fd);
		} else if (need_in) {
			dup2(fdin[0], 0);
			close_pair(fdin);
		} else if (cmd->in) {
			dup2(cmd->in, 0);
			close(cmd->in);
		}

		if (cmd->stdout_to_stderr)
			dup2(2, 1);
		if (cmd->git_cmd) {
			execv_git_cmd(cmd->argv);
		} else {
			execvp(cmd->argv[0], (char *const*) cmd->argv);
		}
		die("exec %s failed.", cmd->argv[0]);
	}

	if (need_in)
		close(fdin[0]);
	else if (cmd->in)
		close(cmd->in);

	return 0;
}

int finish_command(struct child_process *cmd)
{
	if (cmd->close_in)
		close(cmd->in);

	for (;;) {
		int status, code;
		pid_t waiting = waitpid(cmd->pid, &status, 0);
{'fraction_non_alphanumeric': 0.20181968569065342, 'fraction_numerical': 0.020264681555004136, 'mean_word_length': 3.3664259927797833, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '599725', 'n_tokens_mistral': 1208, 'n_tokens_neox': 1084, 'n_words': 219}