From 36e34953561ed390506d5b45b373de9b81e5f833 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Mon, 26 Feb 2024 15:28:45 +0100 Subject: [PATCH] fixed valgrind errors and some leaks --- srcs/exec.c | 63 +++++++++++++++++++++++----------------------- srcs/main.c | 2 +- srcs/minishellrc.c | 4 ++- srcs/path.c | 21 ++++++++++++---- 4 files changed, 52 insertions(+), 38 deletions(-) diff --git a/srcs/exec.c b/srcs/exec.c index 390d6ea..cc4b570 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/02/26 13:41:19 by marde-vr ### ########.fr */ +/* Updated: 2024/02/26 14:52:54 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,6 +70,25 @@ int exec_builtin(t_msh *msh) return (STDOUT_FILENO); } +int get_cmd_count(t_cmd *cmds) +{ + int count; + t_cmd *cur_cmd; + + count = 0; + cur_cmd = cmds; + while (cur_cmd->next != 0) + { + if (cur_cmd->type != ARG && (cur_cmd->next + && cur_cmd->next->type == ARG)) + count++; + cur_cmd = cur_cmd->next; + } + if (cur_cmd->type == ARG) + count++; + return (count); +} + void free_msh(t_msh *msh) { if (msh) @@ -80,6 +99,10 @@ void free_msh(t_msh *msh) free_env(msh->env); if (msh->aliases) free_alias(msh->aliases); + if (msh->pids) + free(msh->pids); + if (msh->fds) + free(msh->fds); free(msh); } } @@ -189,36 +212,15 @@ void pipe_parent(t_msh *msh, int i, int cmd_count) } } -int get_cmd_count(t_cmd *cmds) -{ - int count; - t_cmd *cur_cmd; - - count = 0; - cur_cmd = cmds; - while (cur_cmd->next != 0) - { - if (cur_cmd->type != ARG && (cur_cmd->next - && cur_cmd->next->type == ARG)) - count++; - cur_cmd = cur_cmd->next; - } - if (cur_cmd->type == ARG) - count++; - return (count); -} - int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) { pid_t pid; if (i != cmd_count - 1) { - //ft_printf_fd(2, "piping\n"); if (pipe(msh->fds[i]) == -1) { perror("pipe"); - //ft_printf_fd(2, "exiting (pipe)\n"); // debug ft_exit(msh, 1); } } @@ -226,7 +228,6 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) if (pid == -1) { perror("fork"); - //ft_printf_fd(2, "exiting (fork)\n"); //debug ft_exit(msh, 1); } if (pid == 0) @@ -234,14 +235,8 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) else { pipe_parent(msh, i, cmd_count); - //rl_redisplay(); - /* - if (i != 0 && msh->fds[i - 1][1] != 0) - close(msh->fds[i - 1][1]); - if (i != 0 && msh->fds[i][0] != 0) - close(msh->fds[i][0]); - */ msh->pids[i] = pid; + free(cmd_args); } return (0); } @@ -340,5 +335,11 @@ void exec_command(t_msh *msh) i--; } i = 0; - //todo: free fds tab + while (i < cmd_count) + { + free(msh->fds[i]); + i++; + } + free(msh->fds); + free(msh->pids); } diff --git a/srcs/main.c b/srcs/main.c index 37cdc88..ac93de0 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/02/23 14:06:24 by marde-vr ### ########.fr */ +/* Updated: 2024/02/26 15:28:23 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/minishellrc.c b/srcs/minishellrc.c index 81c8baa..8e585ce 100644 --- a/srcs/minishellrc.c +++ b/srcs/minishellrc.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */ -/* Updated: 2024/02/21 17:34:26 by marde-vr ### ########.fr */ +/* Updated: 2024/02/26 15:24:28 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,10 +24,12 @@ void exec_rc_file(t_msh *msh, int fd) msh->cmds = parse_command(line, msh->env); exec_command(msh); free_cmd(msh->cmds); + free(line); } free(line); line = get_next_line(fd); } + free(line); } void handle_minishellrc(t_msh *msh) diff --git a/srcs/path.c b/srcs/path.c index caea54d..9519cdf 100644 --- 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/02/21 22:51:53 by marde-vr ### ########.fr */ +/* Updated: 2024/02/26 14:49:52 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,11 +65,18 @@ void find_cmd_path(t_msh *msh, char **paths, int *found) } } -/* -void get_cmd_name(t_msh *msh) +void free_paths(char **paths) { - if () -}*/ + int i; + + i = 0; + while(paths[i]) + { + free(paths[i]); + i++; + } + free(paths); +} void get_cmd_path(t_msh *msh) { @@ -84,8 +91,12 @@ void get_cmd_path(t_msh *msh) { paths = split_paths_from_env(msh->env); if (!paths) + { + free_paths(paths); ft_exit(msh, 1); + } find_cmd_path(msh, paths, &found); + free_paths(paths); } if (!found) {