Naji-k / minishell

1 stars 0 forks source link

protect malloc and exit correctly heredoc #101

Closed srondss closed 1 year ago

srondss commented 1 year ago

path & path_hd should not exit(EXIT_FAILURE) but rather use the malloc_error(void *str_to_free) function so that it prints an error message.

int execute_hd_process(t_tools *tools, t_token *redirection)
{
    int     file;
    char    *path;
    char    *path_hd;

    path_hd = ft_itoa(tools->heredoc);
    if (!path_hd)
        exit(EXIT_FAILURE); // need to change this one too
    path = ft_strjoin("/tmp/", path_hd);
    if (!path)
        exit(EXIT_FAILURE); // need to change
    file = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0644);
    if (file < 0)
    {
        free(path_hd);
        free(path);
        return (error_file_handling(redirection->cmd));
    }
    start_heredoc_loop(tools, redirection, file);
    close(file);
    return (0);
}
Naji-k commented 1 year ago

You are right. It should _exit, and not return because we are in the child process

if (!path_hd)
return (ERROR)

then in create_heredoc check if it is success or not, here _exit(0) || _exit(ERROR)