forked from sruthi2498/MyShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecCopyForIO.c
72 lines (56 loc) · 1.06 KB
/
execCopyForIO.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
execute(){
//save in/out
int tmpin = dup(0);
int tmpout = dup(1);
//InFile Saved in CurrentCommand structure
//Set fdin
int fdin;
if(CurrentCommand._inputFile){
fdin = open(CurrentCommand._inputFile, O_READ);
}
else{
//use default input
fdin=dup(tmpin);
}
int ret;
int fdout;
for(i=0; i<CurrentCommand._numberOfSimpleCurrentCommands;i++){
//stdin redirected to fdin
dup2(fdin,0);
close(fdin);
if(i == (CurrentCommand._numberOfSimpleCommands - 1)){
//Last simple CurrentCommand
if(outfile){
fdout = open(CurrentCommand._outFile, O_RDWR);
}
else{
fdout = dup(tmpout);
}
}
else{
//not last simple CurrentCommand
//Create a pipe
int fdpipe[2];
pipe(fdpipe);
fdout=fdpipe[1];
fdin=fdpipe[0];
}
//redirect output
dup2(fdout,1);
close(fdout);
//Create Child Process code
ret=fork()
.
.
.
}//end for
//restore in/out details
dup2(tmpin,0);
dup2(tmpout,1);
close(tmpin);
close(tmpout);
if(!CurrentCommand._background){
//wait for last CurrentCommand
waitpid(ret,NULL);
}
}