From 8ea9a8088ce0cab454fa93c3d08116e968c0e895 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Thu, 8 Feb 2018 01:17:56 +0200 Subject: [PATCH] Fix for arguments wrapped in apostrophes and double quotation marks --- apostolofShellFunctions.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/apostolofShellFunctions.c b/apostolofShellFunctions.c index 0e5090d..2378ad2 100644 --- a/apostolofShellFunctions.c +++ b/apostolofShellFunctions.c @@ -170,14 +170,26 @@ void parseCommand(char *input, char **command, char ***argv, int *argc){ strcpy((*argv)[(*argc) - 1], *command); (*argv)[(*argc) - 1][strlen((*argv)[(*argc) - 1])] = '\0'; - //Parses rest of the arguments + //Parses the rest of the arguments token = strtok(NULL, " "); while (token != NULL){ ++(*argc); *argv = (char**)(realloc(*argv, (*argc) * sizeof(char*))); - (*argv)[(*argc) - 1] = (char*)(malloc((strlen(token) + 1) * sizeof(char))); - strcpy((*argv)[(*argc) - 1], trimWhitespaces(token)); - (*argv)[(*argc) - 1][strlen((*argv)[(*argc) - 1])] = '\0'; + + //Checks if argument is wrapped inside " or ' characters + if ( (token[0] == '"' && token[strlen(token) - 1] == '"' ) || + (token[0] == '\'' && token[strlen(token) - 1] == '\'' ) ){ + token++; + token[strlen(token) - 1] = '\0'; + + (*argv)[(*argc) - 1] = (char*)(malloc((strlen(token)) * sizeof(char))); + strcpy((*argv)[(*argc) - 1], trimWhitespaces(token)); + } else { + (*argv)[(*argc) - 1] = (char*)(malloc((strlen(token) + 1) * sizeof(char))); + strcpy((*argv)[(*argc) - 1], trimWhitespaces(token)); + (*argv)[(*argc) - 1][strlen((*argv)[(*argc) - 1])] = '\0'; + } + token = strtok(NULL, " "); }