diff --git a/command.c b/command.c index 067ff3d..e881758 100644 --- a/command.c +++ b/command.c @@ -1,15 +1,16 @@ -#include +#include "command.h" +#include #include -#include #include #include -#include "command.h" +#include #include "const.h" #include "source.h" #include "variable.h" -void executeCommand(char* input) -{ +void executeCommand(char* input) { + char* args[MAX_ARGS + 1] = {NULL}; + input[strlen(input) - 1] = '\0'; // terminate with null, rather than with \n char* args[MAX_ARGS + 1] = { NULL }; input[strlen(input) - 1] = '\0'; //terminate with null, rather than with \n @@ -23,26 +24,23 @@ void executeCommand(char* input) if(args[0] == NULL) return; // if command includes "=" set variable with the value after "=" as sting - if (strchr(args[0], '=') != NULL) - { - //printf("variable set, %s\n", args[0]); - addVariable(args[0]); - } - else if (strcmp(args[0], "exit") == 0) // exit shell + if (strchr(args[0], '=') != NULL) { + // printf("variable set, %s\n", args[0]); + addVariable(args[0]); + } else if (strcmp(args[0], "exit") == 0) // exit shell { - exit(0); - } - else if (strcmp(args[0], "env") == 0) // display all variables + exit(0); + } else if (strcmp(args[0], "env") == 0) // display all variables { - displayVariable(); - } - else if (strcmp(args[0], "source") == 0) // source command - { - printf("source command\n"); - sourceCommand(args); - } - else + displayVariable(); + } else if (strcmp(args[0], "source") == 0) // source command { + printf("source command\n"); + sourceCommand(args); + } else { + if (fork() == 0) // if inside the child process + { + int comm_res = execvp(args[0], args); if (fork() == 0) // if inside the child process {