diff --git a/Makefile b/Makefile index 1343c68..1f9af05 100644 --- a/Makefile +++ b/Makefile @@ -18,9 +18,6 @@ bonus: ${OBJS_BONUS} make -C libft ${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${LIBS} -%.o:%.c - ${CC} ${CFLAGS} -c -o $@ $< - clean: rm -f ${OBJS} rm -f ${OBJS_BONUS} diff --git a/bonus/.pipex.c.swp b/bonus/.pipex.c.swp new file mode 100644 index 0000000..e52bb4f Binary files /dev/null and b/bonus/.pipex.c.swp differ diff --git a/bonus/pipex.c b/bonus/pipex.c index 2fbcc42..67b2ee8 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/02/14 14:08:49 by erey-bet ### ########.fr */ +/* Updated: 2023/03/07 16:22:34 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ int free_all(t_data *data) int i; int y; - if (data->fd1 > 0 && data->cmds[0]) + if (data->fd1 > 0) close(data->fd1); close(data->fd2); y = -1; @@ -38,12 +38,20 @@ int free_all(t_data *data) void make_cmd(t_data data, int i, int fl1, int fl2) { - dup2(fl1, 0); + if (fl1 > 0) + dup2(fl1, 0); dup2(fl2, 1); close(data.fd[0][0]); close(data.fd[0][1]); - close(data.fd[1][0]); - close(data.fd[1][1]); + if (i > 0) + { + close(data.fd[1][0]); + close(data.fd[1][1]); + } + if (data.fd1 > 0) + close(data.fd1); + if (data.fd2 > 0) + close(data.fd2); execve(data.cmds[i], data.flgs[i], data.env); } @@ -55,6 +63,7 @@ void cmd(t_data data, int i, int t) make_cmd(data, i, data.fd[(i + 1) % 2][0], data.fd[i % 2][1]); else if (t == 2) make_cmd(data, i, data.fd[(i + 1) % 2][0], data.fd2); + write(2, "Error execve\n", 13); free_all(&data); exit(1); } @@ -64,19 +73,11 @@ int pipex(int argc, t_data data) int *fk; int i; - if (pipe(data.fd[0]) < -1) - return (1); - if (pipe(data.fd[1]) < -1) - return (1); fk = ft_calloc(argc - 3, sizeof(int)); + if (!fk) + return (1); if (pipex_loop(argc, &fk, data)) return (1); - close(data.fd[0][0]); - close(data.fd[0][1]); - close(data.fd[1][0]); - close(data.fd[1][1]); - if (data.fd1 > 0 && data.cmds[0]) - waitpid(fk[0], NULL, 0); i = -1; while (++i < argc - 3) waitpid(fk[i], NULL, 0); @@ -88,8 +89,11 @@ int main(int argc, char *argv[], char **env) { t_data data; - if (argc < 5) + if (argc < 5 || (!ft_strncmp(argv[1], "here_doc", 8) && argc < 6)) + { + write(2, "Not enought argument\n", 21); return (1); + } if (ft_strncmp(argv[1], "here_doc", 8) == 0) { argc = argc - 1; diff --git a/bonus/pipex.h b/bonus/pipex.h index 9319255..55aa840 100644 --- a/bonus/pipex.h +++ b/bonus/pipex.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */ -/* Updated: 2023/02/14 14:02:35 by erey-bet ### ########.fr */ +/* Updated: 2023/03/05 23:38:16 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/bonus/pipex_heredoc.c b/bonus/pipex_heredoc.c index 0bdb07c..b712f86 100644 --- a/bonus/pipex_heredoc.c +++ b/bonus/pipex_heredoc.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/14 11:22:13 by erey-bet #+# #+# */ -/* Updated: 2023/02/14 14:59:59 by erey-bet ### ########.fr */ +/* Updated: 2023/03/07 15:46:36 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,12 @@ int parsing_here_doc(int argc, char *argv[], t_data *data) { int i; - data->fd2 = open(argv[argc], O_WRONLY | O_TRUNC | O_CREAT); + data->fd2 = open(argv[argc], O_WRONLY | O_APPEND | O_CREAT, 0644); if (data->fd2 < 0) - return (1); + { + close(data->fd1); + return (print_error(0, argv[argc])); + } data->cmds = ft_calloc(argc - 2, sizeof(char *)); data->flgs = ft_calloc(argc - 2, sizeof(char *)); i = -1; diff --git a/bonus/pipex_parsing.c b/bonus/pipex_parsing.c index e368920..d1f1bea 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/01/30 14:19:02 by erey-bet ### ########.fr */ +/* Updated: 2023/03/07 14:55:20 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,12 @@ int parsing(int argc, char *argv[], char **env, t_data *data) { int i; - data->fd2 = open(argv[argc - 1], O_WRONLY | O_TRUNC | O_CREAT); + data->fd2 = open(argv[argc - 1], O_WRONLY | O_TRUNC | O_CREAT, 0644); if (data->fd2 < 0) - return (1); + return (print_error(0, argv[argc - 1])); data->fd1 = open(argv[1], O_RDONLY); if (data->fd1 < 0) - return (print_error(0, argv[1])); + print_error(0, argv[1]); data->cmds = ft_calloc(argc - 2, sizeof(char *)); data->flgs = ft_calloc(argc - 2, sizeof(char *)); i = -1; @@ -56,23 +56,19 @@ char *test_acces(char **s, char **cmd, char **path, int *y) char *cmd_exist(char **env, char **cmd, int y, char **s) { char *path; - int i; + size_t i; - i = 5; - while ((*env)[i]) + i = 4; + while (i < ft_strlen(*env)) { - if ((*env)[i] != ':') + test_acces(s, cmd, &path, &y); + if (path) + return (path); + *s = ft_calloc(ft_strlen(*env) + 1, 1); + if (*s == NULL) + return (NULL); + while ((*env)[++i] != ':') (*s)[y++] = (*env)[i]; - else - { - test_acces(s, cmd, &path, &y); - if (path) - return (path); - *s = ft_calloc(ft_strlen(*env) + 1, 1); - if (*s == NULL) - return (NULL); - } - i++; } free(*s); free(*cmd); diff --git a/bonus/pipex_utils.c b/bonus/pipex_utils.c index 1727914..75fa77f 100644 --- a/bonus/pipex_utils.c +++ b/bonus/pipex_utils.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */ -/* Updated: 2023/01/20 17:48:55 by erey-bet ### ########.fr */ +/* Updated: 2023/03/06 13:24:53 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,17 +30,13 @@ int print_error(int type, char *error) { if (type == 0) { - write(2, "zsh: no such file or directory: ", 32); + write(2, "pipex: no such file or directory: ", 35); write(2, error, ft_strlen(error)); } - if (type == 1) + else { - write(2, "zsh: command not found: ", 24); - if (get_next(error, ' ') == -1) - write(2, error, ft_strlen(error)); - else - write(2, error, ft_strlen(error) - - (ft_strlen(error) - get_next(error, ' '))); + write(2, "pipex: command not found: ", 26); + ft_putstr_fd(error, 2); } write(2, "\n", 1); return (1); diff --git a/bonus/pipex_utils2.c b/bonus/pipex_utils2.c index a704d7a..d175b9d 100644 --- a/bonus/pipex_utils2.c +++ b/bonus/pipex_utils2.c @@ -6,12 +6,35 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/14 15:46:08 by erey-bet #+# #+# */ -/* Updated: 2023/02/14 15:48:06 by erey-bet ### ########.fr */ +/* Updated: 2023/03/07 16:17:51 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" +static int open_close_pipe(t_data *data, int boolean, int file) +{ + if (!boolean) + { + if (!file) + if (pipe(data->fd[0]) < 0) + return (1); + if (file) + if (pipe(data->fd[1]) < 0) + return (1); + } + else + { + if (!file) + if (close(data->fd[0][0]) < 0 || close(data->fd[0][1]) < 0) + return (1); + if (file) + if (close(data->fd[1][0]) < 0 || close(data->fd[1][1]) < 0) + return (1); + } + return (0); +} + int pipex_loop(int argc, int **fk, t_data data) { int i; @@ -19,20 +42,25 @@ int pipex_loop(int argc, int **fk, t_data data) i = -1; while (++i < argc - 3) { + 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 && data.fd1 > 0 && data.cmds[0]) + if (i == 0 && data.cmds[0]) cmd(data, i, 0); - else if (i + 1 < argc - 3) - cmd(data, i, 1); - else + else if (i + 1 >= argc - 3) cmd(data, i, 2); + else + cmd(data, i, 1); } + if (i > 0) + if (open_close_pipe(&data, 1, (i + 1) % 2)) + return (1); } - return (0); + return (open_close_pipe(&data, 1, (i + 1) % 2)); } int verification_command(char *argv[], t_data data) diff --git a/input b/input deleted file mode 100644 index ae481d0..0000000 --- a/input +++ /dev/null @@ -1,10 +0,0 @@ -QEQFEQFEfefafaa -f - - - -fse - - -wwaqqqf -qqea diff --git a/mandatory/pipex.c b/mandatory/pipex.c index 1101316..54fce8e 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/01/20 14:53:55 by erey-bet ### ########.fr */ +/* Updated: 2023/03/07 09:56:49 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,10 @@ int free_all(t_data *data) { int i; - if (data->fd1 > 0 && data->cmd1) + if (data->fd1 > 0) close(data->fd1); - close(data->fd2); + if (data->fd2 > 0) + close(data->fd2); i = 0; while (data->flg1[i]) free(data->flg1[i++]); @@ -38,6 +39,10 @@ void first_cmd(int fd[2], t_data data) dup2(fd[1], 1); close(fd[0]); close(fd[1]); + if (data.fd2 > 0) + close(data.fd2); + if (data.fd1 > 0) + close(data.fd1); execve(data.cmd1, data.flg1, data.env); free_all(&data); exit(1); @@ -49,6 +54,10 @@ void second_cmd(int fd[2], t_data data) dup2(data.fd2, 1); close(fd[0]); close(fd[1]); + if (data.fd2 > 0) + close(data.fd2); + if (data.fd1 > 0) + close(data.fd1); execve(data.cmd2, data.flg2, data.env); free_all(&data); exit(1); @@ -62,7 +71,7 @@ int pipex(t_data data) if (pipe(fd) < -1) return (1); - if (data.fd1 > 0 && data.cmd1) + if (data.cmd1) { cmd1 = fork(); if (cmd1 < 0) @@ -88,7 +97,10 @@ int main(int argc, char *argv[], char **env) t_data data; if (argc != 5) + { + write(2, "Bad argument's number\n", 22); return (1); + } if (parsing(argv, &data)) return (2); data.cmd1 = find_command(env, data.cmd1); diff --git a/mandatory/pipex.h b/mandatory/pipex.h index 7642500..8dcddc5 100644 --- a/mandatory/pipex.h +++ b/mandatory/pipex.h @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */ -/* Updated: 2023/01/20 14:55:13 by erey-bet ### ########.fr */ +/* Updated: 2023/02/28 12:59:46 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/mandatory/pipex_parsing.c b/mandatory/pipex_parsing.c index cbb400e..dd1fe4a 100644 --- a/mandatory/pipex_parsing.c +++ b/mandatory/pipex_parsing.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */ -/* Updated: 2023/02/14 16:49:31 by erey-bet ### ########.fr */ +/* Updated: 2023/03/07 09:24:12 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,14 +14,21 @@ int parsing(char *argv[], t_data *data) { - data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); + data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT, 0644); if (data->fd2 < 0) - return (1); + return (print_error(1, argv[4])); data->fd1 = open(argv[1], O_RDONLY); if (data->fd1 < 0) - return (print_error(0, argv[1])); + print_error(0, argv[1]); data->cmd1 = get_command(argv[2]); + if (ft_strlen(data->cmd1) < 1) + print_error(2, argv[2]); data->cmd2 = get_command(argv[3]); + if (ft_strlen(data->cmd2) < 1) + { + print_error(2, argv[3]); + return (1); + } data->flg1 = ft_split(argv[2], ' '); data->flg2 = ft_split(argv[3], ' '); data->fl1 = argv[1]; @@ -51,23 +58,19 @@ char *test_acces(char **s, char **cmd, char **path, int *y) char *cmd_exist(char **env, char **cmd, int y, char **s) { char *path; - int i; + size_t i; - i = 5; - while ((*env)[i]) + i = 4; + while (i < ft_strlen(*env)) { - if ((*env)[i] != ':') + test_acces(s, cmd, &path, &y); + if (path) + return (path); + *s = ft_calloc(ft_strlen(*env) + 1, 1); + if (*s == NULL) + return (NULL); + while ((*env)[++i] != ':') (*s)[y++] = (*env)[i]; - else - { - test_acces(s, cmd, &path, &y); - if (path) - return (path); - *s = ft_calloc(ft_strlen(*env) + 1, 1); - if (*s == NULL) - return (NULL); - } - i++; } free(*s); free(*cmd); diff --git a/mandatory/pipex_utils.c b/mandatory/pipex_utils.c index 16b582b..e3aca89 100644 --- a/mandatory/pipex_utils.c +++ b/mandatory/pipex_utils.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */ -/* Updated: 2023/01/20 14:55:47 by erey-bet ### ########.fr */ +/* Updated: 2023/03/02 13:39:41 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,17 +30,13 @@ int print_error(int type, char *error) { if (type == 0) { - write(2, "zsh: no such file or directory: ", 32); + write(2, "pipex: no such file or directory: ", 35); write(2, error, ft_strlen(error)); } - if (type == 1) + else { - write(2, "zsh: command not found: ", 24); - if (get_next(error, ' ') == -1) - write(2, error, ft_strlen(error)); - else - write(2, error, ft_strlen(error) - - (ft_strlen(error) - get_next(error, ' '))); + write(2, "pipex: command not found: ", 26); + ft_putstr_fd(error, 2); } write(2, "\n", 1); return (1); diff --git a/output b/output deleted file mode 100644 index e68b298..0000000 --- a/output +++ /dev/null @@ -1,3 +0,0 @@ -get_next_line -Makefile -pipex