-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
77 lines (70 loc) · 1.47 KB
/
pipe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include<unistd.h>
#include<string.h>
#include <dirent.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<time.h>
#include<grp.h>
#include <sys/wait.h>
#include<fcntl.h>
#include "cd.h"
#include "command.h"
#include "display.h"
#include "echo.h"
#include "execute.h"
#include "ls.h"
#include "redirection.h"
#include "nightswatch.h"
#include "pinfo.h"
#include "pwd.h"
#include "global.h"
#include "global.c"
void ExecutePipe(char command[],char * token,const char delemiter[])
{
char *sub_command[10000];
char temp[1000];
const char pipe_seperator[] = "|";
int no_pipes = 0,i;
char temp2[1000];
TokeniseCommands(sub_command,&no_pipes,command,pipe_seperator);
int stdin = dup(0);
int stdou = dup(1);
int fdin = dup(stdin),fdout;
char test[1000];
for(i=0;i<no_pipes;i++)
{
if(dup2(fdin,0) != 0)
{
perror("dup2 failed\n");
}
close(fdin);
if(i == no_pipes - 1)
{
fdout = dup(stdou);
}
else
{
int fildes[2];
if(pipe(fildes) < 0)
{
perror("pipes failed");
}
fdout = fildes[1];
fdin = fildes[0];
}
if(dup2(fdout,1) != 1)
{
perror("dup2 failed\n");
}
close(fdout);
strcpy(temp,sub_command[i]);
token = strtok(temp,delemiter);
ExecuteCommand(sub_command[i], token, delemiter);
}
dup2(stdin,0);
dup2(stdou,1);
return;
}