fix: infile has now quote removed and env var replacement

This commit is contained in:
Camille Chauvet 2023-03-06 16:06:12 +01:00
parent 5bfc613126
commit 3540743135
8 changed files with 95 additions and 39 deletions

BIN
.nfs00000000098c82b700000161 Executable file

Binary file not shown.

BIN
.nfs00000000098c82e400000158 Executable file

Binary file not shown.

BIN
.nfs00000000098c82ed00000153 Executable file

Binary file not shown.

BIN
.nfs00000000098c82f10000015a Executable file

Binary file not shown.

7
file.c
View File

@ -22,15 +22,16 @@ int ft_file_is_readable(const char *path)
if (fd == -1)
{
ft_eprintf("minishell: %s: No such file or directory\n", path);
return (-1);
return (0);
}
readable = read(fd, "", 0);
if (readable == -1)
{
ft_eprintf("minishell: %s: Permission denied\n", path);
return (-1);
return (0);
}
return (fd);
close(fd);
return (1);
}
int ft_file_is_writable(const char *path)

View File

@ -13,11 +13,13 @@
#include "libftx/libftx.h"
#include "minishell.h"
#include "utils/utils.h"
#include <fcntl.h>
static int ft_infile_is_valid(const char *line)
static int ft_infile_is_valid(t_data *data, const char *line)
{
char **tab;
size_t i;
char *path;
ssize_t i;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
@ -27,28 +29,41 @@ static int ft_infile_is_valid(const char *line)
}
if (tab[0] == NULL)
return (1);
i = 0;
while (tab[i] != NULL)
i = -1;
while (tab[++i] != NULL)
{
if (tab[i][0] == '<')
{
if (tab[i + 1] != NULL && !ft_contain_only_str(tab[i + 1], "| <>"))
continue ;
ft_eprintf("minishell: %s: must be followed by an infile\n", tab[i]);
path = ft_env_filler(data, tab[i + 1]);
if (path == NULL)
return (0);
ft_quote_remover(path);
if (ft_file_is_readable(path) == 0)
{
free(path);
ft_freer_tab_ultimate(1, tab);
return (0);
}
i++;
if (ft_contain_only_str(path, "| <>"))
{
free(path);
ft_eprintf("minishell: %s: must be followed by an infile\n", path);
ft_freer_tab_ultimate(1, tab);
return (0);
}
free(path);
}
}
ft_freer_tab_ultimate(1, tab);
return (1);
}
static int ft_get_infile(const char *line)
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)
@ -64,9 +79,16 @@ static int ft_get_infile(const char *line)
if (fd != 0)
close(fd);
if (ft_strcmp("<", tab[i]) == 0)
fd = ft_file_is_readable(ft_quote_remover(tab[i + 1]));
{
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(tab[i + 1]);
fd = ft_heredoc(ft_quote_remover(tab[i + 1]));
i++;
}
ft_freer_tab_ultimate(1, tab);
@ -90,8 +112,11 @@ static int ft_remove_infile(char *line)
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++;
@ -104,12 +129,12 @@ int ft_infile(t_data *data, char *line)
{
int fd;
if (ft_infile_is_valid(line) == 0)
if (ft_infile_is_valid(data, line) == 0)
{
data->exit_code = 2;
return (-2);
}
fd = ft_get_infile(line);
fd = ft_get_infile(data, line);
if (fd == -2)
return (-2);
ft_remove_infile(line);

59
main.c
View File

@ -39,11 +39,11 @@ static char *ft_get_user_input()
static int ft_minishell(t_data *data, char *line)
{
t_list **cmds;
char *line_clean;
int infile;
int outfile;
int return_value;
if (ft_syntatic_verif(data, line))
return (1);
@ -51,19 +51,21 @@ static int ft_minishell(t_data *data, char *line)
if (line_clean == NULL)
return (1);
outfile = ft_outfile(data, line_clean);
return_value = 0;
if (outfile == -2)
return_value = 1;
{
free(line_clean);
return (0);
}
infile = ft_infile(data, line_clean);
if (return_value == 0 && infile == -2)
return_value = 1;
if (return_value == 0&& ft_gen_exit_code_var(data))
return_value = 1;
cmds = ft_parse_cmds(data, line_clean, infile, outfile);
if (return_value == 0 && cmds == NULL)
return_value = 1;
if (return_value == 0 && ft_cmds_executor(data, cmds))
return_value = 1;
if (infile == -2)
{
if (outfile > 2)
close(outfile);
free(line_clean);
return (0);
}
if (ft_gen_exit_code_var(data))
{
if (outfile > 2)
close(outfile);
if (infile > 2)
@ -71,7 +73,35 @@ static int ft_minishell(t_data *data, char *line)
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean);
return (-1 * return_value);
return (1);
}
cmds = ft_parse_cmds(data, line_clean, infile, outfile);
if (cmds == NULL)
{
if (outfile > 2)
close(outfile);
if (infile > 2)
close(infile);
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean);
return (1);
}
if (ft_cmds_executor(data, cmds))
{
if (outfile > 2)
close(outfile);
if (infile > 2)
close(infile);
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean);
return (1);
}
ft_lstclear(cmds, ft_cmddel);
free(cmds);
free(line_clean);
return (0);
}
void ft_ctrlc(int num)
@ -111,13 +141,14 @@ int main(int ac, char **av, char **env)
signal(SIGINT, ft_ctrlc);
signal(SIGQUIT, ft_quit);
data.exit_code = 0;
ft_gen_exit_code_var(&data);
data.env = init_env(env);
if (data.env == NULL)
return (1);
line = ft_get_user_input();
while (line != NULL)
{
if (ft_minishell(&data, line) == -1)
if (ft_minishell(&data, line) == 1)
break ;
free(line);
line = ft_get_user_input();

View File

@ -15,7 +15,7 @@
static int ft_outfile_is_valid(const char *line)
{
char **tab;
size_t i;
ssize_t i;
tab = ft_split_quoted(line, ' ');
if (tab == NULL)
@ -25,8 +25,8 @@ static int ft_outfile_is_valid(const char *line)
}
if (tab[0] == NULL)
return (1);
i = 0;
while (tab[i] != NULL)
i = -1;
while (tab[++i] != NULL)
{
if (tab[i][0] == '>')
{
@ -36,7 +36,6 @@ static int ft_outfile_is_valid(const char *line)
ft_freer_tab_ultimate(1, tab);
return (0);
}
i++;
}
ft_freer_tab_ultimate(1, tab);
return (1);