From 085b54b67a61ce2f242a2f495980f0da3c33207e Mon Sep 17 00:00:00 2001 From: Pierre Valarcher Date: Wed, 29 Sep 2021 16:02:30 +0200 Subject: [PATCH] exo2 --- Exemples/04-Exec/exo2TDgr1.c | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Exemples/04-Exec/exo2TDgr1.c diff --git a/Exemples/04-Exec/exo2TDgr1.c b/Exemples/04-Exec/exo2TDgr1.c new file mode 100644 index 0000000..426aa27 --- /dev/null +++ b/Exemples/04-Exec/exo2TDgr1.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + pid_t pid; + int nb_pipe = 0; + int valDei; + char *argument[10]; + + if (argc <= 1) { + perror("pas le bon nb argument"); + exit(0); + } + + if (argc >= 10) { + perror("trop d'arguments"); + exit(0); + } + + for (int i = 0; i < argc; i++) { + argument[i] = argv[i+1]; + if (strcmp(argument[i],"|") == 0) { + argument[i] = NULL; + valDei = i+1; + nb_pipe = 1; + } + } + argument[argc-1] = NULL; + + pid = fork(); + + if (pid == 0) { + if (execvp(argv[1], argument) == -1) + { + perror("dans le exec"); + exit (0); + } + if (nb_pipe > 0) { + if (execvp(argument[valDei], &argument[valDei]) == -1) { + perror("dans le exec"); + exit (0); + } + } + + } else { + wait(NULL); + } + return EXIT_SUCCESS; +} \ No newline at end of file