From 6f9ad93fb3314c566654f8808222cab33c365196 Mon Sep 17 00:00:00 2001 From: Tomasz Lewicki Date: Wed, 1 Apr 2020 15:13:27 -0700 Subject: [PATCH 1/2] proposed unified formatting in command.c --- command.c | 66 ++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/command.c b/command.c index 067ff3d..01c7dc1 100644 --- a/command.c +++ b/command.c @@ -1,16 +1,22 @@ -#include +#include "command.h" +#include #include -#include #include +<<<<<<< Updated upstream +======= +#include +>>>>>>> Stashed changes #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 +<<<<<<< Updated upstream char* args[MAX_ARGS + 1] = { NULL }; input[strlen(input) - 1] = '\0'; //terminate with null, rather than with \n @@ -21,33 +27,49 @@ void executeCommand(char* input) token = strtok(NULL, " "); } if(args[0] == NULL) return; +======= + char* token = strtok(input, " "); + for (int i = 0; token != NULL && i < MAX_ARGS; ++i) { + args[i] = token; + token = strtok(NULL, " "); + } + if (args[0] == NULL) return; // empty input +>>>>>>> Stashed changes // 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); +<<<<<<< Updated upstream if (fork() == 0) // if inside the child process { exit(execvp(args[0], args)); } +======= + if (comm_res == -1) // execvp encountered error + { + printf("Command '%s' exited with the following error: %s \n", args[0], + strerror(errno)); + exit(-1); + } else + exit(0); + } +>>>>>>> Stashed changes } wait(NULL); } From 0c7214104ec2211c19bed498bde7ef8ae5ae6ff4 Mon Sep 17 00:00:00 2001 From: Tomasz Lewicki Date: Wed, 1 Apr 2020 15:29:08 -0700 Subject: [PATCH 2/2] fixed last commit - I didnt save the file before staging --- command.c | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/command.c b/command.c index 01c7dc1..e881758 100644 --- a/command.c +++ b/command.c @@ -2,10 +2,6 @@ #include #include #include -<<<<<<< Updated upstream -======= -#include ->>>>>>> Stashed changes #include #include #include "const.h" @@ -16,7 +12,6 @@ void executeCommand(char* input) { char* args[MAX_ARGS + 1] = {NULL}; input[strlen(input) - 1] = '\0'; // terminate with null, rather than with \n -<<<<<<< Updated upstream char* args[MAX_ARGS + 1] = { NULL }; input[strlen(input) - 1] = '\0'; //terminate with null, rather than with \n @@ -27,14 +22,6 @@ void executeCommand(char* input) { token = strtok(NULL, " "); } if(args[0] == NULL) return; -======= - char* token = strtok(input, " "); - for (int i = 0; token != NULL && i < MAX_ARGS; ++i) { - args[i] = token; - token = strtok(NULL, " "); - } - if (args[0] == NULL) return; // empty input ->>>>>>> Stashed changes // if command includes "=" set variable with the value after "=" as sting if (strchr(args[0], '=') != NULL) { @@ -55,21 +42,10 @@ void executeCommand(char* input) { { int comm_res = execvp(args[0], args); -<<<<<<< Updated upstream if (fork() == 0) // if inside the child process { exit(execvp(args[0], args)); } -======= - if (comm_res == -1) // execvp encountered error - { - printf("Command '%s' exited with the following error: %s \n", args[0], - strerror(errno)); - exit(-1); - } else - exit(0); - } ->>>>>>> Stashed changes } wait(NULL); }