Correction Pipex

This commit is contained in:
Etienne Rey-bethbeder 2023-03-07 19:49:15 +01:00
parent ffa3031882
commit 5006061fa6
8 changed files with 39 additions and 28 deletions

BIN
.nfs000000000a95028900000189 Executable file

Binary file not shown.

BIN
.nfs000000000a9502930000018a Executable file

Binary file not shown.

View file

@ -0,0 +1 @@
a

View file

@ -30,7 +30,9 @@ fclean:
re: fclean all
.PHONY: all clean fclean re coffee
rebonus: fclean bonus
.PHONY: all clean fclean re rebonus coffee
coffee:
@clear

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */
/* Updated: 2023/03/07 16:22:34 by erey-bet ### ########.fr */
/* Updated: 2023/03/07 18:16:17 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */
/* Updated: 2023/03/07 14:55:20 by erey-bet ### ########.fr */
/* Updated: 2023/03/07 19:26:23 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -57,9 +57,11 @@ char *cmd_exist(char **env, char **cmd, int y, char **s)
{
char *path;
size_t i;
size_t len;
i = 4;
while (i < ft_strlen(*env))
len = ft_strlen(*env);
while (i < len)
{
test_acces(s, cmd, &path, &y);
if (path)
@ -67,7 +69,7 @@ char *cmd_exist(char **env, char **cmd, int y, char **s)
*s = ft_calloc(ft_strlen(*env) + 1, 1);
if (*s == NULL)
return (NULL);
while ((*env)[++i] != ':')
while ((*env)[++i] != ':' && i < len)
(*s)[y++] = (*env)[i];
}
free(*s);

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 15:46:08 by erey-bet #+# #+# */
/* Updated: 2023/03/07 16:17:51 by erey-bet ### ########.fr */
/* Updated: 2023/03/07 18:56:52 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,6 +35,16 @@ static int open_close_pipe(t_data *data, int boolean, int file)
return (0);
}
static void choice_command(int argc, t_data data, int i)
{
if (i == 0)
cmd(data, i, 0);
else if (i + 1 < argc - 3)
cmd(data, i, 1);
else
cmd(data, i, 2);
}
int pipex_loop(int argc, int **fk, t_data data)
{
int i;
@ -44,17 +54,13 @@ int pipex_loop(int argc, int **fk, t_data data)
{
if (open_close_pipe(&data, 0, i % 2))
return (1);
(*fk)[i] = fork();
if ((*fk)[i] < 0)
return (1);
if ((*fk)[i] == 0)
if ((i > 0 || (i == 0 && data.fd1 > 0)) && data.cmds[i])
{
if (i == 0 && data.cmds[0])
cmd(data, i, 0);
else if (i + 1 >= argc - 3)
cmd(data, i, 2);
else
cmd(data, i, 1);
(*fk)[i] = fork();
if ((*fk)[i] < 0)
return (1);
if ((*fk)[i] == 0)
choice_command(argc, data, i);
}
if (i > 0)
if (open_close_pipe(&data, 1, (i + 1) % 2))

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */
/* Updated: 2023/03/07 09:56:49 by erey-bet ### ########.fr */
/* Updated: 2023/03/07 17:06:38 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -66,29 +66,29 @@ void second_cmd(int fd[2], t_data data)
int pipex(t_data data)
{
int fd[2];
int cmd1;
int cmd2;
int f1;
int f2;
if (pipe(fd) < -1)
return (1);
if (data.cmd1)
if (data.fd1 > -1 && data.cmd1)
{
cmd1 = fork();
if (cmd1 < 0)
f1 = fork();
if (f1 < 0)
return (1);
if (cmd1 == 0)
if (f1 == 0)
first_cmd(fd, data);
}
cmd2 = fork();
if (cmd2 < 0)
f2 = fork();
if (f2 < 0)
return (1);
if (cmd2 == 0)
if (f2 == 0)
second_cmd(fd, data);
close(fd[0]);
close(fd[1]);
if (data.fd1 > 0 && data.cmd1)
waitpid(cmd1, NULL, 0);
waitpid(cmd2, NULL, 0);
waitpid(f1, NULL, 0);
waitpid(f2, NULL, 0);
return (0);
}