From c02e19bfc87dcdde5eeabf88eb74985384ed5c22 Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 12 Feb 2025 21:30:04 +0100 Subject: [PATCH] split executor in files --- src/executor/executer_create_pipes.c | 41 ++++++++ src/executor/executor.c | 127 ----------------------- src/executor/executor_child.c | 25 +++++ src/executor/executor_close_pipes.c | 11 ++ src/executor/executor_execute_pipeline.c | 29 ++++++ src/executor/executor_fork.c | 35 +++++++ 6 files changed, 141 insertions(+), 127 deletions(-) create mode 100644 src/executor/executer_create_pipes.c delete mode 100644 src/executor/executor.c create mode 100644 src/executor/executor_child.c create mode 100644 src/executor/executor_close_pipes.c create mode 100644 src/executor/executor_execute_pipeline.c create mode 100644 src/executor/executor_fork.c diff --git a/src/executor/executer_create_pipes.c b/src/executor/executer_create_pipes.c new file mode 100644 index 0000000..c7dae9f --- /dev/null +++ b/src/executor/executer_create_pipes.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executer_create_pipes.c :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/12 21:25:22 by willem #+# #+# */ +/* Updated: 2025/02/12 21:25:23 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void executer_create_pipes(t_minishell *minishell) +{ + t_list *current; + t_command *command; + int fd[2]; + int fd_in; + + + + fd_in = 0; + current = minishell->commands; + while (current) + { + command = (t_command *)current->content; + if (current->next) + { + pipe(fd); + command->fd_out = fd[1]; + } + else + command->fd_out = 1; + command->fd_in = fd_in; + command->n_pipes = (ft_lstsize(minishell->commands) - 1) * 2; + fd_in = fd[0]; + current = current->next; + } +} \ No newline at end of file diff --git a/src/executor/executor.c b/src/executor/executor.c deleted file mode 100644 index d2cc485..0000000 --- a/src/executor/executor.c +++ /dev/null @@ -1,127 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* executor.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/12 18:45:50 by willem #+# #+# */ -/* Updated: 2025/02/12 18:45:51 by willem ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -void executor_close_pipes(int n_pipes) -{ - int i; - - i = 3; - while (i < n_pipes + 3) - { - close(i); - i++; - } -} - -void executor_child(t_command *command) -{ - char *path; - if (command->fd_in != 0) - dup2(command->fd_in, 0); - if (command->fd_out != 1) - dup2(command->fd_out, 1); - executor_close_pipes(command->n_pipes); - path = executor_absolute_path(command->environment, command->command); - execve(path, command->args, environment_get_arr(command->environment)); -} - -int executor_fork(t_command *command) -{ - pid_t pid; - int status; - - pid = fork(); - if (pid > 0) - { - if(command->fd_in != 0) - close(command->fd_in); - if(command->fd_out != 1) - close(command->fd_out); - waitpid(pid, &status, 0); - return (((status) & 0xff00) >> 8); - } - else if (pid == 0) - executor_child(command); - else - perror("fork"); - return (EXIT_FAILURE); -} - -void executer_create_pipes(t_minishell *minishell) -{ - t_list *current; - t_command *command; - int fd[2]; - int fd_in; - - - - fd_in = 0; - current = minishell->commands; - while (current) - { - command = (t_command *)current->content; - if (current->next) - { - pipe(fd); - command->fd_out = fd[1]; - } - else - command->fd_out = 1; - command->fd_in = fd_in; - command->n_pipes = (ft_lstsize(minishell->commands) - 1) * 2; - fd_in = fd[0]; - current = current->next; - } -} -void executor_execute_pipeline(t_minishell *minishell) -{ - t_list *current; - t_command *command; - - executer_create_pipes(minishell); - current = minishell->commands; - while (current) - { - command = (t_command *)current->content; - command->environment = minishell->environment; - executor_fork(command); - current = current->next; - } -} - -// int main(int argc, char **argv, char **envp) -// { -// t_minishell minishell; -// t_command command[3]; - -// minishell.commands = ft_lstnew(&command[0]); -// ft_lstadd_back(&minishell.commands, ft_lstnew(&command[1])); -// ft_lstadd_back(&minishell.commands, ft_lstnew(&command[2])); - -// command[0].command = "/bin/ls"; -// command[0].args = (char *[]){"ls", "-l", NULL}; -// command[0].environment = envp; - -// command[1].command = "/usr/bin/grep"; -// command[1].args = (char *[]){"grep", "src", NULL}; -// command[1].environment = envp; - -// command[2].command = "/usr/bin/wc"; -// command[2].args = (char *[]){"wc", "-l", NULL}; -// command[2].environment = envp; - -// executor_execute_pipeline(&minishell); -// return (0); -// } \ No newline at end of file diff --git a/src/executor/executor_child.c b/src/executor/executor_child.c new file mode 100644 index 0000000..7855161 --- /dev/null +++ b/src/executor/executor_child.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_child.c :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/12 21:25:10 by willem #+# #+# */ +/* Updated: 2025/02/12 21:25:11 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void executor_child(t_command *command) +{ + char *path; + if (command->fd_in != 0) + dup2(command->fd_in, 0); + if (command->fd_out != 1) + dup2(command->fd_out, 1); + executor_close_pipes(command->n_pipes); + path = executor_absolute_path(command->environment, command->command); + execve(path, command->args, environment_get_arr(command->environment)); +} \ No newline at end of file diff --git a/src/executor/executor_close_pipes.c b/src/executor/executor_close_pipes.c new file mode 100644 index 0000000..3b172bd --- /dev/null +++ b/src/executor/executor_close_pipes.c @@ -0,0 +1,11 @@ +void executor_close_pipes(int n_pipes) +{ + int i; + + i = 3; + while (i < n_pipes + 3) + { + close(i); + i++; + } +} \ No newline at end of file diff --git a/src/executor/executor_execute_pipeline.c b/src/executor/executor_execute_pipeline.c new file mode 100644 index 0000000..bf4961a --- /dev/null +++ b/src/executor/executor_execute_pipeline.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_execute_pipeline.c :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/12 21:25:02 by willem #+# #+# */ +/* Updated: 2025/02/12 21:25:03 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void executor_execute_pipeline(t_minishell *minishell) +{ + t_list *current; + t_command *command; + + executer_create_pipes(minishell); + current = minishell->commands; + while (current) + { + command = (t_command *)current->content; + command->environment = minishell->environment; + executor_fork(command); + current = current->next; + } +} \ No newline at end of file diff --git a/src/executor/executor_fork.c b/src/executor/executor_fork.c new file mode 100644 index 0000000..ad1cd69 --- /dev/null +++ b/src/executor/executor_fork.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_fork.c :+: :+: */ +/* +:+ */ +/* By: willem +#+ */ +/* +#+ */ +/* Created: 2025/02/12 21:24:52 by willem #+# #+# */ +/* Updated: 2025/02/12 21:24:53 by willem ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int executor_fork(t_command *command) +{ + pid_t pid; + int status; + + pid = fork(); + if (pid > 0) + { + if(command->fd_in != 0) + close(command->fd_in); + if(command->fd_out != 1) + close(command->fd_out); + waitpid(pid, &status, 0); + return (((status) & 0xff00) >> 8); + } + else if (pid == 0) + executor_child(command); + else + perror("fork"); + return (EXIT_FAILURE); +} \ No newline at end of file