redirects and fds+count

This commit is contained in:
whaffman 2025-02-21 13:03:49 +01:00
parent 8da0e0a036
commit 3631e127e4
6 changed files with 109 additions and 8 deletions

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* 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

View File

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* executor_count_fds.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* 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;
}

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* executor_create_redirects.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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;
}
}

View File

@ -6,7 +6,7 @@
/* By: willem <willem@student.codam.nl> +#+ */
/* +#+ */
/* 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)

View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* executor_open_fds.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* 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);
}