dup2 pose un pbm pour l'instant

This commit is contained in:
hugogogo
2021-10-18 22:32:34 +02:00
parent 6e32b4ae47
commit 25d159410f

View File

@@ -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/18 11:25:43 by hulamy ### ########.fr */ /* Updated: 2021/10/18 22:29:02 by hulamy ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,7 +16,7 @@ void print_tab(char **array);
typedef struct s_pipe typedef struct s_pipe
{ {
int nb_pipe; int nb_pipes;
int **pipes_fd; int **pipes_fd;
char **input_split; char **input_split;
} t_pipe; } t_pipe;
@@ -47,9 +47,17 @@ int **create_pipes(int nb)
{ {
pipes_fd[i] = calloc(2, sizeof(int)); pipes_fd[i] = calloc(2, sizeof(int));
pipe(pipes_fd[i]); pipe(pipes_fd[i]);
printf("%i - %i\n", pipes_fd[i][0],pipes_fd[i][1]); printf("%i - %i\n", pipes_fd[i][0],pipes_fd[i][1]);
i++; i++;
} }
int j;
j = -1;
while (++j < nb)
{
printf("A\n");
printf("%i: %i - %i\n", j, pipes_fd[j][0], pipes_fd[j][1]);
printf("B\n");
}
return (pipes_fd); return (pipes_fd);
} }
@@ -70,28 +78,64 @@ t_pipe *fill_pipes(char *input)
{ {
t_pipe *pipes; t_pipe *pipes;
pipes->nb_pipe = count_pipes(input); pipes = calloc(1, sizeof(t_pipe));
pipes->nb_pipes = count_pipes(input);
pipes->pipes_fd = create_pipes(pipes->nb_pipes); pipes->pipes_fd = create_pipes(pipes->nb_pipes);
pipes->input_split = split_pipes(input); pipes->input_split = split_pipes(input);
int i;
i = -1;
while (++i < pipes->nb_pipes)
{
printf("C\n");
printf("%i: %i - %i\n", i, pipes->pipes_fd[i][0], pipes->pipes_fd[i][1]);
printf("D\n");
}
return (pipes); return (pipes);
} }
void execute_cmd(t_all *c, char *line_input, t_all *pipes) void execute_cmd(t_all *c, t_pipe *pipes)
{ {
pid_t pid; pid_t pid;
int i;
int stdout_bak;
while (nb_pipes-- >= 0) if (pipes->nb_pipes)
stdout_bak = dup(STDOUT_FILENO);
i = 0;
while (i <= pipes->nb_pipes)
{ {
if (pipes->nb_pipes)
{
printf("%i\n", stdout_bak);
if (i == 0)
{
dup2(pipes->pipes_fd[0][1], STDOUT_FILENO);
}
if (i == 1)
{
dup2(pipes->pipes_fd[0][0], STDIN_FILENO);
dup2(stdout_bak, STDOUT_FILENO);
}
close(pipes->pipes_fd[0][0]);
close(pipes->pipes_fd[0][1]);
close(stdout_bak);
}
pid = fork(); pid = fork();
if (pid == 0) // child if (pid == 0) // child
{ {
if (!ft_strncmp(line_input, "sleep ", 5)) if (!ft_strncmp(pipes->input_split[i], "sleep ", 6))
execve("/bin/sleep", ft_split(line_input, ' '), c->envp); execve("/bin/sleep", ft_split(pipes->input_split[i], ' '), c->envp);
if (!ft_strncmp(pipes->input_split[i], "ls ", 3))
{
write(1, "\na\n\n", 4);
execve("/bin/ls", ft_split(pipes->input_split[i], ' '), c->envp);
}
} }
if (pid > 0) // parent if (pid > 0) // parent
{ {
wait(0); wait(0);
} }
i++;
} }
} }
@@ -112,7 +156,7 @@ void shell_loop(t_all *c)
if (line_input && *line_input) if (line_input && *line_input)
{ {
pipes = fill_pipes(line_input); pipes = fill_pipes(line_input);
execute_cmd(c, line_input, pipes); execute_cmd(c, pipes);
// 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);