mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
here_doc: print error if failed to create temp file
This commit is contained in:
parent
66ea1328aa
commit
1c4733b1cc
4 changed files with 37 additions and 6 deletions
1
Makefile
1
Makefile
|
|
@ -26,6 +26,7 @@ srcs = \
|
||||||
src/env/env_manip.c \
|
src/env/env_manip.c \
|
||||||
src/env/envp.c \
|
src/env/envp.c \
|
||||||
src/executing/here_doc/here_doc.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/here_doc_expand_line.c \
|
||||||
src/executing/here_doc/random_filename.c \
|
src/executing/here_doc/random_filename.c \
|
||||||
src/executing/here_doc/strip_newline.c \
|
src/executing/here_doc/strip_newline.c \
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 11:42:29 by khais #+# #+# */
|
/* 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 <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
#include "../../ft_errno.h"
|
||||||
|
|
||||||
char *expand_line(char *line, t_minishell *app);
|
char *expand_line(char *line, t_minishell *app);
|
||||||
|
void failed_to_open_tmp_file(void);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** - check that arguments are not invalid
|
** - check that arguments are not invalid
|
||||||
|
|
@ -30,13 +32,13 @@ static int setup_here_doc(t_worddesc *marker, int infd, char **filename)
|
||||||
int outfd;
|
int outfd;
|
||||||
|
|
||||||
if (infd < 0 || marker == NULL || marker->word == NULL)
|
if (infd < 0 || marker == NULL || marker->word == NULL)
|
||||||
return (-1);
|
return (ft_errno(FT_EINVAL), -1);
|
||||||
*filename = here_doc_random_filename();
|
*filename = here_doc_random_filename();
|
||||||
if (filename == NULL)
|
if (filename == NULL)
|
||||||
return (-1);
|
return (ft_errno(FT_EERRNO), -1);
|
||||||
outfd = open(*filename, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
|
outfd = open(*filename, O_CREAT | O_WRONLY | O_EXCL, S_IRUSR | S_IWUSR);
|
||||||
if (outfd < 0)
|
if (outfd < 0)
|
||||||
return (free(*filename), -1);
|
return (ft_errno(FT_EERRNO), free(*filename), -1);
|
||||||
return (outfd);
|
return (outfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -60,6 +62,8 @@ static int finalize(int outfd, char *filename)
|
||||||
outfd = open(filename, O_RDONLY, 0);
|
outfd = open(filename, O_RDONLY, 0);
|
||||||
unlink(filename);
|
unlink(filename);
|
||||||
free(filename);
|
free(filename);
|
||||||
|
if (outfd < 0)
|
||||||
|
ft_errno(FT_EERRNO);
|
||||||
return (outfd);
|
return (outfd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,7 +112,7 @@ int here_doc(t_worddesc *marker, int infd, t_minishell *app)
|
||||||
|
|
||||||
outfd = setup_here_doc(marker, infd, &filename);
|
outfd = setup_here_doc(marker, infd, &filename);
|
||||||
if (outfd < 0)
|
if (outfd < 0)
|
||||||
return (-1);
|
return (failed_to_open_tmp_file(), -1);
|
||||||
line = get_next_line(infd);
|
line = get_next_line(infd);
|
||||||
while (line != NULL)
|
while (line != NULL)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
20
src/executing/here_doc/here_doc_errors.c
Normal file
20
src/executing/here_doc/here_doc_errors.c
Normal 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");
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 11:43:32 by khais #+# #+# */
|
/* 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 <fcntl.h>
|
||||||
#include "../src/executing/here_doc/here_doc.h"
|
#include "../src/executing/here_doc/here_doc.h"
|
||||||
#include "../src/env/env_manip.h"
|
#include "../src/env/env_manip.h"
|
||||||
|
#include "../src/ft_errno.h"
|
||||||
|
|
||||||
static void test_here_doc_filename_generation(void)
|
static void test_here_doc_filename_generation(void)
|
||||||
{
|
{
|
||||||
|
|
@ -24,6 +25,7 @@ static void test_here_doc_filename_generation(void)
|
||||||
char *filename2;
|
char *filename2;
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
ft_errno(FT_ESUCCESS);
|
||||||
filename1 = here_doc_random_filename();
|
filename1 = here_doc_random_filename();
|
||||||
filename2 = here_doc_random_filename();
|
filename2 = here_doc_random_filename();
|
||||||
assert(filename1 != NULL);
|
assert(filename1 != NULL);
|
||||||
|
|
@ -41,6 +43,7 @@ static void test_here_doc_invalid_args(void)
|
||||||
t_worddesc *marker;
|
t_worddesc *marker;
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
ft_errno(FT_ESUCCESS);
|
||||||
assert(-1 == here_doc(NULL, 0, NULL));
|
assert(-1 == here_doc(NULL, 0, NULL));
|
||||||
marker = worddesc_create(NULL, 0, NULL);
|
marker = worddesc_create(NULL, 0, NULL);
|
||||||
assert(-1 == here_doc(marker, 0, NULL));
|
assert(-1 == here_doc(marker, 0, NULL));
|
||||||
|
|
@ -58,6 +61,7 @@ static void test_here_doc_only_end_marker(void)
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
ft_errno(FT_ESUCCESS);
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
|
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
|
||||||
infile = open("./here_doc_only_eof.input", O_RDONLY);
|
infile = open("./here_doc_only_eof.input", O_RDONLY);
|
||||||
result = here_doc(marker, infile, NULL);
|
result = here_doc(marker, infile, NULL);
|
||||||
|
|
@ -76,6 +80,7 @@ static void test_here_doc_input_plus_end_marker(void)
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
ft_errno(FT_ESUCCESS);
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
|
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
|
||||||
infile = open("./here_doc_input_plus_eof.input", O_RDONLY);
|
infile = open("./here_doc_input_plus_eof.input", O_RDONLY);
|
||||||
result = here_doc(marker, infile, NULL);
|
result = here_doc(marker, infile, NULL);
|
||||||
|
|
@ -115,6 +120,7 @@ static void test_here_doc_with_expansion(void)
|
||||||
ft_bzero(&app, sizeof(t_minishell));
|
ft_bzero(&app, sizeof(t_minishell));
|
||||||
app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin"));
|
app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin"));
|
||||||
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
ft_errno(FT_ESUCCESS);
|
||||||
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
|
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
|
||||||
infile = open("./here_doc_with_expansion.input", O_RDONLY);
|
infile = open("./here_doc_with_expansion.input", O_RDONLY);
|
||||||
result = here_doc(marker, infile, &app);
|
result = here_doc(marker, infile, &app);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue