creation struc pipes

This commit is contained in:
hugogogo
2021-10-18 11:28:54 +02:00
parent 7b817bb369
commit 6e32b4ae47
2 changed files with 53 additions and 20 deletions

View File

@@ -6,12 +6,21 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/18 08:59:59 by hulamy ### ########.fr */
/* Updated: 2021/10/18 11:25:43 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_tab(char **array);
typedef struct s_pipe
{
int nb_pipe;
int **pipes_fd;
char **input_split;
} t_pipe;
int count_pipes(char *input)
{
int i;
@@ -44,30 +53,55 @@ int **create_pipes(int nb)
return (pipes_fd);
}
void cmd_execution(t_all *c, int **pipes_fd, int nb_pipes, char *line_input)
char **split_pipes(char *input)
{
char **split;
int i;
split = ft_split(input, '|');
i = -1;
while (split[++i])
split[i] = ft_strtrim(split[i], " ");
// print_tab(split);
return (split);
}
t_pipe *fill_pipes(char *input)
{
t_pipe *pipes;
pipes->nb_pipe = count_pipes(input);
pipes->pipes_fd = create_pipes(pipes->nb_pipes);
pipes->input_split = split_pipes(input);
return (pipes);
}
void execute_cmd(t_all *c, char *line_input, t_all *pipes)
{
pid_t pid;
(void)pipes_fd;
if (!nb_pipes)
return ;
pid = fork();
if (pid == 0) // child
while (nb_pipes-- >= 0)
{
if (!ft_strncmp(line_input, "sleep ", 5))
execve("/bin/sleep", ft_split(line_input, ' '), c->envp);
}
if (pid > 0) // parent
{
wait(0);
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;
t_pipe *pipes;
// int nb_pipes;
// int **pipes_fd;
// char **split;
line_input = NULL;
while (1)
@@ -77,9 +111,8 @@ void shell_loop(t_all *c)
line_input = readline(c->prompt);
if (line_input && *line_input)
{
nb_pipes = count_pipes(line_input);
pipes_fd = create_pipes(nb_pipes);
cmd_execution(c, pipes_fd, nb_pipes, line_input);
pipes = fill_pipes(line_input);
execute_cmd(c, line_input, pipes);
// if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
// builtin_env(0, NULL, c);

View File

@@ -22,7 +22,7 @@ void print_tab(char **array)
}
}
void execute_cmd(char *cmd, t_all *c)
void cmd_execution(char *cmd, t_all *c)
{
const char *filename;
char **argv;
@@ -49,5 +49,5 @@ void pipes_hugo(char *input, t_all *c)
//printf("%i\n", nbr_pipes);
//print_tab(split);
i = 0;
execute_cmd(split[i], c);
cmd_execution(split[i], c);
}