fixed valgrind errors and some leaks

This commit is contained in:
mdev9
2024-02-26 15:28:45 +01:00
parent 20fadd3207
commit 36e3495356
4 changed files with 52 additions and 38 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{