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
|
int test(char* buff, char* accu)
{
/*--sky : déclaration des variables avant toute action */
char temp[1024];
char sortie[1024];
/*
--sky : Pourquoi une allocation dynamique pour une taille fixe?
char *temp;
char *sortie;
*/
int fd1[2];
int fd2[2];
/*
--sky : tu te sers pas de r et s
int r,s;
*/
/*
--sky : Si on avait du laisser l'allocation dynamique
--sky : Cast inutile en C
temp = malloc(1024);
sortie = malloc(1024);
--sky : on vérifie si le malloc a echoué
if(temp == NULL || sortie == NULL)
{
exit(EXIT_FAILURE);
}
*/
/*--sky : pipe peut echouer */
//Création des pipes
if(pipe(fd1) == -1)
{
perror("pipe");
exit(errno);
}
if(pipe(fd2) == -1)
{
perror("pipe");
exit(errno);
}
/*--sky : un switch c'est plus pratique */
switch(fork())
{
/*--sky : fork peut echouer */
case -1 :
{
perror("fork");
exit(errno); /*--sky : #include <errno.h> */
}
case 0:
{
/*--sky : close peut echouer, ainsi que n'importe quel autre appel système */
if(close(fd1[1]) == -1)
{
perror("close");
exit(errno);
}
if(close(fd2[0]) == -1)
{
perror("close");
exit(errno);
}
/*--sky : euh les commentaires sont pas bons là, a mois que ce soit les parametres des fonctions */
if(dup2(fd1[0],0) == -1) //Sortie du pipe 1 sur l'entrée du fils
{
perror("dup2");
exit(errno);
}
if(dup2(fd2[1],1) == -1) //Sortie standard du fils sur entrée du pipe 2
{
perror("dup2");
exit(errno);
}
if(close(fd1[0]) == -1)
{
perror("close");
exit(errno);
}
if(close(fd2[1]) == -1)
{
perror("close");
exit(errno);
}
/*
--sky : alors la je comprends pas les argument mais alors pas du tout
read(0,temp,strlen(accu));
*/
if(read(fd1[0], temp, sizeof temp) == -1)
{
perror("read");
exit(errno);
}
printf("execution de la commande: %s\n",buff);
/*--sky : au fait ca sert a quoi ce que tu recois par le pere je comprends pas */
if(execlp(buff, buff, NULL) == -1)
{
perror("execlp");
exit(errno);
}
exit(1);
}
default:
{
if(close(fd1[0]) == -1)
{
perror("close");
exit(errno);
}
if(close(fd2[1]) == -1)
{
perror("close");
exit(errno);
}
/*--sky : ou est l'interet d'envoyer une chaine de caractere au fils, alors que cette chaine est passé en argument? */
if(write(fd1[1], accu, strlen(accu)) == -1) //envoie de l'argument en entrée du pipe (donc sur l'entrée std du fils)
{
perror("write");
exit(errno);
}
if(close(fd1[1]) == -1)
{
perror("close");
exit(errno);
}
while( r = read(fd2[0], sortie, sizeof sortie) > 0 )
printf("%s\n", sortie);
/*--sky : pas vraiment utile */
if(close(fd2[0]) == -1)
{
perror("close");
exit(errno);
}
}
}
/*
--sky : plus besoin des free
free(sortie);
free(temp);
*/
return 0;
} |
Partager