From bdfff1360a48f5a58299a89a7d3ec3f85d243af0 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Fri, 3 May 2024 14:13:17 +0200 Subject: [PATCH] fixed some errors --- Makefile | 3 +- srcs/exec_bonus.c | 2 +- srcs/input_redirections.c | 66 ++++++++++++++++---------------------- srcs/output_redirections.c | 32 ++++++++++-------- srcs/path.c | 2 +- srcs/pipe.c | 6 +++- srcs/redirection_utils.c | 32 ++++++++++++++++++ todo | 7 ---- 8 files changed, 87 insertions(+), 63 deletions(-) create mode 100644 srcs/redirection_utils.c diff --git a/Makefile b/Makefile index 724957b..955b843 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/04/29 10:28:55 by marde-vr ### ########.fr # +# Updated: 2024/05/03 14:12:21 by marde-vr ### ########.fr # # # # **************************************************************************** # @@ -29,6 +29,7 @@ SRCS_RAW = main.c\ here_doc_utils.c\ export.c\ input_redirections.c\ + redirection_utils.c\ sort_wildcard_list.c\ output_redirections.c\ builtins.c\ diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 98513ad..b765cfd 100755 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/04/30 14:42:26 by marde-vr ### ########.fr */ +/* Updated: 2024/05/03 13:10:12 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index eec7d5b..2765e77 100755 --- a/srcs/input_redirections.c +++ b/srcs/input_redirections.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ -/* Updated: 2024/04/28 14:52:19 by tomoron ### ########.fr */ +/* Updated: 2024/05/03 14:11:52 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,39 +33,38 @@ void redirect_input(t_msh *msh, int i, char **cmd_args) } } -void ambiguous_redirect(char *str, t_msh *msh) +int open_input_redirection_file(t_msh *msh, t_cmd **cur_token) { - ft_printf_fd(2, "minishell: %s: ambiguous redirect\n", str); - msh->in_fd = -2; - g_return_code = 1; + t_token *filename; + + if (msh->in_fd != 0) + close(msh->in_fd); + if (ft_strchr((*cur_token)->value, '$')) + ambiguous_redirect((*cur_token)->value, msh); + filename = parse_tokens((*cur_token)->value, msh->env); + if (!filename) + ft_exit(msh, 1); + if (filename->next) + ambiguous_redirect((*cur_token)->value, msh); + if (!filename->next) + msh->in_fd = open(filename->value, O_RDONLY); + if (msh->in_fd == -1 && !filename->next) + { + ft_printf_fd(2, "minishell: %s: ", filename->value); + perror(""); + free_token(filename); + return (1); + } + free_token(filename); + return (msh->in_fd == -2); } int open_input_file(t_msh *msh, t_cmd **cur_token) { - t_token *filename; - if ((*cur_token)->cmd_type == HERE_DOC) handle_here_doc(msh, (*cur_token)->value); if ((*cur_token)->cmd_type == RED_I) - { - if (msh->in_fd != 0) - close(msh->in_fd); - filename = parse_tokens((*cur_token)->value, msh->env); - if (!filename) - ft_exit(msh, 1); - if (filename->next) - ambiguous_redirect((*cur_token)->value, msh); - if (!filename->next) - msh->in_fd = open(filename->value, O_RDONLY); - if (msh->in_fd == -1 && !filename->next) - { - ft_printf_fd(2, "minishell: %s: ", filename->value); - perror(""); - free_token(filename); - return (1); - } - free_token(filename); - } + return (open_input_redirection_file(msh, cur_token)); return (msh->in_fd == -2); } @@ -89,22 +88,11 @@ int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc) if (open_input_file(msh, &cur_token)) return (1); } - if (cur_token && cur_token->next && !is_operand_type(cur_token->next) + if (cur_token && cur_token->next + && !is_operand_type(cur_token) && !is_operand_type(cur_token->next) && cur_token->cmd_type != PIPE && cur_token->next->cmd_type != PIPE) return (get_in_type(msh, t_strt, cur_token->next, here_doc)); if (here_doc) return (get_in_type(msh, t_strt, t_strt, 0)); return (0); } - -int first_is_in_type(t_cmd *cmd) -{ - t_cmd *cur_token; - - cur_token = cmd; - while (cur_token && cur_token->cmd_type == CMD && cur_token->next) - cur_token = cur_token->next; - if (is_input_type(cur_token) || cur_token->cmd_type == PIPE) - return (1); - return (0); -} diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index 4bcc9ac..2a23038 100755 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */ -/* Updated: 2024/04/28 14:52:53 by tomoron ### ########.fr */ +/* Updated: 2024/05/03 14:07:43 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,9 +65,25 @@ void go_to_next_out_type(t_cmd **cur_cmd) *cur_cmd = (*cur_cmd)->next; } -int get_out_type(t_msh *msh, t_cmd *cur_cmd) +void get_out_file(t_msh *msh, t_cmd *cur_cmd, int *ret) { t_token *filename; + + msh->out_type = cur_cmd->cmd_type; + if (ft_strchr(cur_cmd->value, '$')) + ambiguous_redirect(cur_cmd->value, msh); + filename = parse_tokens(cur_cmd->value, msh->env); + if (!filename) + ft_exit(msh, 1); + if (filename->next) + ambiguous_redirect(cur_cmd->value, msh); + if (!filename->next) + *ret = open_out_file(msh, &cur_cmd, filename->value); + free_token(filename); +} + +int get_out_type(t_msh *msh, t_cmd *cur_cmd) +{ int ret; msh->out_type = CMD; @@ -78,17 +94,7 @@ int get_out_type(t_msh *msh, t_cmd *cur_cmd) msh->out_type = 0; else if (cur_cmd && is_output_type(cur_cmd) && !is_operand_type(cur_cmd) && cur_cmd->cmd_type != PIPE) - { - msh->out_type = cur_cmd->cmd_type; - filename = parse_tokens(cur_cmd->value, msh->env); - if (!filename) - ft_exit(msh, 1); - if (filename->next) - ambiguous_redirect(cur_cmd->value, msh); - if (!filename->next) - ret = open_out_file(msh, &cur_cmd, filename->value); - free_token(filename); - } + get_out_file(msh, cur_cmd, &ret); else if (cur_cmd && cur_cmd->cmd_type == PIPE) msh->out_type = PIPE; return (ret || msh->in_fd == -2); diff --git a/srcs/path.c b/srcs/path.c index 292282d..27e1885 100755 --- a/srcs/path.c +++ b/srcs/path.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */ -/* Updated: 2024/05/03 08:34:53 by marde-vr ### ########.fr */ +/* Updated: 2024/05/03 12:51:56 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/pipe.c b/srcs/pipe.c index 3194a1c..e40fdd0 100755 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/04/30 13:56:03 by marde-vr ### ########.fr */ +/* Updated: 2024/05/03 13:13:36 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -92,6 +92,10 @@ void child(t_msh *msh, char **cmd_args, int i) || msh->out_type == RED_O_APP) redirect_output(msh, i, cmd_args); close_pipe_fds(msh, i); + if (msh->in_fd > 2) + close(msh->in_fd); + if (msh->out_fd > 2) + close(msh->out_fd); execute_command(msh, cmd_args); close(0); close(1); diff --git a/srcs/redirection_utils.c b/srcs/redirection_utils.c new file mode 100644 index 0000000..a54ab1b --- /dev/null +++ b/srcs/redirection_utils.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirection_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marde-vr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/03 14:11:22 by marde-vr #+# #+# */ +/* Updated: 2024/05/03 14:11:50 by marde-vr ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void ambiguous_redirect(char *str, t_msh *msh) +{ + ft_printf_fd(2, "minishell: %s: ambiguous redirect\n", str); + msh->in_fd = -2; + g_return_code = 1; +} + +int first_is_in_type(t_cmd *cmd) +{ + t_cmd *cur_token; + + cur_token = cmd; + while (cur_token && cur_token->cmd_type == CMD && cur_token->next) + cur_token = cur_token->next; + if (is_input_type(cur_token) || cur_token->cmd_type == PIPE) + return (1); + return (0); +} diff --git a/todo b/todo index 7cb5621..25995ea 100644 --- a/todo +++ b/todo @@ -1,14 +1,7 @@ -- Leak lors des redirection out - export c= - export a="export b="export"" -- ./aa = permission denied != Is a directory - env -i ./minishell => unset ALL => export a (nes pas export) -- export a=b && cat < A -> A -- lolcat && cat < n affiche pas le heredoc - export a="Makefile out" -- marde-vr@2A7:/tmp/bberkrou$ cat < $a