diff --git a/src/shell3.c b/src/shell3.c deleted file mode 100644 index 2a2d03c..0000000 --- a/src/shell3.c +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include -#include -#include - -#define MAX_LENGTH 200 - -char** u_get_input(char* buffer, char* user_input, int i); - -int main() -{ - char *buffer = (char *) malloc(MAX_LENGTH); - char *user_input1 = (char *) malloc(MAX_LENGTH); - char *user_input2 = (char *) malloc(MAX_LENGTH); - int pid1, pid2; - int my_pipe[2]; - int t = pipe(my_pipe); - if (t == -1) - { - printf("erreur à la création du pipe"); - return 1; - } - char **args1 = u_get_input(buffer, user_input1, 1); - char **args2 = NULL; - while (strcmp(user_input1, "exit") != 0) - { - args2 = u_get_input(buffer, user_input2, 2); - pid1 = fork(); - - if (!pid1) - { - dup2(my_pipe[1], STDOUT_FILENO); - close(my_pipe[0]); - close(my_pipe[1]); - execvp(user_input1, args1); - - return 0; - } - else { - pid2 = fork(); - - if (!pid2) - { - dup2(my_pipe[0], STDIN_FILENO); - close(my_pipe[1]); - close(my_pipe[0]); - execvp(user_input2, args2); - return 0; - } - else - { - close(my_pipe[0]); - close(my_pipe[1]); - waitpid(pid2, NULL, 0); - } - } - - free(args1); - free(args2); - args1 = u_get_input(buffer, user_input1, 1); - pipe(my_pipe); - } - free(args1); - free(buffer); - free(user_input1); - free(user_input2); - return 0; -} - -char** u_get_input(char* buffer, char* user_input, int j) -{ - printf("\ncommande%d> ", j); - memset(buffer, 0, MAX_LENGTH); - memset(user_input, 0, MAX_LENGTH); - fgets(buffer, MAX_LENGTH, stdin); - strncpy(user_input, buffer, strlen(buffer)-1); - - char *token = strtok(user_input, " "); - char **args = (char **) malloc(MAX_LENGTH); - int i = 0; - while (token != NULL && i < MAX_LENGTH) - { - args[i] = token; - i++; - token = strtok(NULL, " "); - } - return args; -} diff --git a/src/shellOpt.c b/src/shellOpt.c index bc46eab..96c7a47 100644 --- a/src/shellOpt.c +++ b/src/shellOpt.c @@ -1,12 +1,17 @@ +/** + * @file shellOpt.c + * @author rick + * @date 2021 + */ + +#include "parser.h" + #include #include #include #include #include -#include "parser.h" - - int main() { int pid; @@ -49,17 +54,17 @@ int main() */ - int index = 0; + int index = 0; while (strcmp(args[0], "exit")) { - if (args[1] != NULL) + while (args[index] != NULL) { - while (args[index] != NULL) + pid = fork(); + if (!pid) { - pid = fork(); - if (!pid) + tok_space(args[index], command); + if (args[1] != NULL) { - tok_space(args[index], command); if (args[index+1] == NULL) dup2(my_pipe[0], STDIN_FILENO); else if (index == 0) @@ -68,24 +73,24 @@ int main() dup2(my_pipe[0], STDIN_FILENO); dup2(my_pipe[1], STDOUT_FILENO); }*/ - close(my_pipe[0]); close(my_pipe[1]); - execvp(command[0], command); - return 0; } - else + + execvp(command[0], command); + return 0; + } + else + { + if (args[index + 1] == NULL) { - if (args[index + 1] == NULL) - { - close(my_pipe[1]); - close(my_pipe[0]); - //wait(&result); - waitpid(pid, NULL, 0); - } - //waitpid(-1, NULL, 0); - index++; + close(my_pipe[1]); + close(my_pipe[0]); + //wait(&result); + waitpid(pid, NULL, 0); } + //waitpid(-1, NULL, 0); + index++; } } @@ -97,7 +102,11 @@ int main() command[i] = (char *) calloc(MAX_LENGTH, sizeof(char)); } parse_char(args, '|'); - pipe(my_pipe); + if (pipe(my_pipe) == -1) + { + printf("erreur sur le pipe"); + return 1; + } index = 0; } @@ -108,4 +117,3 @@ int main() } return 0; } -