This commit is contained in:
Camille Chauvet
2023-03-13 14:23:27 +01:00
parent c43331dca3
commit 4d3c25547e
12 changed files with 174 additions and 317 deletions

View File

@ -12,7 +12,7 @@
#include "./redirection_private.h"
int ft_file_is_readable(const char *path)
int ft_file_is_readable(t_data *data, const char *path)
{
int readable;
int fd;
@ -20,12 +20,14 @@ int ft_file_is_readable(const char *path)
fd = open(path, O_RDONLY);
if (fd == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: No such file or directory\n", path);
return (0);
}
readable = read(fd, "", 0);
if (readable == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
@ -33,27 +35,46 @@ int ft_file_is_readable(const char *path)
return (1);
}
int ft_file_is_writable(const char *path)
int ft_file_is_writable(t_data *data, const char *path)
{
int writeable;
int fd;
fd = open(path, O_WRONLY | O_CREAT, 0644);
fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (fd == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
writeable = write(fd, "", 0);
if (writeable == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
return (fd);
}
int ft_file_is_executable(const char *path)
int ft_file_is_appendable(t_data *data, const char *path)
{
return (access(path, X_OK) == 0);
int writeable;
int fd;
fd = open(path, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (fd == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
writeable = write(fd, "", 0);
if (writeable == -1)
{
data->exit_code = 1;
ft_eprintf("minishell: %s: Permission denied\n", path);
return (0);
}
return (fd);
}

View File

@ -1,138 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* infile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
static int ft_infile_is_valid(t_data *data, const char *line)
{
char **tab;
char *path;
ssize_t i;
int return_code;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (0);
}
if (tab[0] == NULL)
return (1);
return_code = 1;
i = -1;
while (tab[++i] != NULL && return_code == 1)
{
if (tab[i][0] == '<')
{
if (ft_strcmp(tab[i], "<") == 0)
{
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return_code = 0;
ft_quote_remover(path);
free(tab[i + 1]);
tab[i + 1] = path;
if (ft_file_is_readable(tab[i + 1]) == 0)
{
data->exit_code = 1;
return_code = -1;
}
}
if (ft_contain_only_str(tab[i + 1], "| <>"))
return_code = 0;
}
}
if (return_code == 0)
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
ft_freer_tab_ultimate(1, tab);
return (return_code);
}
static int ft_get_infile(t_data *data, const char *line)
{
size_t i;
int fd;
char **tab;
char *path;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (-2);
}
fd = -1;
i = 0;
while (tab[i + 1] != NULL)
{
if (tab[i][0] == '<')
if (fd != 0)
close(fd);
if (ft_strcmp("<", tab[i]) == 0)
{
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return (-2);
ft_quote_remover(path);
fd = open(path, O_RDONLY);
free(path);
}
else if (ft_strcmp("<<", tab[i]) == 0)
fd = ft_heredoc(data, ft_quote_remover(tab[i + 1]));
i++;
}
ft_freer_tab_ultimate(1, tab);
return (fd);
}
static int ft_remove_infile(char *line)
{
size_t i;
size_t y;
char **tab;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
i = 0;
y = 0;
while (tab[i] != NULL)
{
if (tab[i][0] == '<')
{
ft_strshift(line + y,
(-1) * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1 + (line[ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1] != '\0')));
i++;
}
else
y = y + ft_strlen(tab[i]) + (y != 0);
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
}
int ft_infile(t_data *data, char *line)
{
int fd;
if (ft_infile_is_valid(data, line) == 0)
return (-2);
fd = ft_get_infile(data, line);
if (fd == -2)
return (-2);
ft_remove_infile(line);
return (fd);
}

View File

@ -1,135 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* infile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/20 13:05:43 by starnakin ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
static int ft_outfile_is_valid(t_data *data, const char *line)
{
char **tab;
char *path;
ssize_t i;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (0);
}
if (tab[0] == NULL)
return (1);
i = -1;
while (tab[++i] != NULL)
{
if (tab[i][0] == '>')
{
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return (0);
ft_quote_remover(path);
if (ft_file_is_writable(path) == 0)
{
data->exit_code = 1;
free(path);
ft_freer_tab_ultimate(1, tab);
return (0);
}
if (ft_contain_only_str(path, "| >>"))
{
free(path);
ft_eprintf("minishell: %s: must be followed by an outfile\n", path);
ft_freer_tab_ultimate(1, tab);
return (0);
}
free(path);
}
}
ft_freer_tab_ultimate(1, tab);
return (1);
}
static int ft_get_outfile(t_data *data, const char *line)
{
size_t i;
int fd;
char **tab;
char *path;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (-2);
}
fd = -1;
i = 0;
while (tab[i + 1] != NULL)
{
if (tab[i][0] == '>')
if (fd != 0)
close(fd);
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return (-2);
ft_quote_remover(path);
if (ft_strcmp(">", tab[i]) == 0)
fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644);
else if (ft_strcmp(">>", tab[i]) == 0)
fd = open(path, O_CREAT | O_APPEND, 0644);
free(path);
i++;
}
ft_freer_tab_ultimate(1, tab);
return (fd);
}
static int ft_remove_outfile(char *line)
{
size_t i;
size_t y;
char **tab;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
i = 0;
y = 0;
while (tab[i] != NULL)
{
if (tab[i][0] == '>')
{
ft_strshift(line + y,
(-1) * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1 + (line[ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1] != '\0')));
i++;
}
else
y = y + ft_strlen(tab[i]) + (y != 0);
i++;
}
ft_freer_tab_ultimate(1, tab);
return (0);
}
int ft_outfile(t_data *data, char *line)
{
int fd;
if (ft_outfile_is_valid(data, line) == 0)
return (-2);
fd = ft_get_outfile(data, line);
if (fd == -2)
return (-2);
ft_remove_outfile(line);
return (fd);
}

114
redirection/redirection.c Normal file
View File

@ -0,0 +1,114 @@
#include "redirection_private.h"
int ft_replace_file(t_data *data, char **tab)
{
size_t i;
char *redirection;
i = 0;
while (tab[i] != NULL)
{
if (ft_is_in("<>", tab[i][0]))
{
i++;
redirection = ft_env_filler(data, tab[i]);
if (redirection == NULL)
return (1);
free(tab[i]);
tab[i] = redirection;
ft_quote_remover(tab[i]);
}
i++;
}
return (0);
}
void ft_remove_redirection(char *cmd_str)
{
size_t i;
ssize_t start;
ssize_t stop;
i = 0;
while (cmd_str[i] != '\0')
{
start = -1;
while ((ft_is_in_quote(cmd_str, i) || !ft_is_in("<>", cmd_str[i]))
&& cmd_str[i] != '\0')
i++;
if (ft_is_in("<>", cmd_str[i]))
start = i;
while (cmd_str[i] == cmd_str[start])
i++;
i++;
while (cmd_str[i] != '\0' && (cmd_str[i] != ' ' || ft_is_in_quote(cmd_str, i)))
i++;
stop = i - start;
if (start != -1)
ft_strshift(cmd_str + start, -1 * stop);
}
}
int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
{
size_t i;
i = 0;
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);
}
i++;
}
return (0);
}
int ft_redirection(t_data *data, t_cmd *cmd, char *cmd_str)
{
char **tab;
cmd->fd_in[0] = -1;
cmd->fd_in[1] = -1;
cmd->fd_out[0] = -1;
cmd->fd_out[1] = -1;
tab = ft_split_quoted(cmd_str, ' ');
if (tab == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
ft_remove_redirection(cmd_str);
if (ft_set_redirection(data, cmd, tab))
{
ft_freer_tab_ultimate(1, tab);
return (1);
}
ft_freer_tab_ultimate(1, tab);
return (0);
}

View File

@ -1,8 +1,9 @@
#ifndef REDIRECTION_H
# define REDIRECTION_H
# include "../data/data.h"
# include "../cmd/cmd.h"
int ft_infile(t_data *data, char *line);
int ft_outfile(t_data *data, char *line);
int ft_redirection(t_data *data, t_cmd *cmd, char *cmd_str);
int *ft_get_heredoc();
#endif

View File

@ -8,9 +8,11 @@
# include "../data/data.h"
# include "../env/env.h"
# include "../utils/utils.h"
# include "../cmd/cmd.h"
int ft_file_is_readable(const char *path);
int ft_file_is_writable(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_appendable(t_data *data, const char *path);
int ft_heredoc(t_data *data, char *stop);
#endif