🔨」 fix(exec): fixed open FD when redirection with pipe

This commit is contained in:
yosyo
2024-08-01 16:39:48 +02:00
parent 180b1d2b71
commit 4081e962c0
3 changed files with 19 additions and 9 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */ /* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
/* Updated: 2024/07/18 14:34:25 by mmoussou ### ########.fr */ /* Updated: 2024/08/01 16:39:11 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,9 +37,10 @@ int is_in_builtins(char *cmd);
char *get_cmd_local_path(char *cmd, t_env *env); char *get_cmd_local_path(char *cmd, t_env *env);
void print_return_value(int return_code); void print_return_value(int return_code);
void __wait(int i); void __wait(int i);
void __close(void *content); void __close(void *content, int fd1, int fd2);
int check_file(char *cmd, char *input); int check_file(char *cmd, char *input);
void free_exec(t_env *env, char **env_array); void free_exec(t_env *env, char **env_array);
void ft_arrcpy(int *dst, int *src, int n);
void exec_cmd(t_cmd *cmd, char **env, t_env *env_t); void exec_cmd(t_cmd *cmd, char **env, t_env *env_t);
void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec); void __fork_single_cmd(t_cmd *cmd, char **env, t_env *env_t, t_exec exec);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */ /* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
/* Updated: 2024/08/01 06:10:04 by mmoussou ### ########.fr */ /* Updated: 2024/08/01 16:38:31 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,9 +26,8 @@ int exec_fork_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
if (!cmd->cmd) if (!cmd->cmd)
return (get_exit_code(0)); return (get_exit_code(0));
ft_arrcpy(exec.pipe_fd, pipe_fd, 2);
input = ft_strdup(cmd->cmd); input = ft_strdup(cmd->cmd);
exec.pipe_fd[0] = pipe_fd[0];
exec.pipe_fd[1] = pipe_fd[1];
exec.status = switch_cmd_path(cmd, env_t); exec.status = switch_cmd_path(cmd, env_t);
if (exec.status == -1 || !input || check_file(cmd->cmd, input)) if (exec.status == -1 || !input || check_file(cmd->cmd, input))
{ {
@ -106,7 +105,7 @@ t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env)
((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0]; ((t_cmd *)(list_cmd->next->content))->infile = exec.pipe_fd[0];
exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \ exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \
env, exec.pipe_fd); env, exec.pipe_fd);
__close(list_cmd->content); __close(list_cmd->content, exec.pipe_fd[0], exec.pipe_fd[1]);
if (exec.status != -1) if (exec.status != -1)
exec.i++; exec.i++;
list_cmd = list_cmd->next; list_cmd = list_cmd->next;
@ -114,7 +113,7 @@ t_exec exec_pipe(t_exec exec, t_list *list_cmd, t_env *env)
exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \ exec.status = exec_fork_cmd(list_cmd->content, exec.env_array, \
env, exec.pipe_fd); env, exec.pipe_fd);
__sig2(exec.status); __sig2(exec.status);
__close(list_cmd->content); __close(list_cmd->content, exec.pipe_fd[0], exec.pipe_fd[1]);
return (exec); return (exec);
} }

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/09 22:53:01 by adjoly #+# #+# */ /* Created: 2024/07/09 22:53:01 by adjoly #+# #+# */
/* Updated: 2024/07/16 15:49:01 by adjoly ### ########.fr */ /* Updated: 2024/08/01 16:38:54 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,12 +22,16 @@ void __wait(int i)
} }
} }
void __close(void *content) void __close(void *content, int fd1, int fd2)
{ {
if (((t_cmd *)(content))->outfile != STDOUT_FILENO) if (((t_cmd *)(content))->outfile != STDOUT_FILENO)
close(((t_cmd *)(content))->outfile); close(((t_cmd *)(content))->outfile);
if (((t_cmd *)(content))->infile != STDIN_FILENO) if (((t_cmd *)(content))->infile != STDIN_FILENO)
close(((t_cmd *)(content))->infile); close(((t_cmd *)(content))->infile);
if (fd1)
close(fd1);
if (fd2)
close(fd2);
} }
int send_error_exec(char *input) int send_error_exec(char *input)
@ -36,3 +40,9 @@ int send_error_exec(char *input)
free(input); free(input);
return (-1); return (-1);
} }
void ft_arrcpy(int *dst, int *src, int n)
{
while (n--)
dst[n] = src[n];
}