94 lines
2.6 KiB
C
94 lines
2.6 KiB
C
#include <stdio.h> /* entrées sorties */
|
|
#include <unistd.h> /* pimitives de base : fork, ...*/
|
|
#include <stdlib.h> /* exit */
|
|
#include <sys/wait.h> /* wait */
|
|
#include <string.h> /* opérations sur les chaines */
|
|
#include <fcntl.h> /* opérations sur les fichiers */
|
|
#include <errno.h>
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
extern int errno;
|
|
int retour;
|
|
int pipes[2][2];
|
|
|
|
if (pipe(pipes[0]) < 0)
|
|
{ // pipe failed
|
|
fprintf(stderr, "ERROR: pipe error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
if (pipe(pipes[1]) < 0)
|
|
{ // pipe failed
|
|
fprintf(stderr, "ERROR: pipe error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
|
|
if ((retour = fork()) < 0)
|
|
{ // fork failed
|
|
fprintf(stderr, "ERROR: fork error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
else if (retour == 0)
|
|
{ ////////// pipes[0][0] --> grep --> pipes[1][1] //////////
|
|
if (dup2(pipes[0][0], 0) == -1)
|
|
{ // dup2 fail
|
|
fprintf(stderr, "ERROR: dup2 error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
|
|
if (dup2(pipes[1][1], 1) == -1)
|
|
{ // dup2 fail
|
|
fprintf(stderr, "ERROR: dup2 error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
|
|
close(pipes[0][1]);
|
|
close(pipes[1][0]);
|
|
|
|
execlp("grep", "grep", argv[1], NULL);
|
|
fprintf(stderr, "ERROR: execlp error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
|
|
if ((retour = fork()) < 0)
|
|
{ // fork failed
|
|
fprintf(stderr, "ERROR: fork error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
else if (retour == 0)
|
|
{ ////////// pipes[1][0] --> wc --> stdout //////////
|
|
if (dup2(pipes[1][0], 0) == -1)
|
|
{ // dup2 fail
|
|
fprintf(stderr, "ERROR: dup2 error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
|
|
close(pipes[0][0]);
|
|
close(pipes[0][1]);
|
|
close(pipes[1][1]);
|
|
|
|
execlp("wc", "wc", "-l", NULL);
|
|
fprintf(stderr, "ERROR: execlp error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
|
|
////////// stdin --> who --> pipes[0][0] //////////
|
|
if (dup2(pipes[0][1], 1) == -1)
|
|
{ // dup2 fail
|
|
fprintf(stderr, "ERROR: dup2 error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
}
|
|
|
|
close(pipes[0][0]);
|
|
close(pipes[1][0]);
|
|
close(pipes[1][1]);
|
|
|
|
execlp("who", "who", NULL);
|
|
fprintf(stderr, "ERROR: execlp error, (%d) %s\n", errno, strerror(errno));
|
|
exit(errno);
|
|
|
|
// on atteint jamais cet endroit
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
// perror
|