clean: norm part2 (FINAL)
This commit is contained in:
67
redirection/check.c
Normal file
67
redirection/check.c
Normal file
@ -0,0 +1,67 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/03/29 18:43:20 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./redirection_private.h"
|
||||
|
||||
static void ft_check_heredoc(t_data *data, t_cmd *cmd,
|
||||
char *redirection_identifier, char *redirection)
|
||||
{
|
||||
if (ft_strcmp(redirection_identifier, "<<") == 0)
|
||||
cmd->fd_in[0] = ft_heredoc(data, redirection);
|
||||
}
|
||||
|
||||
static void ft_check_infile(t_data *data, t_cmd *cmd,
|
||||
char *redirection_identifier, char *redirection)
|
||||
{
|
||||
if (ft_strcmp(redirection_identifier, "<") == 0)
|
||||
{
|
||||
if (ft_file_is_readable(data, redirection))
|
||||
cmd->fd_in[0] = open(redirection, O_RDONLY);
|
||||
else
|
||||
cmd->fd_in[0] = -2;
|
||||
}
|
||||
}
|
||||
|
||||
static void ft_check_outfile(t_data *data, t_cmd *cmd,
|
||||
char *redirection_identifier, char *redirection)
|
||||
{
|
||||
if (ft_strcmp(redirection_identifier, ">") == 0)
|
||||
{
|
||||
if (ft_file_is_writable(data, redirection))
|
||||
cmd->fd_out[0] = open(redirection,
|
||||
O_WRONLY | O_TRUNC | O_CREAT, 0644);
|
||||
else
|
||||
cmd->fd_out[0] = -2;
|
||||
}
|
||||
}
|
||||
|
||||
static void ft_check_outfile_append(t_data *data, t_cmd *cmd,
|
||||
char *redirection_identifier, char *redirection)
|
||||
{
|
||||
if (ft_strcmp(redirection_identifier, ">>") == 0)
|
||||
{
|
||||
if (ft_file_is_writable(data, redirection))
|
||||
cmd->fd_out[0] = open(redirection,
|
||||
O_WRONLY | O_APPEND | O_CREAT, 0644);
|
||||
else
|
||||
cmd->fd_out[0] = -2;
|
||||
}
|
||||
}
|
||||
|
||||
void 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);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/03/28 15:47:25 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/03/29 16:37:05 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -19,37 +19,61 @@ int *ft_get_heredoc(void)
|
||||
return (&heredoc);
|
||||
}
|
||||
|
||||
static int ft_format_and_write(t_data *data, const char *str, int fd)
|
||||
{
|
||||
char *line_clean;
|
||||
|
||||
line_clean = ft_env_filler(data, str);
|
||||
if (line_clean == NULL)
|
||||
return (1);
|
||||
ft_putendl_fd(line_clean, fd);
|
||||
free(line_clean);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_heredoc2(t_data *data, char *stop, char *line, int fds[2])
|
||||
{
|
||||
line[ft_strlen(line) - 1] = '\0';
|
||||
if (ft_strcmp(line, stop) == 0)
|
||||
{
|
||||
free(line);
|
||||
return (2);
|
||||
}
|
||||
if (ft_format_and_write(data, line, fds[1]))
|
||||
{
|
||||
ft_closer(fds);
|
||||
free(line);
|
||||
return (1);
|
||||
}
|
||||
free(line);
|
||||
ft_printf("> ");
|
||||
line = get_next_line(*ft_get_heredoc());
|
||||
if (line == NULL && *ft_get_heredoc() == -1)
|
||||
{
|
||||
ft_closer(fds);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_heredoc(t_data *data, char *stop)
|
||||
{
|
||||
char *line;
|
||||
char *line_clean;
|
||||
int fds[2];
|
||||
int return_code;
|
||||
|
||||
if (pipe(fds) == -1)
|
||||
return (-2);
|
||||
return (-1);
|
||||
*ft_get_heredoc() = dup(0);
|
||||
ft_printf("> ");
|
||||
line = get_next_line(*ft_get_heredoc());
|
||||
while (line != NULL)
|
||||
{
|
||||
line[ft_strlen(line) - 1] = '\0';
|
||||
if (ft_strcmp(line, stop) == 0)
|
||||
{
|
||||
free(line);
|
||||
return_code = ft_heredoc2(data, stop, line, fds);
|
||||
if (return_code == 2)
|
||||
break ;
|
||||
}
|
||||
line_clean = ft_env_filler(data, line);
|
||||
free(line);
|
||||
ft_putendl_fd(line_clean, fds[1]);
|
||||
free(line_clean);
|
||||
ft_printf("> ");
|
||||
line = get_next_line(*ft_get_heredoc());
|
||||
if (line == NULL && *ft_get_heredoc() == -1)
|
||||
{
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
else if (return_code)
|
||||
return (-2);
|
||||
}
|
||||
}
|
||||
close(fds[1]);
|
||||
*ft_get_heredoc() = -1;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/03/28 15:49:58 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/03/29 17:58:57 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -35,11 +35,21 @@ int ft_replace_file(t_data *data, char **tab)
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_remove_redirection(char *cmd_str)
|
||||
static void ft_skip(char *str, size_t *i, ssize_t *start)
|
||||
{
|
||||
while (str[*i] == str[*start])
|
||||
(*i)++;
|
||||
while (str[*i] == ' ')
|
||||
(*i)++;
|
||||
while (str[*i] != '\0' && (str[*i] != ' '
|
||||
|| ft_is_in_quote(str, *i)))
|
||||
(*i)++;
|
||||
}
|
||||
|
||||
static void ft_remove_redirection(char *cmd_str)
|
||||
{
|
||||
size_t i;
|
||||
ssize_t start;
|
||||
ssize_t stop;
|
||||
|
||||
i = 0;
|
||||
while (cmd_str[i] != '\0')
|
||||
@ -50,19 +60,12 @@ void ft_remove_redirection(char *cmd_str)
|
||||
i++;
|
||||
if (ft_is_in("<>", cmd_str[i]))
|
||||
start = i;
|
||||
if (start == -1)
|
||||
else
|
||||
continue ;
|
||||
while (cmd_str[i] == cmd_str[start])
|
||||
i++;
|
||||
while (cmd_str[i] == ' ')
|
||||
i++;
|
||||
while (cmd_str[i] != '\0' && (cmd_str[i] != ' '
|
||||
|| ft_is_in_quote(cmd_str, i)))
|
||||
i++;
|
||||
stop = i - start;
|
||||
ft_skip(cmd_str, &i, &start);
|
||||
if (start != -1)
|
||||
{
|
||||
ft_strshift(cmd_str + start, -1 * stop);
|
||||
ft_strshift(cmd_str + start, -1 * (i - start));
|
||||
i = start;
|
||||
}
|
||||
}
|
||||
@ -76,35 +79,8 @@ int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
|
||||
while (tab[i + 1] != NULL)
|
||||
{
|
||||
ft_quote_remover(tab[i + 1]);
|
||||
if (ft_strcmp(tab[i], "<<") == 0)
|
||||
{
|
||||
cmd->fd_in[0] = ft_heredoc(data, tab[i + 1]);
|
||||
if (cmd->fd_in[0] == -2)
|
||||
return (1);
|
||||
}
|
||||
else if (ft_strcmp(tab[i], "<") == 0)
|
||||
{
|
||||
if (ft_file_is_readable(data, tab[i + 1]))
|
||||
cmd->fd_in[0] = open(tab[i + 1], O_RDONLY);
|
||||
else
|
||||
return (1);
|
||||
}
|
||||
else if (ft_strcmp(tab[i], ">") == 0)
|
||||
{
|
||||
if (ft_file_is_writable(data, tab[i + 1]))
|
||||
cmd->fd_out[0] = open(tab[i + 1],
|
||||
O_WRONLY | O_TRUNC | O_CREAT, 0644);
|
||||
else
|
||||
return (1);
|
||||
}
|
||||
else if (ft_strcmp(tab[i], ">>") == 0)
|
||||
{
|
||||
if (ft_file_is_appendable(data, tab[i + 1]))
|
||||
cmd->fd_out[0] = open(tab[i + 1],
|
||||
O_WRONLY | O_APPEND | O_CREAT, 0644);
|
||||
else
|
||||
return (1);
|
||||
}
|
||||
if (ft_check_redirection(data, cmd, tab[i], tab[i + 1]) == 2)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/27 13:44:31 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/03/28 15:44:38 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/03/29 18:21:45 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,6 +24,8 @@
|
||||
|
||||
int ft_file_is_readable(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,
|
||||
char *redirection_identifier, char *redirection);
|
||||
int ft_file_is_appendable(t_data *data, const char *path);
|
||||
int ft_heredoc(t_data *data, char *stop);
|
||||
|
||||
|
Reference in New Issue
Block a user