tests de pipes et fork et execve et wait

This commit is contained in:
hugogogo
2021-10-17 22:12:55 +02:00
parent 98f247a378
commit 226829b891
4 changed files with 94 additions and 25 deletions

View File

@@ -6,15 +6,48 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 05:59:26 by lperrey #+# #+# */
/* Updated: 2021/10/15 17:33:46 by hulamy ### ########.fr */
/* Updated: 2021/10/17 22:10:48 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int count_pipes(char *input)
{
int i;
int nb;
i = -1;
nb = 0;
while (input[++i])
if (input[i] == '|')
nb++;
return (nb);
}
int **create_pipes(int nb)
{
int **pipes_fd;
int i;
pipes_fd = calloc(nb, sizeof(int *));
i = 0;
while(i < nb)
{
pipes_fd[i] = calloc(2, sizeof(int));
pipe(pipes_fd[i]);
printf("%i - %i\n", pipes_fd[i][0],pipes_fd[i][1]);
i++;
}
return (pipes_fd);
}
void shell_loop(t_all *c)
{
char *line_input;
int nb_pipes;
int **pipes_fd;
// pid_t pid;
line_input = NULL;
while (1)
@@ -24,14 +57,22 @@ void shell_loop(t_all *c)
line_input = readline(c->prompt);
if (line_input && *line_input)
{
if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
builtin_env(0, NULL, c);
else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder
builtin_exit(0, NULL, c);
else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp
pipes_hugo(line_input);
else
printf("echo: %s\n", line_input);
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);
// if (!ft_strncmp(line_input, "env", 4)) // temp placeholder
// builtin_env(0, NULL, c);
// else if (!ft_strncmp(line_input, "exit", 5)) // temp placeholder
// builtin_exit(0, NULL, c);
// else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp
// else if (!ft_strncmp(line_input, "pipe ", 5)) // temp temp temp
// pipes_hugo(line_input, c);
// else
// printf("echo: %s\n", line_input);
}
}
}

View File

@@ -17,20 +17,37 @@ void print_tab(char **array)
i = 0;
while (array[i])
{
printf("%i: %s\n", i, array[i]);
printf("%i: [%s]\n", i, array[i]);
i++;
}
}
void pipes_hugo(char *input)
void execute_cmd(char *cmd, t_all *c)
{
const char *filename;
char **argv;
char **envp;
// (void)cmd;
filename = ft_strtrim(cmd, " ");
printf("[%s]\n", filename);
// filename = "/bin/ls";
argv = ft_split(filename, ' ');
envp = c->envp;
execve(filename, argv, envp);
}
void pipes_hugo(char *input, t_all *c)
{
char **split;
int nbr_pipes;
int i;
split = ft_split(input, '|');
split[0] += 5; // pour sauter la premiere entree qui est "pipe"
nbr_pipes = size_tab(split);
//printf("%i\n", nbr_pipes);
//print_tab(split);
i = 0;
execute_cmd(split[i], c);
}