ASR31-valarche-2021/Exemples/04-Exec/exo2TDgr2.c

36 lines
646 B
C
Raw Permalink Normal View History

2021-09-29 17:15:47 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
int main(int argc, char *argv[]) {
pid_t pid;
int indice = 0;
if (argc < 2) {
perror("argument pas bon");
exit (0);
}
/* Est ce qu'il y a un pipe AA */
for (int i = 0; i < argc; i++) {
if (strcmp("AA", argv[i]) == 0) {
argv[i] = NULL;
indice = i;
break;
}
}
if (indice == 0)
/* si il n'y a pas de pipe AA */
execvp(argv[1], &argv[1]);
else {
/* si il y a un pipe AA */
pid = fork();
if (pid == 0)
execvp(argv[indice+1], &argv[indice+1]);
else execvp(argv[1], &argv[1]);
}
return EXIT_SUCCESS;
}