From ded1b802b222b63e0ddefb440db5842312ac6345 Mon Sep 17 00:00:00 2001 From: tomoron Date: Wed, 22 May 2024 13:53:49 +0200 Subject: [PATCH] kill me --- Makefile | 3 ++- srcs/here_doc.c | 10 ++++++---- srcs/minishell.h | 3 ++- srcs/parsing.c | 9 +++++++-- srcs/parsing_utils.c | 24 ++++++++++++++++++++++++ srcs/parsing_var.c | 4 ++-- 6 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 srcs/parsing_utils.c diff --git a/Makefile b/Makefile index 0cf1dfc..3f6630d 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/05/06 11:00:33 by tomoron ### ########.fr # +# Updated: 2024/05/22 13:50:22 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -46,6 +46,7 @@ SRCS_RAW = main.c\ get_len_bonus.c\ check_syntax.c\ check_syntax_utils.c\ + parsing_utils.c\ unset.c\ free.c diff --git a/srcs/here_doc.c b/srcs/here_doc.c index 9e51b6f..b24530c 100755 --- a/srcs/here_doc.c +++ b/srcs/here_doc.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/24 17:44:32 by marde-vr #+# #+# */ -/* Updated: 2024/05/06 15:09:44 by tomoron ### ########.fr */ +/* Updated: 2024/05/22 13:48:39 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ void get_here_doc_input(t_msh *msh, char *eof) ft_printf_fd(2, " end-of-file, (wanted `%s')\n", eof); break ; } - if (line && !ft_strncmp(line, eof, ft_strlen(eof))) + if (line && !ft_strcmp(line, eof)) break ; parse_var(msh, line); write(msh->in_fd, "\n", 1); @@ -58,7 +58,8 @@ void here_doc_signal(t_msh *msh, int child_pid, char *here_doc_file) set_echoctl(msh->echoctl); signal(SIGINT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive); - close(msh->in_fd); + if (msh->in_fd > 2) + close(msh->in_fd); if (WIFEXITED(status) && WEXITSTATUS(status)) { unlink(here_doc_file); @@ -79,7 +80,8 @@ void handle_here_doc(t_msh *msh, char *eof) here_doc_file = get_tmp_file_name(msh); if (msh->here_doc_filename) { - close(msh->in_fd); + if (msh->in_fd) + close(msh->in_fd); unlink(msh->here_doc_filename); free(msh->here_doc_filename); } diff --git a/srcs/minishell.h b/srcs/minishell.h index 743433a..33216b6 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/05/16 13:21:32 by tomoron ### ########.fr */ +/* Updated: 2024/05/22 13:49:58 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -165,6 +165,7 @@ char *get_var_name(char *str); int exec_builtin(t_msh *msh); void get_cmd_path(t_msh *msh); int is_cmd_type(t_cmd *cmd); +int get_home_var_len(void); int set_echoctl(int value); int print_env(t_env *env); t_cmd *free_cmd(t_cmd *cmd); diff --git a/srcs/parsing.c b/srcs/parsing.c index af29897..9eeea37 100755 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ -/* Updated: 2024/05/06 16:26:35 by tomoron ### ########.fr */ +/* Updated: 2024/05/22 13:52:40 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -18,11 +18,16 @@ int add_home_to_str(char *res) i = 0; str = getenv("HOME"); - while (str[i]) + while (str && str[i]) { res[i] = str[i]; i++; } + if (!str) + { + res[i] = '~'; + return (1); + } return (i); } diff --git a/srcs/parsing_utils.c b/srcs/parsing_utils.c new file mode 100644 index 0000000..79e7fb3 --- /dev/null +++ b/srcs/parsing_utils.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/22 13:49:17 by tomoron #+# #+# */ +/* Updated: 2024/05/22 13:50:31 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int get_home_var_len(void) +{ + char *str; + + str = getenv("HOME"); + if (!str) + return (1); + else + return (ft_strlen("HOME")); +} diff --git a/srcs/parsing_var.c b/srcs/parsing_var.c index 3546932..b41638a 100755 --- a/srcs/parsing_var.c +++ b/srcs/parsing_var.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */ -/* Updated: 2024/05/08 11:59:25 by tomoron ### ########.fr */ +/* Updated: 2024/05/22 13:52:29 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,7 @@ int get_token_len(char *command) if (*command == '\'' && !in_dquote) in_quote = !in_quote; else if (*command == '~' && !in_quote && !in_dquote) - res += ft_strlen(getenv("HOME")); + res += get_home_var_len(); else if (*command != '\'' && *command != '"') res++; else if ((*command == '\'' && in_dquote) || (*command == '"'