From 01e0f4fd92759d221b55013c437369b20ec36bb6 Mon Sep 17 00:00:00 2001 From: yosyo Date: Mon, 15 Jul 2024 21:01:30 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=94=A8=E3=80=8D=20fix(exec/exec?= =?UTF-8?q?=5Fcmds):=20fixed=20forks=20and=20all?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/execution.h | 3 +- src/exec/exec_split_cmd.c | 61 +++++++++++++++++++++++++++++++++------ src/exec/free_exec.c | 22 ++++++++++++++ 3 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 src/exec/free_exec.c diff --git a/include/execution.h b/include/execution.h index 70ef247..e3a9b14 100644 --- a/include/execution.h +++ b/include/execution.h @@ -6,7 +6,7 @@ /* By: mmoussou cmd); + exec.pipe_fd[0] = pipe_fd[0]; + exec.pipe_fd[1] = pipe_fd[1]; + exec.status = switch_cmd_path(cmd, env_t); + if (exec.status == -1 || !input || check_file(cmd->cmd, input)) + { + if (exec.status == -1) + printf("minishell : command not found: %s\n", input); + get_exit_code(127); + free(input); + return (-1); + } + free(input); + fork_pid = fork(); + if (!fork_pid) + { + __fork_single_cmd(cmd, env, env_t, exec); + free_exec(env_t, env); + exit(get_exit_code(-1)); + } + return (fork_pid); +} + int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2]) { t_exec exec; @@ -76,23 +108,37 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2]) { if (exec.status == -1) printf("minishell : command not found: %s\n", input); + get_exit_code(127); free(input); return (-1); } free(input); if (is_in_builtins(cmd->cmd) > 0) - { exec_cmd(cmd->cmd, cmd->argv, env, env_t); + if (is_in_builtins(cmd->cmd) > 0) return (0); - } fork_pid = fork(); if (!fork_pid) __fork_single_cmd(cmd, env, env_t, exec); return (fork_pid); } +t_exec exec_pipe_unforked(t_exec exec, t_list *list_cmd, t_env *env) +{ + exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \ + env, exec.pipe_fd); + __sig(exec.status); + if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO) + close(((t_cmd *)(list_cmd->content))->outfile); + if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO) + close(((t_cmd *)(list_cmd->content))->infile); + return (exec); +} + t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env) { + if (!list_cmd->next) + return (exec_pipe_unforked(exec, list_cmd, env)); while (list_cmd->next) { exec.status = pipe(exec.pipe_fd); @@ -103,20 +149,17 @@ t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env) ((t_cmd *)(list_cmd->content))->outfile = exec.pipe_fd[1]; if (((t_cmd *)(list_cmd->next->content))->infile == STDIN_FILENO) ((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0]; - exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \ + exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \ env, exec.pipe_fd); __close(list_cmd->content); if (exec.status != -1) exec.i++; list_cmd = list_cmd->next; } - exec.status = exec_single_cmd(list_cmd->content, exec.env_array, \ + exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \ env, exec.pipe_fd); __sig(exec.status); - if (((t_cmd *)(list_cmd->content))->outfile != STDOUT_FILENO) - close(((t_cmd *)(list_cmd->content))->outfile); - if (((t_cmd *)(list_cmd->content))->infile != STDIN_FILENO) - close(((t_cmd *)(list_cmd->content))->infile); + __close(list_cmd->content); return (exec); } diff --git a/src/exec/free_exec.c b/src/exec/free_exec.c new file mode 100644 index 0000000..4cab960 --- /dev/null +++ b/src/exec/free_exec.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free_exec.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou