aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-15 19:08:20 +0100
committerDominik Kaiser2025-01-15 19:08:20 +0100
commit1032eefe7247cc5c240feedd2f49d8d69a326d79 (patch)
treedbf4e4292da3653755879b1450a74a5e9c9087d3
parent9f2424c0dca6073d1e97f290ad890a2ad7143ef1 (diff)
downloadminishell-1032eefe7247cc5c240feedd2f49d8d69a326d79.tar.gz
minishell-1032eefe7247cc5c240feedd2f49d8d69a326d79.zip
Refactor heredoc
-rw-r--r--src/read_heredoc.c41
1 files changed, 28 insertions, 13 deletions
diff --git a/src/read_heredoc.c b/src/read_heredoc.c
index 4b45b14..53633e8 100644
--- a/src/read_heredoc.c
+++ b/src/read_heredoc.c
@@ -6,13 +6,18 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/15 18:22:09 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 18:43:20 by dkaiser ### ########.fr */
+/* 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)
{
@@ -35,18 +40,8 @@ char *read_heredoc(char *delimiter)
line_length = ft_strlen(line) + 1;
temp = malloc(total_length + line_length + 1);
if (!temp)
- {
- perror("malloc");
- return (free(result), NULL);
- }
- if (result)
- {
- ft_strcpy(temp, result);
- free(result);
- }
- else
- temp[0] = '\0';
- result = concat_str(temp, line);
+ return (print_error_and_free(result));
+ result = get_result(temp, result, line);
total_length += line_length;
}
return (result);
@@ -59,3 +54,23 @@ static char *concat_str(char *temp, char *line)
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);
+}