From 345fb81a9181603873fbb09c8255754409fa8bc4 Mon Sep 17 00:00:00 2001 From: Vadim Tolmachev Date: Thu, 14 Oct 2021 01:18:57 +0300 Subject: [PATCH] add bonus part --- bonus/child_bonus.c | 61 ++++++++++++++++++++ bonus/error_bonus.c | 32 +++++++++++ bonus/files_bonus.c | 42 ++++++++++++++ bonus/free_bonus.c | 56 +++++++++++++++++++ bonus/here_doc_bonus.c | 56 +++++++++++++++++++ bonus/pipex_bonus.c | 65 +++++++++++++++++++++ gnl/get_next_line.c | 48 ++++++++++++++++ gnl/get_next_line.h | 33 +++++++++++ gnl/get_next_line_utils.c | 115 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 508 insertions(+) create mode 100644 bonus/child_bonus.c create mode 100644 bonus/error_bonus.c create mode 100644 bonus/files_bonus.c create mode 100644 bonus/free_bonus.c create mode 100644 bonus/here_doc_bonus.c create mode 100644 bonus/pipex_bonus.c create mode 100644 gnl/get_next_line.c create mode 100644 gnl/get_next_line.h create mode 100644 gnl/get_next_line_utils.c diff --git a/bonus/child_bonus.c b/bonus/child_bonus.c new file mode 100644 index 0000000..5615585 --- /dev/null +++ b/bonus/child_bonus.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* child_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere infile = open(argv[1], O_RDONLY); + if (pipex->infile < 0) + msg_error(ERR_INFILE); + } +} + +void get_outfile(char *argv, t_ppxb *pipex) +{ + if (pipex->here_doc) + pipex->outfile = open(argv, O_WRONLY | O_CREAT | O_APPEND, 0000644); + else + pipex->outfile = open(argv, O_CREAT | O_RDWR | O_TRUNC, 0000644); + if (pipex->outfile < 0) + msg_error(ERR_OUTFILE); +} diff --git a/bonus/free_bonus.c b/bonus/free_bonus.c new file mode 100644 index 0000000..06a4cd0 --- /dev/null +++ b/bonus/free_bonus.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* free_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere infile); + close(pipex->outfile); + if (pipex->here_doc) + unlink(".heredoc_tmp"); + while (pipex->cmd_paths[i]) + { + free(pipex->cmd_paths[i]); + i++; + } + free(pipex->cmd_paths); + free(pipex->pipe); +} + +void child_free(t_ppxb *pipex) +{ + int i; + + i = 0; + while (pipex->cmd_args[i]) + { + free(pipex->cmd_args[i]); + i++; + } + free(pipex->cmd_args); + free(pipex->cmd); +} + +void pipe_free(t_ppxb *pipex) +{ + close(pipex->infile); + close(pipex->outfile); + if (pipex->here_doc) + unlink(".heredoc_tmp"); + free(pipex->pipe); + msg(ERR_ENVP); + exit(1); +} diff --git a/bonus/here_doc_bonus.c b/bonus/here_doc_bonus.c new file mode 100644 index 0000000..0e4ed68 --- /dev/null +++ b/bonus/here_doc_bonus.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* here_doc_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere here_doc = 1; + return (6); + } + else + { + pipex->here_doc = 0; + return (5); + } +} + +void here_doc(char *argv, t_ppxb *pipex) +{ + int file; + char *buf; + + file = open(".heredoc_tmp", O_CREAT | O_WRONLY | O_TRUNC, 0000644); + if (file < 0) + msg_error(ERR_HEREDOC); + while (1) + { + write(1, "heredoc> ", 9); + if (get_next_line(0, &buf) < 0) + exit(1); + if (!ft_strncmp(argv, buf, ft_strlen(argv) + 1)) + break ; + write(file, buf, ft_strlen(buf)); + write(file, "\n", 1); + free(buf); + } + free(buf); + close(file); + pipex->infile = open(".heredoc_tmp", O_RDONLY); + if (pipex->infile < 0) + { + unlink(".heredoc_tmp"); + msg_error(ERR_HEREDOC); + } +} diff --git a/bonus/pipex_bonus.c b/bonus/pipex_bonus.c new file mode 100644 index 0000000..0128e36 --- /dev/null +++ b/bonus/pipex_bonus.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere cmd_nmbs - 1) + { + if (pipe(pipex->pipe + 2 * i) < 0) + parent_free(pipex); + i++; + } +} + +void close_pipes(t_ppxb *pipex) +{ + int i; + + i = 0; + while (i < (pipex->pipe_nmbs)) + { + close(pipex->pipe[i]); + i++; + } +} + +int main(int argc, char *argv[], char *envp[]) +{ + t_ppxb pipex; + + if (argc < args_in(argv[1], &pipex)) + return (msg(ERR_INPUT)); + get_infile(argv, &pipex); + get_outfile(argv[argc - 1], &pipex); + pipex.cmd_nmbs = argc - 3 - pipex.here_doc; + pipex.pipe_nmbs = 2 * (pipex.cmd_nmbs - 1); + pipex.pipe = (int *)malloc(sizeof(int) * pipex.pipe_nmbs); + if (!pipex.pipe) + msg_error(ERR_PIPE); + pipex.env_path = find_path(envp); + pipex.cmd_paths = ft_split(pipex.env_path, ':'); + if (!pipex.cmd_paths) + pipe_free(&pipex); + creat_pipes(&pipex); + pipex.idx = -1; + while (++(pipex.idx) < pipex.cmd_nmbs) + child(pipex, argv, envp); + close_pipes(&pipex); + waitpid(-1, NULL, 0); + parent_free(&pipex); + return (0); +} diff --git a/gnl/get_next_line.c b/gnl/get_next_line.c new file mode 100644 index 0000000..90f8f28 --- /dev/null +++ b/gnl/get_next_line.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere 0) + { + rd = read(fd, buf, BUFFER_SIZE); + if (rd < 0) + return (some_error(buf)); + buf[rd] = '\0'; + rem = str_join(rem, buf); + } + free(buf); + *line = get_line(rem); + rem = trim_rem(rem); + if (rd == 0 && !rem) + return (0); + return (1); +} diff --git a/gnl/get_next_line.h b/gnl/get_next_line.h new file mode 100644 index 0000000..6c9c2aa --- /dev/null +++ b/gnl/get_next_line.h @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere +# include +# include +# include + +# ifndef BUFFER_SIZE +# define BUFFER_SIZE 100 +# endif + +int get_next_line(int fd, char **line); +size_t ft_strlen(const char *s); +int find_nl(char *str); +char *str_join(char *s1, char *s2); +char *get_line(char *str); +char *trim_rem(char *str); +int some_error(char *str); + +#endif diff --git a/gnl/get_next_line_utils.c b/gnl/get_next_line_utils.c new file mode 100644 index 0000000..1cc9f4c --- /dev/null +++ b/gnl/get_next_line_utils.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_next_line_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bcaffere