From b1c199cc74cb95247d9ea33ada7887ce00ddba94 Mon Sep 17 00:00:00 2001 From: Skylar Date: Sun, 27 Mar 2022 20:58:03 +1300 Subject: [PATCH] Fixed history command --- ash.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/ash.c b/ash.c index ba398a8..fb98ae4 100644 --- a/ash.c +++ b/ash.c @@ -4,6 +4,8 @@ #include #include +int historyAmount = 0; + char *readLine(void) { printf("ash> "); @@ -83,9 +85,21 @@ void historyCommand(char *commandType, char **historyOfUser) return; } - if (commandType==NULL) + if (commandType==NULL && historyAmount < 11) { - for (int i = 1; i <= 10; i++) + for (int i = 1; i <= historyAmount; i++) + { + printf("%d: %s\n", i, historyOfUser[i-1]); + + if (historyOfUser[i]==NULL) + { + break; + } + } + } + else if (commandType==NULL) + { + for (int i = historyAmount-9; i <= historyAmount; i++) { printf("%d: %s\n", i, historyOfUser[i-1]); @@ -121,6 +135,8 @@ void historyCommand(char *commandType, char **historyOfUser) splitToArgs(fullCommandWanted, historyArgs); executeAndWait(historyArgs); + + free(historyArgs); } @@ -129,16 +145,12 @@ void historyCommand(char *commandType, char **historyOfUser) void addToHistory(char *userString, char **historyOfUser) { - for (int i = 9; i > 0; i--) - { - historyOfUser[i] = historyOfUser[i-1]; - } - char *temp = malloc(1000); strcpy(temp, userString); - historyOfUser[0] = temp; + historyOfUser[historyAmount] = temp; + historyAmount++; } int main() @@ -172,11 +184,8 @@ int main() splitToArgs(fullCommand, userArgs); - // Adding this to command history, unless it is the history check command - if (strcmp(userArgs[0], "history") != 0 && strcmp(userArgs[0], "h") != 0) - { - addToHistory(constantFullCommand, historyCommands); - } + // Adding this to command history + addToHistory(constantFullCommand, historyCommands); // If command is cd if (strcmp(userArgs[0], "cd") == 0)