add executor_fork and executor_child

This commit is contained in:
whaffman 2025-02-12 19:14:05 +01:00
parent 662343a6ef
commit b196377066
2 changed files with 64 additions and 1 deletions

View File

@ -6,7 +6,7 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */
/* Updated: 2025/02/05 15:25:40 by whaffman ######## odam.nl */
/* Updated: 2025/02/12 18:47:30 by willem ######## odam.nl */
/* */
/* ************************************************************************** */
@ -50,6 +50,7 @@ typedef struct s_command
{
char *command;
char **args;
char **environment;
int fd_in;
int fd_out;
} t_command;

62
src/executor/executor.c Normal file
View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* 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_child(t_command *command)
{
if (command->fd_in != 0)
{
dup2(command->fd_in, 0);
close(command->fd_in);
}
if (command->fd_out != 1)
{
dup2(command->fd_out, 1);
close(command->fd_out);
}
execve(command->command, command->args, command->environment);
}
int executor_fork(t_command *command)
{
pid_t pid;
int status;
pid = fork();
if (pid > 0)
{
waitpid(pid, &status, 0);
return (((status) & 0xff00) >> 8);
}
else if (pid == 0)
executor_child(command);
else
perror("fork");
return (EXIT_FAILURE);
}
int main(int argc, char **argv, char **envp)
{
t_minishell minishell;
t_command command;
command.command = "/bin/ls";
command.args = (char *[]){"ls", "-l", NULL};
command.environment = envp;
command.fd_in = 0;
command.fd_out = 1;
printf("exit status: %d\n", executor_fork(&command));
return (0);
}