diff options
| author | Christopher Uhlig | 2025-01-22 02:40:27 +0100 |
|---|---|---|
| committer | Christopher Uhlig | 2025-01-22 02:40:27 +0100 |
| commit | f6e474d27a1398c6d4f2e88c7f2d3797b85217da (patch) | |
| tree | 7dd7bd5f2a151b39498991b567ae51cff8242782 /src/read_heredoc.c | |
| parent | 78dc50a2bce3c6e31405437189e2990d8fc720ac (diff) | |
| download | minishell-f6e474d27a1398c6d4f2e88c7f2d3797b85217da.tar.gz minishell-f6e474d27a1398c6d4f2e88c7f2d3797b85217da.zip | |
kinda fix for pipe error and again.vs for you db just chnage workfolder gn
Diffstat (limited to 'src/read_heredoc.c')
| -rw-r--r-- | src/read_heredoc.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/read_heredoc.c b/src/read_heredoc.c new file mode 100644 index 0000000..53633e8 --- /dev/null +++ b/src/read_heredoc.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_heredoc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/15 18:22:09 by dkaiser #+# #+# */ +/* Updated: 2025/01/15 19:08:07 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include <errno.h> +#include <stdio.h> +#include <sys/errno.h> + +static char *concat_str(char *temp, char *line); +static char *get_result(char *temp, char *result, char *line); +static void *print_error_and_free(char *result); + +char *read_heredoc(char *delimiter) +{ + char *line; + char *result; + char *temp; + size_t total_length; + size_t line_length; + + total_length = 0; + result = NULL; + while (1) + { + line = readline("> "); + if (!line || ft_strcmp(line, delimiter) == 0) + { + free(line); + break ; + } + line_length = ft_strlen(line) + 1; + temp = malloc(total_length + line_length + 1); + if (!temp) + return (print_error_and_free(result)); + result = get_result(temp, result, line); + total_length += line_length; + } + return (result); +} + +static char *concat_str(char *temp, char *line) +{ + ft_strcat(temp, line); + ft_strcat(temp, "\n"); + free(line); + return (temp); +} + +static char *get_result(char *temp, char *result, char *line) +{ + if (result) + { + ft_strcpy(temp, result); + free(result); + } + else + temp[0] = '\0'; + return (concat_str(temp, line)); +} + +static void *print_error_and_free(char *result) +{ + errno = ENOMEM; + perror("heredoc"); + free(result); + return (NULL); +} |
