test pipe
This commit is contained in:
parent
8f391e5c2a
commit
914d6ceafd
3
main.c
3
main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||||
/* Updated: 2022/03/06 17:41:41 by apommier ### ########.fr */
|
/* Updated: 2022/03/07 11:57:14 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,6 +22,7 @@ int main(int ac, char **av, char **path)
|
|||||||
char *input;
|
char *input;
|
||||||
|
|
||||||
printf("---MINISHELL START---\n");
|
printf("---MINISHELL START---\n");
|
||||||
|
execute(t_cmd *cmd, *infile, *outfile);
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
color();
|
color();
|
||||||
|
|||||||
@ -37,12 +37,13 @@ typedef struct s_command {
|
|||||||
char *out_file;
|
char *out_file;
|
||||||
char *input_file;
|
char *input_file;
|
||||||
char *err_file;
|
char *err_file;
|
||||||
int background;
|
|
||||||
struct s_command *current_cmd;
|
|
||||||
struct s_simple *current_s_cmd;
|
struct s_simple *current_s_cmd;
|
||||||
} t_cmd;
|
} t_cmd;
|
||||||
|
|
||||||
//main.c
|
//main.c
|
||||||
int main();//int ac, char **av, char **path);
|
int main();//int ac, char **av, char **path);
|
||||||
|
|
||||||
|
//pipe.c
|
||||||
|
void execute(t_cmd *cmd, char *infile, char *outfile);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
84
pipe.c
Normal file
84
pipe.c
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pipe.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/03/07 11:13:32 by apommier #+# #+# */
|
||||||
|
/* Updated: 2022/03/07 11:52:26 by apommier ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void execute(t_cmd *cmd, char *infile, char *outfile)
|
||||||
|
{
|
||||||
|
//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 (infile)
|
||||||
|
fdin = open(infile,O_READ);
|
||||||
|
else //Use default input
|
||||||
|
fdin=dup(tmpin);
|
||||||
|
|
||||||
|
while( i < numsimplecommands)
|
||||||
|
{
|
||||||
|
//redirect input
|
||||||
|
dup2(fdin, 0);
|
||||||
|
close(fdin);
|
||||||
|
//setup output
|
||||||
|
if (i == numsimplecommands - 1)
|
||||||
|
{
|
||||||
|
if(outfile)// Last simple command
|
||||||
|
fdout=open(outfile,……);
|
||||||
|
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];
|
||||||
|
}// if/else
|
||||||
|
|
||||||
|
// Redirect output
|
||||||
|
dup2(fdout,1);
|
||||||
|
close(fdout);
|
||||||
|
|
||||||
|
// Create child process
|
||||||
|
ret=fork();
|
||||||
|
if(ret==0)
|
||||||
|
{
|
||||||
|
execvp(scmd[i].args[0], scmd[i].args);
|
||||||
|
perror(“execvpâ€);
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
} //while
|
||||||
|
|
||||||
|
//restore in/out defaults
|
||||||
|
dup2(tmpin,0);
|
||||||
|
dup2(tmpout,1);
|
||||||
|
close(tmpin);
|
||||||
|
close(tmpout);
|
||||||
|
|
||||||
|
if (!background) {
|
||||||
|
// Wait for last command
|
||||||
|
waitpid(ret, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // execute
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user