Résolution temporaire pour #8 & ajout commentaires

This commit is contained in:
rick 2021-02-19 02:00:54 +01:00
parent 5c2a686e45
commit 8d4256f9d7
Signed by: Rick
GPG key ID: 2B593F087240EE99

View file

@ -44,26 +44,36 @@ int main()
while (commands[index] != NULL) while (commands[index] != NULL)
{ {
parse_string(commands[index], args, ' '); parse_string(commands[index], args, ' ');
if (!change_dir(args)) if (!change_dir(args)) /* si la commande demandée nest pas un cd */
{ {
pid = fork(); pid = fork();
if (!pid) if (!pid) /* le fils */
{ {
if (commands[index+1] == NULL) if (commands[index+1] == NULL)
{
dup2(my_pipe[0], STDIN_FILENO); dup2(my_pipe[0], STDIN_FILENO);
close(my_pipe[1]);
}
else if (index == 0) else if (index == 0)
{
dup2(my_pipe[1], STDOUT_FILENO); dup2(my_pipe[1], STDOUT_FILENO);
close(my_pipe[0]); close(my_pipe[0]);
close(my_pipe[1]); }
else
fclose(fdopen(my_pipe[0], "w"));
/* si la commande est intermédiaire dans le pipe, on vide le buffer
* de sortie */
execvp(args[0], args); execvp(args[0], args);
return 0; exit(0);
} }
/* lorsquon arrive à la dernière commande, on peut fermer le pipe et
* attendre le dernier processus */
if (commands[index + 1] == NULL) if (commands[index + 1] == NULL)
{ {
close(my_pipe[1]);
close(my_pipe[0]); close(my_pipe[0]);
close(my_pipe[1]);
waitpid(pid, NULL, 0); waitpid(pid, NULL, 0);
} }
} }