diff --git a/include/execution.h b/include/execution.h index cb1a296..b5c5473 100644 --- a/include/execution.h +++ b/include/execution.h @@ -6,14 +6,17 @@ /* By: mmoussou infile, STDIN_FILENO); @@ -51,8 +54,37 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t) cmd->cmd = get_cmd_path(cmd->cmd, env_t); if (!(cmd->cmd)) return (-1); - - int fork_pid = fork(); + fork_pid = fork(); + if (fork_pid == -1) + return (-1); + if (!fork_pid) + { + status = execve(cmd->cmd, cmd->argv, env); + exit(-1); + } + else + waitpid(fork_pid, NULL, 0); + if (status == -1) + return (-1); + return (0); +} + +int exec_last_cmd(t_cmd *cmd, char **env, t_env *env_t) +{ + int fork_pid; + int status; + + status = dup2(cmd->infile, STDIN_FILENO); + if (status == -1) + return (-1); + status = dup2(STDOUT_FILENO, cmd->outfile); + if (status == -1) + return (-1); + if (cmd->cmd[0] != '/') + cmd->cmd = get_cmd_path(cmd->cmd, env_t); + if (!(cmd->cmd)) + return (-1); + fork_pid = fork(); if (fork_pid == -1) return (-1); if (!fork_pid) @@ -71,7 +103,6 @@ int exec_split_cmd(t_list *list_cmd, t_env *env) { char **env_array; int status; - t_cmd *cmd; env_array = env_get(env); if (!env_array) @@ -86,27 +117,8 @@ int exec_split_cmd(t_list *list_cmd, t_env *env) } list_cmd = list_cmd->next; } - cmd = list_cmd->content; - status = dup2(cmd->infile, STDIN_FILENO); - if (status == -1) - return (-1); - status = dup2(STDOUT_FILENO, cmd->outfile); - if (status == -1) - return (-1); - if (cmd->cmd[0] != '/') - cmd->cmd = get_cmd_path(cmd->cmd, env); - if (!(cmd->cmd)) - return (-1); - int fork_pid = fork(); - if (fork_pid == -1) - return (-1); - if (!fork_pid) - { - status = execve(cmd->cmd, cmd->argv, env_array); - exit(-1); - } - else - waitpid(fork_pid, NULL, 0); + status = exec_last_cmd(list_cmd->content, env_array, env); + ft_free("a", &env_array); if (status == -1) return (-1); return (0); diff --git a/src/exec/get_path.c b/src/exec/get_path.c index a46e80a..ae263a1 100644 --- a/src/exec/get_path.c +++ b/src/exec/get_path.c @@ -6,7 +6,7 @@ /* By: mmoussou content))->redirection, av); cmd_list = get_cmd_list(piped); - while (cmd_list) + exec_split_cmd(cmd_list, &env_l); + /*while (cmd_list) { cmd = cmd_list->content; cmd_list = cmd_list->next; } - print_cmd(cmd); + print_cmd(cmd);*/ free(test); ft_lstclear(&piped, free_token); ft_free("a", &lll);