execution de cmd dans fork avec wait
This commit is contained in:
29
srcs/main.c
29
srcs/main.c
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/04 05:59:26 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 **pipes_fd;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (!nb)
|
||||||
|
return (NULL);
|
||||||
pipes_fd = calloc(nb, sizeof(int *));
|
pipes_fd = calloc(nb, sizeof(int *));
|
||||||
i = 0;
|
i = 0;
|
||||||
while(i < nb)
|
while(i < nb)
|
||||||
@@ -42,12 +44,30 @@ int **create_pipes(int nb)
|
|||||||
return (pipes_fd);
|
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)
|
void shell_loop(t_all *c)
|
||||||
{
|
{
|
||||||
char *line_input;
|
char *line_input;
|
||||||
int nb_pipes;
|
int nb_pipes;
|
||||||
int **pipes_fd;
|
int **pipes_fd;
|
||||||
// pid_t pid;
|
|
||||||
|
|
||||||
line_input = NULL;
|
line_input = NULL;
|
||||||
while (1)
|
while (1)
|
||||||
@@ -59,10 +79,7 @@ void shell_loop(t_all *c)
|
|||||||
{
|
{
|
||||||
nb_pipes = count_pipes(line_input);
|
nb_pipes = count_pipes(line_input);
|
||||||
pipes_fd = create_pipes(nb_pipes);
|
pipes_fd = create_pipes(nb_pipes);
|
||||||
//pid = fork();
|
cmd_execution(c, pipes_fd, nb_pipes, line_input);
|
||||||
if (!ft_strncmp(line_input, "sleep ", 5))
|
|
||||||
execve("/bin/sleep", ft_split(line_input, ' '), c->envp);
|
|
||||||
write(1, "t", 1);
|
|
||||||
|
|
||||||
// if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
|
// if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
|
||||||
// builtin_env(0, NULL, c);
|
// builtin_env(0, NULL, c);
|
||||||
|
|||||||
Reference in New Issue
Block a user