fix(here_doc): correctly delete here_doc files in more cases

This commit is contained in:
Khaïs COLIN 2025-05-06 16:53:54 +02:00
parent 4e9f631918
commit fb92167494
10 changed files with 42 additions and 40 deletions

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */ /* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */
/* Updated: 2025/05/05 11:22:40 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:51:04 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,7 @@
#include "../simple_cmd/simple_cmd.h" #include "../simple_cmd/simple_cmd.h"
#include "libft.h" #include "libft.h"
void redirect_destroy(t_redirect *redirect) void redirect_destroy(t_redirect *redirect, bool delete_file)
{ {
t_redirect *next; t_redirect *next;
@ -25,7 +25,8 @@ void redirect_destroy(t_redirect *redirect)
free(redirect->here_doc_eof); free(redirect->here_doc_eof);
if (redirect->type == FT_HEREDOC if (redirect->type == FT_HEREDOC
&& redirect->redirectee.filename != NULL && redirect->redirectee.filename != NULL
&& redirect->redirectee.filename->word != NULL) && redirect->redirectee.filename->word != NULL
&& delete_file)
unlink(redirect->redirectee.filename->word); unlink(redirect->redirectee.filename->word);
worddesc_destroy(redirect->redirectee.filename); worddesc_destroy(redirect->redirectee.filename);
free(redirect->unexpanded_filename); free(redirect->unexpanded_filename);
@ -48,7 +49,7 @@ static void group_cmd_destroy(t_group_cmd *cmd)
if (cmd == NULL) if (cmd == NULL)
return ; return ;
cmd_destroy(cmd->cmd); cmd_destroy(cmd->cmd);
redirect_destroy(cmd->redirects); redirect_destroy(cmd->redirects, false);
free(cmd); free(cmd);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 16:52:31 by kcolin #+# #+# */ /* Created: 2025/04/09 16:52:31 by kcolin #+# #+# */
/* Updated: 2025/04/14 15:10:13 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:50:26 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,6 @@
# include "../../minishell.h" # include "../../minishell.h"
void cmd_destroy(t_cmd *cmd); void cmd_destroy(t_cmd *cmd);
void redirect_destroy(t_redirect *redirect); void redirect_destroy(t_redirect *redirect, bool delete_file);
#endif // CMD_DESTROY_H #endif // CMD_DESTROY_H

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/02 14:40:34 by kcolin #+# #+# */ /* Created: 2025/05/02 14:40:34 by kcolin #+# #+# */
/* Updated: 2025/05/02 14:40:34 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:50:52 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -83,7 +83,7 @@ t_redirect *redir_from_words(t_worddesc *operator,
here_doc(spec, STDIN_FILENO, app), 0, NULL, WORD_TOKEN); here_doc(spec, STDIN_FILENO, app), 0, NULL, WORD_TOKEN);
worddesc_destroy(spec); worddesc_destroy(spec);
if (redir->redirectee.filename == NULL) if (redir->redirectee.filename == NULL)
return (redirect_destroy(redir), NULL); return (redirect_destroy(redir, true), NULL);
} }
else else
redir->redirectee.filename = specifier; redir->redirectee.filename = specifier;

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 10:13:58 by kcolin #+# #+# */ /* Created: 2025/04/15 10:13:58 by kcolin #+# #+# */
/* Updated: 2025/04/17 11:45:24 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:57:51 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,11 +34,12 @@ t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist **tokens)
else else
{ {
worddesc_destroy(redir_operator); worddesc_destroy(redir_operator);
return (ft_errno(FT_EERRNO), redirect_destroy(redir_list), NULL); return (ft_errno(FT_EERRNO), redirect_destroy(redir_list, true),
NULL);
} }
new_redir = redir_from_words(redir_operator, redir_specifier, app); new_redir = redir_from_words(redir_operator, redir_specifier, app);
if (new_redir == NULL && ft_errno_get() != FT_ESUCCESS) if (new_redir == NULL && ft_errno_get() != FT_ESUCCESS)
return (redirect_destroy(redir_list), NULL); return (redirect_destroy(redir_list, true), NULL);
t_redirect_add_back(&redir_list, new_redir); t_redirect_add_back(&redir_list, new_redir);
} }
return (redir_list); return (redir_list);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* cmdgroup_remove_quotes.c :+: :+: :+: */ /* cmdgroup_remove_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 17:36:20 by kcolin #+# #+# */ /* Created: 2025/05/06 16:50:43 by kcolin #+# #+# */
/* Updated: 2025/05/02 12:53:03 by jguelen ### ########.fr */ /* Updated: 2025/05/06 16:50:43 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,10 +33,10 @@ static t_redirect *redirection_remove_quotes(t_redirect *in_list)
current->redirectee.filename = result; current->redirectee.filename = result;
if (current->redirectee.filename == NULL) if (current->redirectee.filename == NULL)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
ft_errno(FT_EERRNO); ft_errno(FT_EERRNO);
return (redirect_destroy(current), NULL); return (redirect_destroy(current, true), NULL);
} }
} }
out_list = t_redirect_add_back(&out_list, current); out_list = t_redirect_add_back(&out_list, current);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* simple_cmd.c :+: :+: :+: */ /* simple_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 12:30:07 by kcolin #+# #+# */ /* Created: 2025/05/06 16:52:05 by kcolin #+# #+# */
/* Updated: 2025/05/02 12:53:31 by jguelen ### ########.fr */ /* Updated: 2025/05/06 16:52:05 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,7 +32,7 @@ void simple_cmd_destroy(t_simple_cmd *cmd)
if (cmd == NULL) if (cmd == NULL)
return ; return ;
wordlist_destroy(cmd->words); wordlist_destroy(cmd->words);
redirect_destroy(cmd->redirections); redirect_destroy(cmd->redirections, false);
free(cmd); free(cmd);
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 13:34:51 by kcolin #+# #+# */ /* Created: 2025/04/01 13:34:51 by kcolin #+# #+# */
/* Updated: 2025/04/28 14:38:29 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:52:25 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,9 +36,9 @@ static t_redirect *redirection_var_expansion(t_redirect *in_list,
if (current->redirectee.filename == NULL if (current->redirectee.filename == NULL
&& ft_errno_get() != FT_ESUCCESS) && ft_errno_get() != FT_ESUCCESS)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
return (redirect_destroy(current), NULL); return (redirect_destroy(current, true), NULL);
} }
} }
out_list = t_redirect_add_back(&out_list, current); out_list = t_redirect_add_back(&out_list, current);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* expand_wildcard.c :+: :+: :+: */ /* expand_wildcard.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 19:28:28 by kcolin #+# #+# */ /* Created: 2025/05/06 16:51:47 by kcolin #+# #+# */
/* Updated: 2025/05/02 13:00:43 by jguelen ### ########.fr */ /* Updated: 2025/05/06 16:52:34 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,13 +30,13 @@ static t_redirect *redirect_expand_wildcard_one(t_redirect *current)
if (expansion_result != NULL) if (expansion_result != NULL)
{ {
ambiguous_redirect(current->unexpanded_filename); ambiguous_redirect(current->unexpanded_filename);
redirect_destroy(current); redirect_destroy(current, true);
wordlist_destroy(expansion_result); wordlist_destroy(expansion_result);
return (ft_errno(FT_EERRNO), NULL); return (ft_errno(FT_EERRNO), NULL);
} }
} }
else if (expansion_result == NULL && ft_errno_get() != FT_ESUCCESS) else if (expansion_result == NULL && ft_errno_get() != FT_ESUCCESS)
return (redirect_destroy(current), NULL); return (redirect_destroy(current, true), NULL);
return (current); return (current);
} }
@ -55,8 +55,8 @@ static t_redirect *redirect_expand_wildcards(t_redirect *in_list)
if (redirect_expand_wildcard_one(current) == NULL if (redirect_expand_wildcard_one(current) == NULL
&& ft_errno_get() != FT_ESUCCESS) && ft_errno_get() != FT_ESUCCESS)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
return (NULL); return (NULL);
} }
} }

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 11:54:16 by kcolin #+# #+# */ /* Created: 2025/04/21 11:54:16 by kcolin #+# #+# */
/* Updated: 2025/04/23 11:19:26 by kcolin ### ########.fr */ /* Updated: 2025/05/06 16:51:36 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,9 +20,9 @@
static void redirect_fieldsplit_cleanup(t_redirect *in_list, static void redirect_fieldsplit_cleanup(t_redirect *in_list,
t_redirect *out_list, t_redirect *current, t_simple_cmd *cmd) t_redirect *out_list, t_redirect *current, t_simple_cmd *cmd)
{ {
redirect_destroy(in_list); redirect_destroy(in_list, true);
redirect_destroy(out_list); redirect_destroy(out_list, true);
redirect_destroy(current); redirect_destroy(current, true);
cmd->redirections = NULL; cmd->redirections = NULL;
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* sig_handlers.c :+: :+: :+: */ /* sig_handlers.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 12:01:39 by kcolin #+# #+# */ /* Created: 2025/05/06 16:57:25 by kcolin #+# #+# */
/* Updated: 2025/05/05 12:03:03 by jguelen ### ########.fr */ /* Updated: 2025/05/06 16:57:27 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -70,4 +70,4 @@ void sig_heredoc(int signum, siginfo_t *siginfo, void *context)
ft_printf("\n"); ft_printf("\n");
} }
g_signum = signum; g_signum = signum;
} }