From 642a9ab2325d7615904b68a9fc276e4a66313c64 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Mon, 22 Apr 2024 19:19:55 +0200 Subject: [PATCH] fixed leaks and some redirection errors --- srcs/exec_bonus.c | 12 +++++++----- srcs/input_redirections.c | 8 ++++++-- srcs/main.c | 4 +--- srcs/minishell.h | 6 +++--- srcs/output_redirections.c | 27 +++++++++++---------------- srcs/pipe.c | 6 +++--- 6 files changed, 31 insertions(+), 32 deletions(-) diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 13c2f24..dca63e6 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/22 16:43:18 by tomoron ### ########.fr */ +/* Updated: 2024/04/22 19:18:52 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,12 +26,12 @@ void get_redirections(t_msh *msh, t_cmd *cmds) if (!get_out_type(msh, cmds)) get_in_type(msh, cmds); } - + /* printf("in_type:"); print_cmd_type(msh->in_type, 0); printf("\nout_type:"); print_cmd_type(msh->out_type, 0); - printf("\n"); + printf("\n");*/ } t_cmd *get_next_command(t_cmd *cmd) @@ -93,6 +93,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) if (pipe(msh->fds[i]) == -1) { perror("minishell: pipe"); + free(cmd_args); ft_exit(msh, 1); } //fprintf(stderr, "pipe: msh->fds[%d][0]: %d, msh->fds[%d][1]: %d\n", i, msh->fds[i][0], i, msh->fds[i][1]); @@ -101,6 +102,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) if (pid == -1) { perror("minishell: fork"); + free(cmd_args); ft_exit(msh, 1); } if (pid == 0) @@ -132,10 +134,10 @@ int get_cmd_count(t_cmd *cmds) int nb; nb = 0; - while(cmds && (is_output_type(cmds) || is_input_type(cmds))) - cmds=cmds->next; while (cmds && !is_operand_type(cmds)) { + while(cmds && (is_output_type(cmds) || is_input_type(cmds))) + cmds=cmds->next; if (is_cmd_type(cmds)) nb++; while(cmds && (is_output_type(cmds) || is_input_type(cmds) || is_cmd_type(cmds))) diff --git a/srcs/input_redirections.c b/srcs/input_redirections.c index a649352..8be566f 100755 --- a/srcs/input_redirections.c +++ b/srcs/input_redirections.c @@ -6,18 +6,21 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ -/* Updated: 2024/04/22 17:02:13 by tomoron ### ########.fr */ +/* Updated: 2024/04/22 19:12:50 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void redirect_input(t_msh *msh, int i) +void redirect_input(t_msh *msh, int i, char **cmd_args) { if (msh->in_type != PIPE) { if (dup2(msh->in_fd, 0) < 0) + { + free(cmd_args); ft_exit(msh, 1); + } close(msh->in_fd); } else @@ -25,6 +28,7 @@ void redirect_input(t_msh *msh, int i) if (dup2(msh->fds[i - 1][0], 0) < 0) { perror("dup2"); //debug + free(cmd_args); ft_exit(msh, 1); } } diff --git a/srcs/main.c b/srcs/main.c index 7891333..7f99bf4 100755 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ -/* Updated: 2024/04/22 18:39:39 by tomoron ### ########.fr */ +/* Updated: 2024/04/22 18:41:29 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,6 @@ char *get_prompt(t_env *env) char cwd_buffer[100]; char *cwd; - if(!isatty(0)) - return(ft_strdup("")); res = ft_strjoin_free("\001", ft_get_color(10, 255, 80), 2); res = ft_strjoin_free(res, "\033[1m\002", 1); if (getenv("USER")) diff --git a/srcs/minishell.h b/srcs/minishell.h index 94f4e4d..3202768 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/04/22 15:25:22 by marde-vr ### ########.fr */ +/* Updated: 2024/04/22 19:13:12 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,14 +107,14 @@ int file_access(t_msh *msh, int *found); void remove_command_from_msh(t_msh *msh); void ft_exit(t_msh *msh, int error_code); void sort_wildcards_token(t_token *list); +void redirect_input(t_msh *msh, int i, char **cmd_args); +void redirect_output(t_msh *msh, int i, char **cmd_args); void print_cmd_type(t_cmd_type type, char *value); int cmd_is_forkable_builtin(char *cmd_token); void signal_handler_command(int signum); void ft_exit(t_msh *msh, int exit_code); -void redirect_output(t_msh *msh, int i); char **split_paths_from_env(t_env *env); int add_return_code_to_str(char *res); -void redirect_input(t_msh *msh, int i); void parse_var(t_msh *msh, char *line); void print_parsed_token(t_token *cmd);//debug int get_var_name_len(char *command); diff --git a/srcs/output_redirections.c b/srcs/output_redirections.c index 867af90..0472ca9 100755 --- a/srcs/output_redirections.c +++ b/srcs/output_redirections.c @@ -6,30 +6,30 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */ -/* Updated: 2024/04/19 14:10:30 by tomoron ### ########.fr */ +/* Updated: 2024/04/22 19:12:32 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -#include -void redirect_output(t_msh *msh, int i) +void redirect_output(t_msh *msh, int i, char **cmd_args) { if (msh->out_type != PIPE) { - //fprintf(stderr, "redirecting output\n"); if (dup2(msh->out_fd, 1) < 0) + { + free(cmd_args); ft_exit(msh, 1); + } + } else { - //fprintf(stderr, "redirecting pipe output\n"); - //sleep(1); if (dup2(msh->fds[i][1], 1) < 0) { perror("dup2"); - //fprintf(stderr, "exiting\n"); - //ft_exit(msh, 1); + free(cmd_args); + ft_exit(msh, 1); } } } @@ -51,7 +51,7 @@ int open_out_file(t_msh *msh, t_cmd **cur_cmd, char *filename) while ((*cur_cmd)->next && is_cmd_type((*cur_cmd)->next)) *cur_cmd = (*cur_cmd)->next; if ((*cur_cmd)->next && is_output_type((*cur_cmd)->next)) - get_out_type(msh, *cur_cmd); + get_out_type(msh, (*cur_cmd)->next); } return (0); } @@ -66,17 +66,12 @@ int get_out_type(t_msh *msh, t_cmd *cmds) msh->out_fd = 0; ret = 0; cur_cmd = cmds; - while (cur_cmd && !is_cmd_type(cur_cmd)) + while (cur_cmd && cur_cmd->next && (!is_cmd_type(cur_cmd) && !is_output_type(cur_cmd))) cur_cmd = cur_cmd->next; while (cur_cmd && cur_cmd->next && !is_output_type(cur_cmd) && !is_operand_type(cur_cmd) && cur_cmd->cmd_type != PIPE) - { - //fprintf(stderr, "%s: %d\n", cur_cmd->value, cur_cmd->cmd_type); cur_cmd = cur_cmd->next; - } - //if (cur_cmd) - //fprintf(stderr, "%s: %d\n", cur_cmd->value, cur_cmd->cmd_type); - if (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN) + if (cur_cmd && (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN)) msh->out_type = 0; else if (cur_cmd && is_output_type(cur_cmd) && !is_operand_type(cur_cmd) && cur_cmd->cmd_type != PIPE) diff --git a/srcs/pipe.c b/srcs/pipe.c index 07aee57..fd78ecb 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/22 13:20:25 by marde-vr ### ########.fr */ +/* Updated: 2024/04/22 19:18:39 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,10 +55,10 @@ void child(t_msh *msh, char **cmd_args, int i) if ((msh->in_type != CMD && msh->in_type != PAREN && msh->in_type != AND && msh->in_type != OR && msh->in_type != PIPE) || (msh->in_type == PIPE && i > 0)) - redirect_input(msh, i); + redirect_input(msh, i, cmd_args); if (msh->out_type == PIPE || msh->out_type == RED_O || msh->out_type == RED_O_APP) - redirect_output(msh, i); + redirect_output(msh, i, cmd_args); close_pipe_fds(msh, i); execute_command(msh, cmd_args); close(0);