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
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <string.h>
int main(int argc, char *argv[])
{
int fds[2];
char *commande;
pipe(fds);
if (fork() == 0) {
close(fds[0]);
dup2(fds[1], STDOUT_FILENO);
close(fds[1]);
if(argc = 5){
commande = strcat(argv[1]," ");
commande = strcat(commande,argv[2]);
commande = strcat("/bin/",commande);
char *const args[] = { commande, NULL };
execv(args[0], args);
}
else{
commande = strcat("/bin/",argv[1]);
char *const args[] = { commande, NULL };
execv(args[0], args);
}
_exit(EXIT_FAILURE);
}
if (fork() == 0) {
close(fds[1]);
dup2(fds[0], STDIN_FILENO);
close(fds[0]);
if(argc = 5){
commande = strcat(argv[3]," ");
commande = strcat(commande,argv[4]);
commande = strcat("/bin/",commande);
char *const args[] = { commande, NULL };
execv(args[0], args);
}
else{
commande = strcat("/bin/",argv[3]);
char *const args[] = { commande, NULL };
execv(args[0], args);
}
_exit(EXIT_FAILURE);
}
close(fds[0]);
close(fds[1]);
wait(NULL);
wait(NULL);
return 0;
} |
Partager