diff --git a/inc/executor.h b/inc/executor.h index 61d0075..7fdb68e 100644 --- a/inc/executor.h +++ b/inc/executor.h @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 17:06:07 by willem #+# #+# */ -/* Updated: 2025/02/18 15:45:27 by whaffman ######## odam.nl */ +/* Updated: 2025/02/21 13:01:39 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,9 +21,8 @@ pid_t executor_fork(t_command *command); void executor_create_pipes(t_minishell *minishell); int executor_execute_pipeline(t_minishell *minishell); void executor_close_fds(int n_fds); - - - - +int executor_count_fds(t_minishell *minishell); +int executor_open_fds(t_list *redirect_list); +void executor_create_redirects(t_minishell *minishell); #endif // EXECUTOR_H diff --git a/src/executor/executor_count_fds.c b/src/executor/executor_count_fds.c new file mode 100644 index 0000000..b1c1dea --- /dev/null +++ b/src/executor/executor_count_fds.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_count_fds.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/21 12:42:26 by whaffman #+# #+# */ +/* Updated: 2025/02/21 12:46:00 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int executor_count_fds(t_minishell *minishell) +{ + int fd; + int count; + t_list *current; + t_command *command; + + + current = minishell->commands; + count = (ft_lstsize(current) - 1) * 2; + while (current) + { + command = (t_command *)current->content; + count += ft_lstsize(command->redirect_in); + count += ft_lstsize(command->redirect_out); + current = current->next; + } + return (count); +} diff --git a/src/executor/executor_create_pipes.c b/src/executor/executor_create_pipes.c index d2306ac..6c24ff5 100644 --- a/src/executor/executor_create_pipes.c +++ b/src/executor/executor_create_pipes.c @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:22 by willem #+# #+# */ -/* Updated: 2025/02/13 14:58:49 by whaffman ######## odam.nl */ +/* Updated: 2025/02/21 12:48:04 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ void executor_create_pipes(t_minishell *minishell) t_command *command; int fd[2]; int fd_in; + const int n_fds = executor_count_fds(minishell); fd_in = 0; current = minishell->commands; @@ -43,7 +44,7 @@ void executor_create_pipes(t_minishell *minishell) else command->fd_out = 1; command->fd_in = fd_in; - command->n_fds = (ft_lstsize(minishell->commands) - 1) * 2; + command->n_fds = n_fds; fd_in = fd[0]; current = current->next; } diff --git a/src/executor/executor_create_redirects.c b/src/executor/executor_create_redirects.c new file mode 100644 index 0000000..e9c4caa --- /dev/null +++ b/src/executor/executor_create_redirects.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_create_redirects.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/21 13:00:00 by whaffman #+# #+# */ +/* Updated: 2025/02/21 13:00:11 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void executor_create_redirects(t_minishell *minishell) +{ + t_list *current; + t_command *command; + + current = minishell->commands; + while (current) + { + command = (t_command *)current->content; + command->fd_in = executor_open_fds(command->redirect_in); + command->fd_out = executor_open_fds(command->redirect_out); + current = current->next; + } +} diff --git a/src/executor/executor_execute_pipeline.c b/src/executor/executor_execute_pipeline.c index 1d75caf..3182355 100644 --- a/src/executor/executor_execute_pipeline.c +++ b/src/executor/executor_execute_pipeline.c @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:02 by willem #+# #+# */ -/* Updated: 2025/02/19 13:40:45 by whaffman ######## odam.nl */ +/* Updated: 2025/02/21 12:49:21 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ int executor_execute_pipeline(t_minishell *minishell) int exit_status; executor_create_pipes(minishell); + executor_create_redirects(minishell); current = minishell->commands; last_pid = 0; while (current) diff --git a/src/executor/executor_open_fds.c b/src/executor/executor_open_fds.c new file mode 100644 index 0000000..9c104ca --- /dev/null +++ b/src/executor/executor_open_fds.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_open_fds.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/21 12:50:42 by whaffman #+# #+# */ +/* Updated: 2025/02/21 12:59:41 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int executor_open_fds(t_list *redirect_list) +{ + t_redirect *redirect; + int fd; + + fd = 0; + while (redirect_list) + { + redirect = (t_redirect *)redirect_list->content; + if (redirect->type == T_REDIRECT_IN) + fd = open(redirect->value, O_RDONLY); + else if (redirect->type == T_REDIRECT_OUT) + fd = open(redirect->value, O_WRONLY | O_CREAT | O_TRUNC, 0644); + else if (redirect->type == T_APPEND_OUT) + fd = open(redirect->value, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd < 0) + { + error_msg("executor_open_fds", "can't open file"); + return (fd); + } + redirect_list = redirect_list->next; + } + return (fd); + +}