diff --git a/includes/minishell.h b/includes/minishell.h index 4fc452f..48c03ef 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: lderidde +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/27 11:25:25 by lderidde #+# #+# */ -/* Updated: 2025/02/11 14:20:44 by lderidde ### ########.fr */ +/* Updated: 2025/02/18 09:08:46 by lderidde ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,7 @@ typedef struct s_msh t_ast_n *head; int here_fd; char *input; + char *prev_input; int hist; char **env; } t_msh; diff --git a/srcs/execution/exec_cmd.c b/srcs/execution/exec_cmd.c index ec6df0e..dabe828 100644 --- a/srcs/execution/exec_cmd.c +++ b/srcs/execution/exec_cmd.c @@ -82,6 +82,7 @@ int exec(t_ast_n *node) if (execve(path, node->args, node->msh->env) == -1) { free_child(node->msh); + ft_free(&path); perror("execve"); exit(1); } diff --git a/srcs/execution/exec_heredoc.c b/srcs/execution/exec_heredoc.c index fb2e69a..060c205 100644 --- a/srcs/execution/exec_heredoc.c +++ b/srcs/execution/exec_heredoc.c @@ -96,7 +96,7 @@ void read_input(t_ast_n *node, int j) check = ifhere_remove_quote(node, j); str = get_next_line(node->msh->here_fd, 0); - while (str && ft_strncmp(str, node->files[j], ft_strlen(str) - 1) != 0) + while (str && ft_strncmp(str, node->files[j], -1) != 0) { if (!check) expander_here(&str, node); diff --git a/srcs/expander/expand_var.c b/srcs/expander/expand_var.c index 5ffb482..5f92e46 100644 --- a/srcs/expander/expand_var.c +++ b/srcs/expander/expand_var.c @@ -51,21 +51,24 @@ int expand_exit(t_ast_n *node, int j, int i) { int k; char *new; + char *num; char *ret; int len; k = -1; if (node->args[j][i + 1] && node->args[j][i + 1] == '?') { - len = ft_strlen(ft_itoa(node->msh->ex_code)); + num = ft_itoa(node->msh->ex_code); + len = ft_strlen(num); new = ft_calloc(ft_strlen(node->args[j]) + len + 1, 1); if (!new) return (0); ret = new; while (++k < i) *new++ = node->args[j][k]; - ft_strlcat(new, ft_itoa(node->msh->ex_code), -1); + ft_strlcat(new, num, -1); ft_strlcat(new, &node->args[j][i + 2], -1); + ft_free(&num); ft_free(&node->args[j]); node->args[j] = ret; return (1); diff --git a/srcs/main.c b/srcs/main.c index 874fbda..9b3ba7e 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -28,6 +28,8 @@ static void add_prevhistory(t_msh *msh) while (str) { tmp = ft_substr(str, 0, ft_strlen(str) - 1); + ft_free(&msh->prev_input); + msh->prev_input = ft_strdup(tmp); add_history(tmp); free(tmp); free(str); @@ -67,6 +69,7 @@ static void exit_manual(t_msh *msh) } else ret = msh->ex_code; + ft_free(&msh->prev_input); free_msh(msh); ft_fprintf(2, "exit\n"); exit(ret); diff --git a/srcs/msh_struct.c b/srcs/msh_struct.c index ff0f37b..9bc292a 100644 --- a/srcs/msh_struct.c +++ b/srcs/msh_struct.c @@ -39,6 +39,7 @@ t_msh *init_msh(char **envp) msh->ex_code = 0; msh->here_fd = -1; msh->input = NULL; + msh->prev_input = NULL; if (!msh) return (NULL); if (!envp[0]) @@ -56,5 +57,6 @@ void free_msh(t_msh *msh) if (msh->here_fd != -1) close(msh->here_fd); free(msh->input); + ft_free(&msh->prev_input); free(msh); } diff --git a/srcs/powerline.c b/srcs/powerline.c index da02a53..4b75833 100644 --- a/srcs/powerline.c +++ b/srcs/powerline.c @@ -17,9 +17,16 @@ static void handle_input(char *in, t_msh *msh) { if (ft_strlen(in) > 0 && !is_only_space(in)) { - add_history(in); - ft_fprintf(msh->hist, "%s\n", in); + if (ft_strlen(in) != ft_strlen(msh->prev_input) || + ft_strncmp(in, msh->prev_input, -1) != 0) + { + add_history(in); + ft_fprintf(msh->hist, "%s\n", in); + } } + ft_free(&msh->prev_input); + if (in) + msh->prev_input = ft_strdup(in); } char *get_pwd(void)