fix: leak
This commit is contained in:
parent
5e0ae80404
commit
5caeed312c
53
execution.c
53
execution.c
@ -1,7 +1,17 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* execution.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/21 13:48:39 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libftx/libftx.h"
|
||||
#include "minishell.h"
|
||||
#include "utils/utils.h"
|
||||
#include <unistd.h>
|
||||
|
||||
static char *ft_get_executable_path(char *executable_name, t_list **env)
|
||||
{
|
||||
@ -83,10 +93,31 @@ static int ft_excutor(t_cmd *cmd, t_list **env)
|
||||
return (return_value);
|
||||
}
|
||||
|
||||
static int ft_own_cmd(t_list **env, t_cmd *cmd)
|
||||
{
|
||||
/* if (ft_strcmp(cmd->executable, "pwd") == 0) */
|
||||
/* return (pwd(env, cmd->fd_out)); */
|
||||
if (ft_strcmp(cmd->executable, "env") == 0)
|
||||
return (print_env(env, cmd->fd_out));
|
||||
if (ft_strcmp(cmd->executable, "export") == 0)
|
||||
return (print_export(env, cmd->fd_out));
|
||||
/* if (ft_strcmp(cmd->executable, "cd") == 0) */
|
||||
/* return (move_folder(cmd->args[0], cmd->fd_out)); */
|
||||
/* if (ft_strcmp(cmd->executable, "unset") == 0) */
|
||||
/* return (unset(env, cmd->args, cmd->fd_out)); */
|
||||
/* if (ft_strcmp(cmd->executable, "echo") == 0) */
|
||||
/* return (echo(cmd->fd_out, cmd->args[0])); */
|
||||
if (ft_strcmp(cmd->executable, "exit") == 0)
|
||||
return (-2);
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int ft_cmds_executor(t_list **cmds, t_list **env)
|
||||
{
|
||||
t_cmd *content;
|
||||
t_list *current;
|
||||
char *return_value;
|
||||
int cmd_return;
|
||||
int fds[2];
|
||||
|
||||
current = *cmds;
|
||||
@ -103,13 +134,25 @@ int ft_cmds_executor(t_list **cmds, t_list **env)
|
||||
content->fd_out = fds[1];
|
||||
((t_cmd *) current->next->content)->fd_in = fds[0];
|
||||
}
|
||||
content->executable = ft_get_executable_path(content->executable, env);
|
||||
if (content->executable != NULL)
|
||||
ft_excutor(content, env);
|
||||
cmd_return = ft_own_cmd(env, content);
|
||||
if (cmd_return == -1)
|
||||
{
|
||||
content->executable = ft_get_executable_path(
|
||||
content->executable, env);
|
||||
if (content->executable != NULL)
|
||||
cmd_return = ft_excutor(content, env);
|
||||
}
|
||||
if (content->fd_in != 0)
|
||||
close(content->fd_in);
|
||||
if (content->fd_out != 1 && content->fd_out != 2)
|
||||
close(content->fd_out);
|
||||
return_value = ft_itoa(cmd_return);
|
||||
if (return_value == NULL)
|
||||
{
|
||||
ft_eprintf("minishell: malloc failed\n");
|
||||
return (1);
|
||||
}
|
||||
set_value_by_key("?", return_value, env);
|
||||
current = current->next;
|
||||
}
|
||||
return (0);
|
||||
|
Loading…
Reference in New Issue
Block a user