diff --git a/.nfs00000000098c82b700000161 b/.nfs00000000098c82b700000161 new file mode 100755 index 0000000..eaadc7c Binary files /dev/null and b/.nfs00000000098c82b700000161 differ diff --git a/.nfs00000000098c82e400000158 b/.nfs00000000098c82e400000158 new file mode 100755 index 0000000..e6391b1 Binary files /dev/null and b/.nfs00000000098c82e400000158 differ diff --git a/.nfs00000000098c82ed00000153 b/.nfs00000000098c82ed00000153 new file mode 100755 index 0000000..1699e31 Binary files /dev/null and b/.nfs00000000098c82ed00000153 differ diff --git a/.nfs00000000098c82f10000015a b/.nfs00000000098c82f10000015a new file mode 100755 index 0000000..f3ca8d0 Binary files /dev/null and b/.nfs00000000098c82f10000015a differ diff --git a/file.c b/file.c index d2b5d31..9b3e201 100644 --- a/file.c +++ b/file.c @@ -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) diff --git a/infile.c b/infile.c index e229209..98af4d2 100644 --- a/infile.c +++ b/infile.c @@ -13,11 +13,13 @@ #include "libftx/libftx.h" #include "minishell.h" #include "utils/utils.h" +#include -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]); - ft_freer_tab_ultimate(1, tab); - return (0); + 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); + } + 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); 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); diff --git a/main.c b/main.c index 82084fb..a001016 100644 --- a/main.c +++ b/main.c @@ -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,27 +51,57 @@ 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; + 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) + close(infile); + ft_lstclear(cmds, ft_cmddel); + free(cmds); + free(line_clean); + return (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 (outfile > 2) - close(outfile); - if (infile > 2) - close(infile); + 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 (-1 * return_value); + 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(); diff --git a/outfile.c b/outfile.c index de4f3ca..1f8a510 100644 --- a/outfile.c +++ b/outfile.c @@ -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);