diff --git a/srcs/main.c b/srcs/main.c index 3479912..faac772 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */ -/* Updated: 2021/10/17 22:10:48 by hulamy ### ########.fr */ +/* Updated: 2021/10/18 08:59:59 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,6 +30,8 @@ int **create_pipes(int nb) int **pipes_fd; int i; + if (!nb) + return (NULL); pipes_fd = calloc(nb, sizeof(int *)); i = 0; while(i < nb) @@ -42,12 +44,30 @@ int **create_pipes(int nb) return (pipes_fd); } +void cmd_execution(t_all *c, int **pipes_fd, int nb_pipes, char *line_input) +{ + pid_t pid; + + (void)pipes_fd; + if (!nb_pipes) + return ; + pid = fork(); + if (pid == 0) // child + { + if (!ft_strncmp(line_input, "sleep ", 5)) + execve("/bin/sleep", ft_split(line_input, ' '), c->envp); + } + if (pid > 0) // parent + { + wait(0); + } +} + void shell_loop(t_all *c) { char *line_input; int nb_pipes; int **pipes_fd; -// pid_t pid; line_input = NULL; while (1) @@ -59,10 +79,7 @@ void shell_loop(t_all *c) { nb_pipes = count_pipes(line_input); pipes_fd = create_pipes(nb_pipes); - //pid = fork(); - if (!ft_strncmp(line_input, "sleep ", 5)) - execve("/bin/sleep", ft_split(line_input, ' '), c->envp); - write(1, "t", 1); + cmd_execution(c, pipes_fd, nb_pipes, line_input); // if (!ft_strncmp(line_input, "env", 4)) // temp placeholder // builtin_env(0, NULL, c);