diff --git a/.nfs000000000a95028900000189 b/.nfs000000000a95028900000189 new file mode 100755 index 0000000..89a04aa Binary files /dev/null and b/.nfs000000000a95028900000189 differ diff --git a/.nfs000000000a9502930000018a b/.nfs000000000a9502930000018a new file mode 100755 index 0000000..e8b43e9 Binary files /dev/null and b/.nfs000000000a9502930000018a differ diff --git a/.nfs000000000a95034400000192 b/.nfs000000000a95034400000192 new file mode 100644 index 0000000..7898192 --- /dev/null +++ b/.nfs000000000a95034400000192 @@ -0,0 +1 @@ +a diff --git a/Makefile b/Makefile index 1f9af05..ac373bd 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/bonus/pipex.c b/bonus/pipex.c index 67b2ee8..ec58f67 100644 --- a/bonus/pipex.c +++ b/bonus/pipex.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/bonus/pipex_parsing.c b/bonus/pipex_parsing.c index d1f1bea..c98d020 100644 --- a/bonus/pipex_parsing.c +++ b/bonus/pipex_parsing.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/bonus/pipex_utils2.c b/bonus/pipex_utils2.c index d175b9d..29582b6 100644 --- a/bonus/pipex_utils2.c +++ b/bonus/pipex_utils2.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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)) diff --git a/mandatory/pipex.c b/mandatory/pipex.c index 54fce8e..e8eb6df 100644 --- a/mandatory/pipex.c +++ b/mandatory/pipex.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); }