pipe sur redirection ne marche toujours pas

This commit is contained in:
hugogogo
2021-10-20 09:42:26 +02:00
parent f8c23f85b4
commit e08ceeaffe
3 changed files with 34 additions and 11 deletions

View File

@@ -1,6 +1,27 @@
#include "minishell.h"
void close_fd(t_cmd *cmd, pid_t pid)
{
if (cmd->fd_in != 0)
close(cmd->fd_in);
if (pid == 0)
{
if (cmd->pipe_out != 1)
{
close(cmd->pipe_in);
close(cmd->pipe_out);
}
if (cmd->fd_out != 1)
close(cmd->fd_out);
}
else
{
if (cmd->fd_out != 1 && cmd->fd_redirect == 1)
close(cmd->fd_out);
}
}
void exec_cmd(char **envp, t_list *cmd_list)
{
t_cmd *cmd;
@@ -15,23 +36,19 @@ void exec_cmd(char **envp, t_list *cmd_list)
if (pid == 0)
{
if (cmd->fd_in != 0)
{
dup2(cmd->fd_in, STDIN_FILENO);
close(cmd->fd_in);
}
if (cmd->fd_out != 1)
{
dup2(cmd->fd_out, STDOUT_FILENO);
close(cmd->fd_out);
}
close_fd(cmd, pid);
execve(cmd->argv[0], cmd->argv, envp);
}
else
{
if (cmd->fd_in != 0)
close(cmd->fd_in);
if (cmd->fd_out != 1 && cmd->fd_redirect == 1)
close(cmd->fd_out);
close_fd(cmd, pid);
// if (cmd->fd_in != 0)
// close(cmd->fd_in);
// if (cmd->fd_out != 1 && cmd->fd_redirect == 1)
// close(cmd->fd_out);
}
while ((wpid = wait(&status)) > 0);
cmd_list = cmd_list->next;

View File

@@ -32,6 +32,8 @@ int handle_fd(char **input, int i, int fdin, t_cmd *cmd)
cmd->fd_in = fdin;
cmd->fd_out = 1;
cmd->fd_redirect = 1;
cmd->pipe_in = 0;
cmd->pipe_out = 1;
next_in = 0;
if (input[i + 1])
{
@@ -39,6 +41,8 @@ int handle_fd(char **input, int i, int fdin, t_cmd *cmd)
pipe(pipes_fd);
cmd->fd_out = pipes_fd[1];
next_in = pipes_fd[0];
cmd->pipe_in = pipes_fd[0];
cmd->pipe_out = pipes_fd[1];
}
tmp = ft_strchr(input[i], '>');
if (tmp)