diff --git a/.nfs00000000098c816000000394 b/.nfs00000000098c816000000394 deleted file mode 100755 index 4655b91..0000000 Binary files a/.nfs00000000098c816000000394 and /dev/null differ diff --git a/Makefile b/Makefile index 5c15c8d..b42dc7f 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,8 @@ SRCS = ${BUILTINS_SRC} \ ./syntax/syntax.c \ ./format/format.c \ ./redirection/heredoc.c \ - ./redirection/infile.c \ - ./redirection/outfile.c \ ./redirection/file.c \ + ./redirection/redirection.c \ ./parse/parse.c OBJS = ${SRCS:.c=.o} diff --git a/execution/execution.c b/execution/execution.c index bc64e6c..b32171f 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -51,12 +51,12 @@ static bool ft_executor(t_cmd *cmd, char **env) return (1); if (cmd->pid == 0) { - dup2(cmd->fd_in[0], 0); - dup2(cmd->fd_out[0], 1); if (cmd->fd_in[1] != -1) dup2(cmd->fd_in[1], 0); if (cmd->fd_out[1] != -1) dup2(cmd->fd_out[1], 1); + dup2(cmd->fd_in[0], 0); + dup2(cmd->fd_out[0], 1); ft_closer(cmd->fd_in); ft_closer(cmd->fd_out); execve(cmd->executable, cmd->args, env); diff --git a/main.c b/main.c index c6ae897..977ce93 100644 --- a/main.c +++ b/main.c @@ -10,8 +10,6 @@ /* */ /* ************************************************************************** */ -#include "cmd/cmd.h" -#include "libftx/libft/list.h" #include "minishell.h" static char *ft_get_user_input() @@ -89,7 +87,8 @@ void ft_ctrlc(int num) void ft_quit(int num) { (void) num; - + rl_replace_line("", 0); + rl_redisplay(); ft_printf("%c%c %c%c", 0, 0, 0, 0); } @@ -122,7 +121,6 @@ int main(int ac, char **av, char **env) if (line == NULL) break ; } - free(data.exit_code_str); ft_lstclear(data.cmds, ft_cmddel); free(data.cmds); diff --git a/parse/parse.c b/parse/parse.c index c4442c1..cb324e6 100644 --- a/parse/parse.c +++ b/parse/parse.c @@ -1,26 +1,5 @@ #include "./parse_private.h" -static int ft_redirection_parse(t_data *data, char *cmd_str, t_cmd *cmd) -{ - int fd_in; - int fd_out; - - fd_in = ft_infile(data, cmd_str); - if (fd_in == -2) - return (1); - fd_out = ft_outfile(data, cmd_str); - if (fd_out == -2) - { - close(fd_in); - return (1); - } - cmd->fd_in[0] = fd_in; - cmd->fd_in[1] = -1; - cmd->fd_out[0] = fd_out; - cmd->fd_out[1] = -1; - return (0); -} - int ft_args_parse(t_data *data, char *cmd_str, t_cmd *cmd) { char **tab; @@ -39,6 +18,7 @@ int ft_args_parse(t_data *data, char *cmd_str, t_cmd *cmd) ft_freer_tab_ultimate(1, tab); return (1); } + ft_quote_remover(str); free(tab[i]); tab[i] = str; i++; @@ -54,7 +34,12 @@ int ft_executable_parse(t_data *data, t_cmd *cmd) path = cmd->args[0]; own = 0; - if (ft_strcmp(cmd->args[0], "env") == 0) + if (cmd->args[0] == NULL) + { + ft_closer(cmd->fd_in); + ft_closer(cmd->fd_out); + } + else if (ft_strcmp(cmd->args[0], "env") == 0) own = 1; else if (ft_strcmp(cmd->args[0], "export") == 0) own = 1; @@ -68,7 +53,7 @@ int ft_executable_parse(t_data *data, t_cmd *cmd) own = 1; else if (ft_strcmp(cmd->args[0], "cd") == 0) own = 1; - if (own == 0) + else path = ft_get_executable(data->env, cmd->args[0]); cmd->own_cmd = own; cmd->executable = path; @@ -86,8 +71,11 @@ int ft_cmd_parser(t_data *data, char *cmd_str) ft_eprintf("minishell: malloc failed\n"); return (1); } - if (ft_redirection_parse(data, cmd_str, cmd)) + if (ft_redirection(data, cmd, cmd_str)) + { + ft_cmddel(cmd); return (1); + } if (ft_args_parse(data, cmd_str, cmd)) { ft_cmddel(cmd); @@ -118,11 +106,17 @@ int ft_cmds_parser(t_data *data, const char *line) i = 0; while (tab[i] != NULL) { - ft_cmd_parser(data, tab[i]); + if (ft_cmd_parser(data, tab[i])) + { + ft_freer_tab_ultimate(1, tab); + return (1); + } i++; } - ft_add_fd(((t_cmd *) (*data->cmds)->content)->fd_in, 0); - ft_add_fd(((t_cmd *) ((ft_lstlast(*data->cmds))->content))->fd_out, 1); + if (((t_cmd *) (*data->cmds)->content)->fd_in[0] == -1) + ((t_cmd *) (*data->cmds)->content)->fd_in[0] = 0; + if (((t_cmd *) (ft_lstlast(*data->cmds))->content)->fd_out[0] == -1) + (((t_cmd *) (ft_lstlast(*data->cmds))->content)->fd_out[0] = 1); ft_freer_tab_ultimate(1, tab); return (0); } diff --git a/redirection/file.c b/redirection/file.c index 7789403..052216c 100644 --- a/redirection/file.c +++ b/redirection/file.c @@ -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); } diff --git a/redirection/infile.c b/redirection/infile.c deleted file mode 100644 index 773bd45..0000000 --- a/redirection/infile.c +++ /dev/null @@ -1,138 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* infile.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet 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); -} diff --git a/redirection/outfile.c b/redirection/outfile.c deleted file mode 100644 index adaf83f..0000000 --- a/redirection/outfile.c +++ /dev/null @@ -1,135 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* infile.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet ') - { - 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); -} diff --git a/redirection/redirection.c b/redirection/redirection.c new file mode 100644 index 0000000..c9fa7d1 --- /dev/null +++ b/redirection/redirection.c @@ -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); +} diff --git a/redirection/redirection.h b/redirection/redirection.h index baf4b34..cb5bb30 100644 --- a/redirection/redirection.h +++ b/redirection/redirection.h @@ -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 diff --git a/redirection/redirection_private.h b/redirection/redirection_private.h index e22e87f..e816796 100644 --- a/redirection/redirection_private.h +++ b/redirection/redirection_private.h @@ -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 diff --git a/t b/t new file mode 100755 index 0000000..9b47f86 --- /dev/null +++ b/t @@ -0,0 +1 @@ +bozo