Skip to content

Commit

Permalink
Fixed history command
Browse files Browse the repository at this point in the history
  • Loading branch information
Biscuitmunch committed Mar 27, 2022
1 parent 938091d commit b1c199c
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions ash.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <unistd.h>
#include <sys/wait.h>

int historyAmount = 0;

char *readLine(void)
{
printf("ash> ");
Expand Down Expand Up @@ -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]);

Expand Down Expand Up @@ -121,6 +135,8 @@ void historyCommand(char *commandType, char **historyOfUser)
splitToArgs(fullCommandWanted, historyArgs);

executeAndWait(historyArgs);

free(historyArgs);
}


Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b1c199c

Please sign in to comment.