Bonjour, ça fait plusieurs jours que je suis sur la création d'un shell qui gère certaines commandes internes, les commandes en tâches de fond (elles commencent par "tf"), et là je bloque sur la création d'un pipe. Tout marche sauf le pipe. Je vous préviens le code est un peu "sale", on peut surement l'améliorer et l'optimiser, je ne comprends pas bien les commandes pipe() et dup2(), est-ce que quelqu'un pourrait me les expliquer pour le pipe ? Dans quel ordre faut-il les mettre ? Et quelles sont mes erreurs dans mon code ? Merci beaucoup !

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
 
#include <stdio.h>
#include <unistd.h>
#include "ligne_commande.h"
#include <sys/types.h> 
#include <sys/wait.h> 
#include <string.h>
#include <stdlib.h>
#include <assert.h>
 
int commandes_internes (char** cmd,int cpt );
int split(char** cmd, int cpt_str, char* commande1[],char* commande2[]);
int verif_pipe(char** cmd, int cpt_str);
 
int main()
{
	char** commande;
	char** commande1;
 
	char** commande2;
 
	int status;
	int nb,cpt,i;
	int pipefd[2];
	int cpt_str,j;
	int nb_split1=0;
	cpt=0;
	commande1 = malloc(sizeof(char*)*20);
	for(i=0;i<20; i++) 
	  commande1[i] = malloc(sizeof(char)*10); 
	commande2 = malloc(sizeof(char*)*20);
	for(i=0;i<20; i++) 
	  commande2[i] = malloc(sizeof(char)*10);
	setenv("INVITE", "\n> ", 1);
	do
	{
		printf("%s", getenv("INVITE"));
		fflush(stdout);
		commande=lis_ligne(&cpt_str);
		if(commande==NULL) return 0;
		if(commande[0]==NULL) continue;
 
		if( !commandes_internes(commande,cpt) ) //commande externe
		{			
			int pid=fork();
			if(pid==0)
			{
				if(strcmp(commande[0],"tf")!=0 && !verif_pipe(commande, cpt_str)) {nb=execvp(commande[0],commande);}
				else if (verif_pipe(commande, cpt_str))
				{
					while(strcmp(commande[nb_split1],"|")!=0)
					{
						strcpy(commande1[nb_split1],commande[nb_split1]);
						nb_split1=nb_split1+1;
					}
					commande1[nb_split1+1]=NULL;
					for(j=nb_split1+1;j<cpt_str;j++)
					{
						strcpy(commande2[j],commande[j]);
						j++;
					}
					commande2[j+1]=NULL;
 
					nb=pipe(pipefd);
					if(nb==-1)
						{
							perror("Erreur de l'appel systeme pipe");
							return(-1);
						}
					int pid3=fork();
					if(pid3==0)
					{
						dup2(pipefd[1], 1);
						close(pipefd[0]);
						close (pipefd[1]);
						nb=execvp(commande1[0],commande1);
						if(nb==-1) printf("Cette commande n'existe pas.\n");fflush(stdout);
					}
					else if (pid3 > 0)
					{
						/* Parent */
						dup2(pipefd[0],0);
						close(pipefd[0]);
						close(pipefd[1]);
						waitpid(pid3, &status,0);
					}
					else if (pid3==-1)
					{
						perror("Erreur de l'appel systeme fork");
						return(-1);
					}
					int pid4=fork();
					if(pid4==0)
					{
						nb=execvp(commande2[0],commande2);
						if(nb==-1) printf("Cette commande n'existe pas.\n");fflush(stdout);
					}
					else if (pid4 > 0)
					{
						/* Parent */
						waitpid(pid4, &status,0);
					}
					else if (pid4==-1)
					{
						perror("Erreur de l'appel systeme fork");
						return(-1);
					}
				}
				else 
				{				
					cpt++;				
					int pid2=fork();
					if(pid2==0)
					{					
						nb=execvp(commande[1],commande+1);
					}
					else if (pid2==-1)
					{
						perror("Erreur de l'appel systeme fork");
						return(-1);
					}
					else
					{					
						waitpid(pid2,&status,0);
						printf("Le processus en tache de fond numero %d est termine.",pid2);
					}
				}
				if(nb==-1) printf("Cette commande n'existe pas.\n");fflush(stdout);
			}
			else if (pid==-1)
			{
				perror("Erreur de l'appel systeme fork");
				return(-1);
			}
			else
			{
				if(strcmp(commande[0],"tf")!=0)
					waitpid(pid,&status,0);
			}
		}
	} while (fin_de_fichier(commande)==0 || ligne_vide(commande)==1);
return 0;
}
 
int commandes_internes (char** cmd,int cpt ){
 
	char* droite;
	int nb,i,status;	
	if( strcmp(cmd[0],"exit") == 0 )  //strcmp compare 2 chaînes de caractère et renvoie 0 si elles sont identiques
	{
		printf("fin du programme\n");
		for(i=0;i<cpt;i++)
		{
			waitpid(-1,&status,0);	
		}
		exit(0); //fin du programme
	}
 
 
	if( strcmp(cmd[0],"cd")==0 ) 
	{
		if(cmd[1])
		{
			nb=chdir(cmd[1]);
			if(nb==-1) perror("Erreur de la commande chdir.");
		}
		else printf("Syntaxe : cd argument");
		return 1;
	}
 
	if( strcmp(cmd[0],"export")==0 )
	{	
		if(cmd[1])
		{
		droite=separe_egal(cmd[1]);
		if(droite==NULL) printf("La commande export n'a pas de signe egal.");
		nb=setenv(cmd[1], droite, 1);
		if(nb==-1) perror("Il n'y a plus de place pour cette variable d'environnement.");
		}
		else
			printf("Syntaxe : export arg1=arg2");
		return 1;
	}
 
	return 0;
}
 
int verif_pipe(char** cmd, int cpt_str)
{
	int i;
	for(i=0;i<cpt_str;i++)
	{
		if(strcmp(cmd[i],"|")==0) return 1;
	}
	return 0;
}