here_doc: print error if failed to create temp file

This commit is contained in:
Khaïs COLIN 2025-03-28 18:28:27 +01:00
parent 66ea1328aa
commit 1c4733b1cc
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 37 additions and 6 deletions

View file

@ -26,6 +26,7 @@ srcs = \
src/env/env_manip.c \
src/env/envp.c \
src/executing/here_doc/here_doc.c \
src/executing/here_doc/here_doc_errors.c \
src/executing/here_doc/here_doc_expand_line.c \
src/executing/here_doc/random_filename.c \
src/executing/here_doc/strip_newline.c \

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 11:42:29 by khais #+# #+# */
/* Updated: 2025/03/28 16:43:47 by khais ### ########.fr */
/* Updated: 2025/03/28 19:07:45 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,8 +15,10 @@
#include <fcntl.h>
#include <unistd.h>
#include "libft.h"
#include "../../ft_errno.h"
char *expand_line(char *line, t_minishell *app);
void failed_to_open_tmp_file(void);
/*
** - check that arguments are not invalid
@ -30,13 +32,13 @@ static int setup_here_doc(t_worddesc *marker, int infd, char **filename)
int outfd;
if (infd < 0 || marker == NULL || marker->word == NULL)
return (-1);
return (ft_errno(FT_EINVAL), -1);
*filename = here_doc_random_filename();
if (filename == NULL)
return (-1);
return (ft_errno(FT_EERRNO), -1);
outfd = open(*filename, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
if (outfd < 0)
return (free(*filename), -1);
return (ft_errno(FT_EERRNO), free(*filename), -1);
return (outfd);
}
@ -60,6 +62,8 @@ static int finalize(int outfd, char *filename)
outfd = open(filename, O_RDONLY, 0);
unlink(filename);
free(filename);
if (outfd < 0)
ft_errno(FT_EERRNO);
return (outfd);
}
@ -108,7 +112,7 @@ int here_doc(t_worddesc *marker, int infd, t_minishell *app)
outfd = setup_here_doc(marker, infd, &filename);
if (outfd < 0)
return (-1);
return (failed_to_open_tmp_file(), -1);
line = get_next_line(infd);
while (line != NULL)
{

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* here_doc_errors.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 18:29:44 by khais #+# #+# */
/* Updated: 2025/03/28 19:03:45 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "libft.h"
#include "../../ft_errno.h"
void failed_to_open_tmp_file(void)
{
ft_perror("minishell: cannot create temp file for here-document");
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 11:43:32 by khais #+# #+# */
/* Updated: 2025/03/28 16:41:08 by khais ### ########.fr */
/* Updated: 2025/03/28 19:05:55 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@
#include <fcntl.h>
#include "../src/executing/here_doc/here_doc.h"
#include "../src/env/env_manip.h"
#include "../src/ft_errno.h"
static void test_here_doc_filename_generation(void)
{
@ -24,6 +25,7 @@ static void test_here_doc_filename_generation(void)
char *filename2;
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
ft_errno(FT_ESUCCESS);
filename1 = here_doc_random_filename();
filename2 = here_doc_random_filename();
assert(filename1 != NULL);
@ -41,6 +43,7 @@ static void test_here_doc_invalid_args(void)
t_worddesc *marker;
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
ft_errno(FT_ESUCCESS);
assert(-1 == here_doc(NULL, 0, NULL));
marker = worddesc_create(NULL, 0, NULL);
assert(-1 == here_doc(marker, 0, NULL));
@ -58,6 +61,7 @@ static void test_here_doc_only_end_marker(void)
int result;
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
ft_errno(FT_ESUCCESS);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
infile = open("./here_doc_only_eof.input", O_RDONLY);
result = here_doc(marker, infile, NULL);
@ -76,6 +80,7 @@ static void test_here_doc_input_plus_end_marker(void)
int result;
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
ft_errno(FT_ESUCCESS);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
infile = open("./here_doc_input_plus_eof.input", O_RDONLY);
result = here_doc(marker, infile, NULL);
@ -115,6 +120,7 @@ static void test_here_doc_with_expansion(void)
ft_bzero(&app, sizeof(t_minishell));
app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin"));
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
ft_errno(FT_ESUCCESS);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
infile = open("./here_doc_with_expansion.input", O_RDONLY);
result = here_doc(marker, infile, &app);