From 1c8892983e56da70af28536f421e14870a0a73a4 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Wed, 29 Mar 2023 19:23:18 +0200 Subject: [PATCH] fix: multiple invalid redirection --- redirection/check.c | 38 +++++++++++++++++++++++-------- redirection/redirection.c | 5 ++-- redirection/redirection_private.h | 4 ++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/redirection/check.c b/redirection/check.c index f53f880..b748512 100644 --- a/redirection/check.c +++ b/redirection/check.c @@ -6,20 +6,25 @@ /* By: cchauvet fd_in[0] = ft_heredoc(data, redirection); + if (cmd->fd_in[0] == -2) + return (1); + } + return (0); } -static void ft_check_infile(t_data *data, t_cmd *cmd, +static bool ft_check_infile(t_data *data, t_cmd *cmd, char *redirection_identifier, char *redirection) { if (ft_strcmp(redirection_identifier, "<") == 0) @@ -27,11 +32,15 @@ static void ft_check_infile(t_data *data, t_cmd *cmd, if (ft_file_is_readable(data, redirection)) cmd->fd_in[0] = open(redirection, O_RDONLY); else + { cmd->fd_in[0] = -2; + return (1); + } } + return (0); } -static void ft_check_outfile(t_data *data, t_cmd *cmd, +static bool ft_check_outfile(t_data *data, t_cmd *cmd, char *redirection_identifier, char *redirection) { if (ft_strcmp(redirection_identifier, ">") == 0) @@ -40,11 +49,15 @@ static void ft_check_outfile(t_data *data, t_cmd *cmd, cmd->fd_out[0] = open(redirection, O_WRONLY | O_TRUNC | O_CREAT, 0644); else + { cmd->fd_out[0] = -2; + return (1); + } } + return (0); } -static void ft_check_outfile_append(t_data *data, t_cmd *cmd, +static bool ft_check_outfile_append(t_data *data, t_cmd *cmd, char *redirection_identifier, char *redirection) { if (ft_strcmp(redirection_identifier, ">>") == 0) @@ -53,15 +66,20 @@ static void ft_check_outfile_append(t_data *data, t_cmd *cmd, cmd->fd_out[0] = open(redirection, O_WRONLY | O_APPEND | O_CREAT, 0644); else + { cmd->fd_out[0] = -2; + return (1); + } } + return (0); } -void ft_check_redirection(t_data *data, t_cmd *cmd, +bool ft_check_redirection(t_data *data, t_cmd *cmd, char *redirection_identifier, char *redirection) { - ft_check_heredoc(data, cmd, redirection_identifier, redirection); - ft_check_infile(data, cmd, redirection_identifier, redirection); - ft_check_outfile(data, cmd, redirection_identifier, redirection); - ft_check_outfile_append(data, cmd, redirection_identifier, redirection); + return (ft_check_heredoc(data, cmd, redirection_identifier, redirection) + || ft_check_infile(data, cmd, redirection_identifier, redirection) + || ft_check_outfile(data, cmd, redirection_identifier, redirection) + || ft_check_outfile_append(data, cmd, redirection_identifier, + redirection)); } diff --git a/redirection/redirection.c b/redirection/redirection.c index c106d37..10bc034 100644 --- a/redirection/redirection.c +++ b/redirection/redirection.c @@ -6,7 +6,7 @@ /* By: cchauvet