heredocs + some leaks

This commit is contained in:
Loic Deridder
2025-02-11 11:07:57 +01:00
parent 0c76cc2422
commit 1ac81bef26
6 changed files with 244 additions and 52 deletions

View File

@@ -1,5 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 09:01:14 by lderidde #+# #+# */
/* Updated: 2025/02/11 10:45:08 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
static void remove_quote(char **str, char c)
{
char *new;
int i;
int k;
int len;
i = 0;
k = 0;
len = ft_strlen(*str);
new = ft_calloc(len - 1, sizeof(char));
while (i < len - 2)
{
if ((&((*str)[k]) == ft_strchr(*str, c)) ||
(&((*str)[k]) == ft_strrchr(*str, c)))
{
k++;
}
else
new[i++] = (*str)[k++];
}
ft_free(str);
*str = new;
}
static int ifremove_quote(char **str)
{
char c;
int ret;
ret = 0;
if (!ft_strchr(*str, '\'') && !ft_strchr(*str, '\"'))
c = 0;
else if (!ft_strchr(*str, '\"'))
c = '\'';
else if (!ft_strchr(*str, '\''))
c = '\"';
else if (ft_strchr(*str, '\'') < ft_strchr(*str, '\"'))
c = '\'';
else
c = '\"';
if (c && (ft_strchr(*str, c) != ft_strrchr(*str, c)))
remove_quote(str, c);
if (c)
ret = 1;
return (ret);
}
void read_hereinput(char *limiter)
{
char buf[100000];
@@ -14,6 +74,7 @@ void read_hereinput(char *limiter)
if (r == 0)
{
ft_fprintf (2, "\n");
ft_fprintf (1, "%s\n", limiter);
exit(EXIT_SUCCESS);
}
while (r && c != '\n' && c != '\0')
@@ -25,7 +86,7 @@ void read_hereinput(char *limiter)
buf[i] = '\0';
if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0)
{
ft_fprintf(1, "%s", buf);
ft_fprintf(1, "%s\n", buf);
exit(EXIT_SUCCESS);
}
buf[i++] = '\n';
@@ -42,6 +103,7 @@ void parse_heredoc(char *limiter)
pid = fork();
if (pid == 0)
{
ifremove_quote(&limiter);
dup2(fd, STDOUT_FILENO);
close (fd);
while (1)