Browse Source

Fix for arguments wrapped in apostrophes and double quotation marks

master
Apostolos Fanakis 7 years ago
parent
commit
8ea9a8088c
  1. 14
      apostolofShellFunctions.c

14
apostolofShellFunctions.c

@ -170,14 +170,26 @@ void parseCommand(char *input, char **command, char ***argv, int *argc){
strcpy((*argv)[(*argc) - 1], *command); strcpy((*argv)[(*argc) - 1], *command);
(*argv)[(*argc) - 1][strlen((*argv)[(*argc) - 1])] = '\0'; (*argv)[(*argc) - 1][strlen((*argv)[(*argc) - 1])] = '\0';
//Parses rest of the arguments //Parses the rest of the arguments
token = strtok(NULL, " "); token = strtok(NULL, " ");
while (token != NULL){ while (token != NULL){
++(*argc); ++(*argc);
*argv = (char**)(realloc(*argv, (*argc) * sizeof(char*))); *argv = (char**)(realloc(*argv, (*argc) * sizeof(char*)));
//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))); (*argv)[(*argc) - 1] = (char*)(malloc((strlen(token) + 1) * sizeof(char)));
strcpy((*argv)[(*argc) - 1], trimWhitespaces(token)); strcpy((*argv)[(*argc) - 1], trimWhitespaces(token));
(*argv)[(*argc) - 1][strlen((*argv)[(*argc) - 1])] = '\0'; (*argv)[(*argc) - 1][strlen((*argv)[(*argc) - 1])] = '\0';
}
token = strtok(NULL, " "); token = strtok(NULL, " ");
} }

Loading…
Cancel
Save