2023-03-29 13:07:57 -04:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* check.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
|
2023-04-17 08:16:41 -04:00
|
|
|
/* Updated: 2023/04/17 11:15:38 by cchauvet ### ########.fr */
|
2023-03-29 13:07:57 -04:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "./redirection_private.h"
|
2023-04-07 11:12:48 -04:00
|
|
|
#include <stdbool.h>
|
2023-04-17 08:16:41 -04:00
|
|
|
#include "../signal/signal.h"
|
|
|
|
#include <signal.h>
|
2023-03-29 13:07:57 -04:00
|
|
|
|
2023-03-29 13:23:18 -04:00
|
|
|
static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
|
2023-03-29 13:07:57 -04:00
|
|
|
char *redirection_identifier, char *redirection)
|
|
|
|
{
|
2023-04-04 08:51:34 -04:00
|
|
|
int fd;
|
|
|
|
|
2023-03-29 13:07:57 -04:00
|
|
|
if (ft_strcmp(redirection_identifier, "<<") == 0)
|
2023-03-29 13:23:18 -04:00
|
|
|
{
|
2023-04-05 08:13:56 -04:00
|
|
|
if (cmd->fd_in[0] == -2)
|
|
|
|
return (0);
|
2023-04-17 08:16:41 -04:00
|
|
|
signal(SIGINT, ft_ctrlc_heredoc);
|
2023-04-04 08:51:34 -04:00
|
|
|
fd = ft_heredoc(data, redirection);
|
2023-04-17 08:16:41 -04:00
|
|
|
signal(SIGINT, ft_ctrlc);
|
2023-04-04 08:51:34 -04:00
|
|
|
if (fd == -2)
|
2023-03-29 13:23:18 -04:00
|
|
|
return (1);
|
2023-04-04 08:51:34 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cmd->fd_in[0] > 2)
|
|
|
|
close(cmd->fd_in[0]);
|
|
|
|
cmd->fd_in[0] = fd;
|
|
|
|
}
|
2023-03-29 13:23:18 -04:00
|
|
|
}
|
|
|
|
return (0);
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 13:23:18 -04:00
|
|
|
static bool ft_check_infile(t_data *data, t_cmd *cmd,
|
2023-03-29 13:07:57 -04:00
|
|
|
char *redirection_identifier, char *redirection)
|
|
|
|
{
|
2023-04-04 08:51:34 -04:00
|
|
|
int fd;
|
|
|
|
|
2023-03-29 13:07:57 -04:00
|
|
|
if (ft_strcmp(redirection_identifier, "<") == 0)
|
|
|
|
{
|
2023-04-05 08:13:56 -04:00
|
|
|
if (cmd->fd_in[0] == -2)
|
|
|
|
return (0);
|
2023-03-29 13:07:57 -04:00
|
|
|
if (ft_file_is_readable(data, redirection))
|
2023-04-04 08:51:34 -04:00
|
|
|
{
|
|
|
|
fd = open(redirection, O_RDONLY);
|
|
|
|
if (cmd->fd_in[0] > 2)
|
|
|
|
close(cmd->fd_in[0]);
|
|
|
|
cmd->fd_in[0] = fd;
|
|
|
|
}
|
2023-03-29 13:07:57 -04:00
|
|
|
else
|
2023-03-29 13:23:18 -04:00
|
|
|
{
|
2023-04-04 08:51:34 -04:00
|
|
|
if (cmd->fd_in[0] > 2)
|
|
|
|
close(cmd->fd_in[0]);
|
2023-03-29 13:07:57 -04:00
|
|
|
cmd->fd_in[0] = -2;
|
2023-04-05 08:13:56 -04:00
|
|
|
return (0);
|
2023-03-29 13:23:18 -04:00
|
|
|
}
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|
2023-03-29 13:23:18 -04:00
|
|
|
return (0);
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 13:23:18 -04:00
|
|
|
static bool ft_check_outfile(t_data *data, t_cmd *cmd,
|
2023-03-29 13:07:57 -04:00
|
|
|
char *redirection_identifier, char *redirection)
|
|
|
|
{
|
2023-04-04 08:51:34 -04:00
|
|
|
int fd;
|
|
|
|
|
2023-03-29 13:07:57 -04:00
|
|
|
if (ft_strcmp(redirection_identifier, ">") == 0)
|
|
|
|
{
|
2023-04-05 08:13:56 -04:00
|
|
|
if (cmd->fd_out[0] == -2)
|
|
|
|
return (0);
|
2023-03-29 13:07:57 -04:00
|
|
|
if (ft_file_is_writable(data, redirection))
|
2023-04-04 08:51:34 -04:00
|
|
|
{
|
|
|
|
fd = open(redirection,
|
2023-03-29 13:07:57 -04:00
|
|
|
O_WRONLY | O_TRUNC | O_CREAT, 0644);
|
2023-04-04 08:51:34 -04:00
|
|
|
if (cmd->fd_out[0] > 2)
|
|
|
|
close(cmd->fd_out[0]);
|
|
|
|
cmd->fd_out[0] = fd;
|
|
|
|
}
|
2023-03-29 13:07:57 -04:00
|
|
|
else
|
2023-03-29 13:23:18 -04:00
|
|
|
{
|
2023-04-04 08:51:34 -04:00
|
|
|
if (cmd->fd_out[0] > 2)
|
|
|
|
close(cmd->fd_out[0]);
|
2023-03-29 13:07:57 -04:00
|
|
|
cmd->fd_out[0] = -2;
|
2023-04-05 08:13:56 -04:00
|
|
|
return (0);
|
2023-03-29 13:23:18 -04:00
|
|
|
}
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|
2023-03-29 13:23:18 -04:00
|
|
|
return (0);
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 13:23:18 -04:00
|
|
|
static bool ft_check_outfile_append(t_data *data, t_cmd *cmd,
|
2023-03-29 13:07:57 -04:00
|
|
|
char *redirection_identifier, char *redirection)
|
|
|
|
{
|
2023-04-04 08:51:34 -04:00
|
|
|
int fd;
|
|
|
|
|
2023-03-29 13:07:57 -04:00
|
|
|
if (ft_strcmp(redirection_identifier, ">>") == 0)
|
|
|
|
{
|
2023-04-05 08:13:56 -04:00
|
|
|
if (cmd->fd_out[0] == -2)
|
|
|
|
return (0);
|
2023-04-05 11:32:04 -04:00
|
|
|
if (ft_file_is_appendable(data, redirection))
|
2023-04-04 08:51:34 -04:00
|
|
|
{
|
|
|
|
fd = open(redirection,
|
2023-03-29 13:07:57 -04:00
|
|
|
O_WRONLY | O_APPEND | O_CREAT, 0644);
|
2023-04-04 08:51:34 -04:00
|
|
|
if (cmd->fd_out[0] > 2)
|
|
|
|
close(cmd->fd_out[0]);
|
|
|
|
cmd->fd_out[0] = fd;
|
|
|
|
}
|
2023-03-29 13:07:57 -04:00
|
|
|
else
|
2023-03-29 13:23:18 -04:00
|
|
|
{
|
2023-04-04 08:51:34 -04:00
|
|
|
if (cmd->fd_out[0] > 2)
|
|
|
|
close(cmd->fd_out[0]);
|
2023-03-29 13:07:57 -04:00
|
|
|
cmd->fd_out[0] = -2;
|
2023-03-29 13:23:18 -04:00
|
|
|
}
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|
2023-03-29 13:23:18 -04:00
|
|
|
return (0);
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 13:23:18 -04:00
|
|
|
bool ft_check_redirection(t_data *data, t_cmd *cmd,
|
2023-03-29 13:07:57 -04:00
|
|
|
char *redirection_identifier, char *redirection)
|
|
|
|
{
|
2023-04-04 07:56:21 -04:00
|
|
|
char *str;
|
2023-04-07 11:12:48 -04:00
|
|
|
bool out;
|
2023-04-04 07:56:21 -04:00
|
|
|
|
2023-04-07 11:12:48 -04:00
|
|
|
if (ft_is_in("<>", redirection_identifier[0])
|
|
|
|
&& ft_is_in("<>", redirection[0]))
|
2023-04-04 07:56:21 -04:00
|
|
|
{
|
2023-04-14 10:17:35 -04:00
|
|
|
ft_eprintf("bozoshell: %s: invalid redirection file\n", redirection);
|
2023-04-04 07:56:21 -04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
str = ft_strdup(redirection);
|
|
|
|
if (str == NULL)
|
|
|
|
{
|
2023-04-14 10:17:35 -04:00
|
|
|
ft_eprintf("bozoshell: malloc failed\n");
|
2023-04-04 07:56:21 -04:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
ft_quote_remover(str);
|
2023-04-07 11:12:48 -04:00
|
|
|
out = 0;
|
2023-04-04 08:51:34 -04:00
|
|
|
if (ft_check_heredoc(data, cmd, redirection_identifier, str)
|
2023-04-07 11:12:48 -04:00
|
|
|
|| ft_check_infile(data, cmd, redirection_identifier, str)
|
|
|
|
|| ft_check_outfile(data, cmd, redirection_identifier, str)
|
|
|
|
|| ft_check_outfile_append(data, cmd, redirection_identifier, str))
|
|
|
|
out = 1;
|
2023-04-04 07:56:21 -04:00
|
|
|
free(str);
|
2023-04-07 11:12:48 -04:00
|
|
|
return (out);
|
2023-03-29 13:07:57 -04:00
|
|
|
}
|