42_minishell/redirection/check.c
Etienne Rey-bethbeder 0b56e95868 _
2023-04-14 16:17:35 +02:00

148 lines
3.6 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
/* Updated: 2023/04/07 15:06:39 by alouis-j ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
#include <stdbool.h>
static bool ft_check_heredoc(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
int fd;
if (ft_strcmp(redirection_identifier, "<<") == 0)
{
if (cmd->fd_in[0] == -2)
return (0);
fd = ft_heredoc(data, redirection);
if (fd == -2)
return (1);
else
{
if (cmd->fd_in[0] > 2)
close(cmd->fd_in[0]);
cmd->fd_in[0] = fd;
}
}
return (0);
}
static bool ft_check_infile(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
int fd;
if (ft_strcmp(redirection_identifier, "<") == 0)
{
if (cmd->fd_in[0] == -2)
return (0);
if (ft_file_is_readable(data, redirection))
{
fd = open(redirection, O_RDONLY);
if (cmd->fd_in[0] > 2)
close(cmd->fd_in[0]);
cmd->fd_in[0] = fd;
}
else
{
if (cmd->fd_in[0] > 2)
close(cmd->fd_in[0]);
cmd->fd_in[0] = -2;
return (0);
}
}
return (0);
}
static bool ft_check_outfile(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
int fd;
if (ft_strcmp(redirection_identifier, ">") == 0)
{
if (cmd->fd_out[0] == -2)
return (0);
if (ft_file_is_writable(data, redirection))
{
fd = open(redirection,
O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (cmd->fd_out[0] > 2)
close(cmd->fd_out[0]);
cmd->fd_out[0] = fd;
}
else
{
if (cmd->fd_out[0] > 2)
close(cmd->fd_out[0]);
cmd->fd_out[0] = -2;
return (0);
}
}
return (0);
}
static bool ft_check_outfile_append(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
int fd;
if (ft_strcmp(redirection_identifier, ">>") == 0)
{
if (cmd->fd_out[0] == -2)
return (0);
if (ft_file_is_appendable(data, redirection))
{
fd = open(redirection,
O_WRONLY | O_APPEND | O_CREAT, 0644);
if (cmd->fd_out[0] > 2)
close(cmd->fd_out[0]);
cmd->fd_out[0] = fd;
}
else
{
if (cmd->fd_out[0] > 2)
close(cmd->fd_out[0]);
cmd->fd_out[0] = -2;
}
}
return (0);
}
bool ft_check_redirection(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
char *str;
bool out;
if (ft_is_in("<>", redirection_identifier[0])
&& ft_is_in("<>", redirection[0]))
{
ft_eprintf("bozoshell: %s: invalid redirection file\n", redirection);
return (1);
}
str = ft_strdup(redirection);
if (str == NULL)
{
ft_eprintf("bozoshell: malloc failed\n");
return (1);
}
ft_quote_remover(str);
out = 0;
if (ft_check_heredoc(data, cmd, redirection_identifier, str)
|| 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;
free(str);
return (out);
}