Skip to content

Commit 0749f54

Browse files
DOLIPRANE74DOLIPRANE74
authored andcommitted
Ajout de macro pour entree sortie
1 parent 6b7676a commit 0749f54

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/shell.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "readcmd.h"
1515
#include "csapp.h"
1616

17+
#define ENTREE 1
18+
#define SORTIE 0
1719

1820
void handler(int sig){
1921
while(waitpid(-1, NULL, WNOHANG|WUNTRACED) > 0){}
@@ -39,8 +41,7 @@ int main()
3941
printf("exit\n");
4042
exit(0);
4143
}
42-
43-
44+
4445

4546
/* Display each command of the pipe */
4647
for (i=0; l->seq[i]!=0; i++) {
@@ -50,9 +51,7 @@ int main()
5051
}
5152
if (pipe(tube2) == -1){
5253
perror("pipe2 error\n");
53-
}
54-
55-
54+
}
5655
pid=fork();
5756
if(pid==0){
5857
//printf("cmd= %s ", l->seq[i][0]);
@@ -76,25 +75,25 @@ int main()
7675
}
7776

7877
if(i==0 && l->seq[i+1]==0){
79-
close(tube2[0]);
80-
close(tube2[1]);
78+
close(tube2[SORTIE]);
79+
close(tube2[ENTREE]);
8180
}
8281

8382
if(i==0 && l->seq[i+1]!=0){
84-
close(tube2[0]); //ferme sortie tube
85-
Dup2(tube2[1],1); // entrée tube dans sortie standard
83+
close(tube2[SORTIE]); //ferme sortie tube
84+
Dup2(tube2[ENTREE],1); // entrée tube dans sortie standard
8685
}
8786

8887
if(i!=0 && l->seq[i+1]==0){ // s'il y a une commande précédente
89-
close(tube[1]);
90-
Dup2(tube[0],0); // sortie tube dans entrée standard
88+
close(tube[ENTREE]);
89+
Dup2(tube[SORTIE],0); // sortie tube dans entrée standard
9190
}
9291

9392
if(i!=0 && l->seq[i+1]!=0){
94-
close(tube[1]);
95-
Dup2(tube[0],0);
96-
close(tube2[0]);
97-
Dup2(tube2[1],1);
93+
close(tube[ENTREE]);
94+
Dup2(tube[SORTIE],0);
95+
close(tube2[SORTIE]);
96+
Dup2(tube2[ENTREE],1);
9897
}
9998

10099
if (execvp(cmd[0],cmd)==-1) { //remplace le l->err et execute execvp
@@ -105,14 +104,14 @@ int main()
105104
} else { // père
106105

107106
if(tube[0]!=-1) {
108-
close(tube[0]);
107+
close(tube[SORTIE]);
109108
}
110109
if(tube[1]!=-1) {
111-
close(tube[1]);
110+
close(tube[ENTREE]);
112111
}
113112

114-
tube[0]=tube2[0];
115-
tube[1]=tube2[1];
113+
tube[SORTIE]=tube2[SORTIE];
114+
tube[ENTREE]=tube2[ENTREE];
116115

117116
if (l->ampersand == 0){
118117
if(waitpid(pid,&status,0)==-1){

0 commit comments

Comments
 (0)