From b1963770664f50a41e507d8f6f27c70b0089937c Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 12 Feb 2025 19:14:05 +0100 Subject: [PATCH] add executor_fork and executor_child --- inc/typedef.h | 3 +- src/executor/executor.c | 62 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 src/executor/executor.c diff --git a/inc/typedef.h b/inc/typedef.h index 6b6a6f2..e5372e5 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* 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; diff --git a/src/executor/executor.c b/src/executor/executor.c new file mode 100644 index 0000000..fe28b48 --- /dev/null +++ b/src/executor/executor.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* 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_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); +} \ No newline at end of file