Minishell/pipe.c
2022-03-08 18:34:47 +01:00

95 lines
2.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/07 11:13:32 by apommier #+# #+# */
/* Updated: 2022/03/08 18:33:51 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void execute(t_cmd *cmd)
{
//save in/out
int ret;
int fdout;
int tmpin = dup(0);
int tmpout= dup(1);
int fdin;
int fdpipe[2];
int i;
i = 0;
//set the initial input
if (cmd->current_s_cmd->infile)
{
fdin = open(cmd->current_s_cmd->infile, O_RDONLY);
printf("good chosse -redirect infile- fdin= %d\n", fdin);
}
else if (cmd->infile)
{
fdin = open(cmd->infile, O_RDONLY);
printf("good chosse -infile- fdin= %d\n", fdin);
}
else
{
fdin=dup(tmpin);
printf("good chosse -standar in- fdin= %d\n", fdin);
}
while( i < cmd->nb_s_cmd)
{
if (i != 0 && cmd->current_s_cmd->infile)
fdin = open(cmd->current_s_cmd->infile, O_RDONLY);
//redirect input
dup2(fdin, 0);
close(fdin);
//setup output
if (i == cmd->nb_s_cmd - 1)
{
// Last simple command
if (cmd->current_s_cmd->outfile)
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
else if(cmd->outfile)
fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
else// Use default output
fdout=dup(tmpout);
}
else
{
//not last
//simple command
//create pipe
//int fdpipe[2];
pipe(fdpipe);
fdout=fdpipe[1];
fdin=fdpipe[0];
}
// Redirect output
dup2(fdout,1);
close(fdout);
// Create child process
ret=fork();
int return_exec;
if(ret==0)
{
return_exec = execvp(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args);
_exit(1);
}
i++;
} //while
//restore in/out defaults
dup2(tmpin,0);
dup2(tmpout,1);
close(tmpin);
close(tmpout);
// Wait for last command
waitpid(ret, NULL, 0);
} // execute