fix: multiple invalid redirection

This commit is contained in:
Camille Chauvet 2023-03-29 19:23:18 +02:00
parent c1e61780e0
commit 1c8892983e
3 changed files with 33 additions and 14 deletions

View File

@ -6,20 +6,25 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */ /* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
/* Updated: 2023/03/29 18:43:20 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 19:21:59 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "./redirection_private.h" #include "./redirection_private.h"
static void ft_check_heredoc(t_data *data, t_cmd *cmd, static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection) char *redirection_identifier, char *redirection)
{ {
if (ft_strcmp(redirection_identifier, "<<") == 0) if (ft_strcmp(redirection_identifier, "<<") == 0)
{
cmd->fd_in[0] = ft_heredoc(data, redirection); cmd->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) char *redirection_identifier, char *redirection)
{ {
if (ft_strcmp(redirection_identifier, "<") == 0) 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)) if (ft_file_is_readable(data, redirection))
cmd->fd_in[0] = open(redirection, O_RDONLY); cmd->fd_in[0] = open(redirection, O_RDONLY);
else else
{
cmd->fd_in[0] = -2; 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) char *redirection_identifier, char *redirection)
{ {
if (ft_strcmp(redirection_identifier, ">") == 0) 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, cmd->fd_out[0] = open(redirection,
O_WRONLY | O_TRUNC | O_CREAT, 0644); O_WRONLY | O_TRUNC | O_CREAT, 0644);
else else
{
cmd->fd_out[0] = -2; 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) char *redirection_identifier, char *redirection)
{ {
if (ft_strcmp(redirection_identifier, ">>") == 0) 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, cmd->fd_out[0] = open(redirection,
O_WRONLY | O_APPEND | O_CREAT, 0644); O_WRONLY | O_APPEND | O_CREAT, 0644);
else else
{
cmd->fd_out[0] = -2; 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) char *redirection_identifier, char *redirection)
{ {
ft_check_heredoc(data, cmd, redirection_identifier, redirection); return (ft_check_heredoc(data, cmd, redirection_identifier, redirection)
ft_check_infile(data, cmd, redirection_identifier, redirection); || ft_check_infile(data, cmd, redirection_identifier, redirection)
ft_check_outfile(data, cmd, redirection_identifier, redirection); || ft_check_outfile(data, cmd, redirection_identifier, redirection)
ft_check_outfile_append(data, cmd, redirection_identifier, redirection); || ft_check_outfile_append(data, cmd, redirection_identifier,
redirection));
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */
/* Updated: 2023/03/29 19:11:46 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 19:19:51 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -79,7 +79,8 @@ int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {
ft_quote_remover(tab[i + 1]); ft_quote_remover(tab[i + 1]);
ft_check_redirection(data, cmd, tab[i], tab[i + 1]); if (ft_check_redirection(data, cmd, tab[i], tab[i + 1]))
return (1);
i++; i++;
} }
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:44:31 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:44:31 by cchauvet #+# #+# */
/* Updated: 2023/03/29 18:21:45 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 19:19:21 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,7 +24,7 @@
int ft_file_is_readable(t_data *data, const char *path); int ft_file_is_readable(t_data *data, const char *path);
int ft_file_is_writable(t_data *data, const char *path); int ft_file_is_writable(t_data *data, const char *path);
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); char *redirection_identifier, char *redirection);
int ft_file_is_appendable(t_data *data, const char *path); int ft_file_is_appendable(t_data *data, const char *path);
int ft_heredoc(t_data *data, char *stop); int ft_heredoc(t_data *data, char *stop);