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) if (fd == -1)
{ {
ft_eprintf("minishell: %s: No such file or directory\n", path); ft_eprintf("minishell: %s: No such file or directory\n", path);
return (-1); return (0);
} }
readable = read(fd, "", 0); readable = read(fd, "", 0);
if (readable == -1) if (readable == -1)
{ {
ft_eprintf("minishell: %s: Permission denied\n", path); 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) int ft_file_is_writable(const char *path)

View File

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

65
main.c
View File

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

View File

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