Skip to content

Commit

Permalink
Added history command
Browse files Browse the repository at this point in the history
  • Loading branch information
Biscuitmunch committed Mar 24, 2022
1 parent ab0e026 commit cf2cdd1
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions main.c → ash.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,34 @@ void executeAndWait(char **userArgs)
}
}

void historyCommand(char *userString)
void historyCommand(char *commandType, char **historyOfUser)
{

for (int i = 1; i <= 10; i++)
{
printf("%d: %s\n", i, historyOfUser[i-1]);

if (historyOfUser[i]==NULL)
{
break;
}
}

}

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;

}

int main()
Expand All @@ -81,6 +106,9 @@ int main()
char *OGdirectory = malloc(1000);
getcwd(OGdirectory, 1000);

// History variable
char **historyCommands = malloc(1000);

while (strcmp(fullCommand, "exit") != 0)
{
// Resetting the users old line
Expand All @@ -89,6 +117,9 @@ int main()
// Taking the users full line
fullCommand = readLine();

// Adding this to command history
addToHistory(fullCommand, historyCommands);

// Resetting an args 2d array
char **userArgs = malloc(1000);
// Filling the array with users args
Expand All @@ -108,16 +139,19 @@ int main()
}

// If command is history
if (strcmp(userArgs[0], "history") == 0 || strcmp(userArgs[0], "h") == 0)
else if (strcmp(userArgs[0], "history") == 0 || strcmp(userArgs[0], "h") == 0)
{
historyCommand(userArgs[1]);
historyCommand(userArgs[1], historyCommands);
}

// Run built-in normal commands
else
{
executeAndWait(userArgs);
}

free(userArgs);

}

return 0;
Expand Down

0 comments on commit cf2cdd1

Please sign in to comment.