split executor in files

This commit is contained in:
whaffman 2025-02-12 21:30:04 +01:00
parent 68efadf4ac
commit c02e19bfc8
6 changed files with 141 additions and 127 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,11 @@
void executor_close_pipes(int n_pipes)
{
int i;
i = 3;
while (i < n_pipes + 3)
{
close(i);
i++;
}
}

View File

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

View File

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