Add sh(1) history builtin.

This commit is contained in:
Jonas 'Sortie' Termansen 2022-11-06 00:26:13 +01:00
parent 3c69791078
commit c6af3bc074
1 changed files with 14 additions and 0 deletions

14
sh/sh.c
View File

@ -58,6 +58,7 @@ static const char* builtin_commands[] =
"exit",
"unset",
"clearenv",
"history",
(const char*) NULL,
};
@ -1341,6 +1342,19 @@ struct execute_result execute(char** tokens,
}
}
if ( strcmp(argv[0], "history") == 0 )
{
for ( size_t i = 0; i < edit_state.history_used; i++ )
{
const char* line = edit_state.history[i];
if ( fprintf(stdout, "%5zu %s\n", i + 1, line) < 0 )
err(1, "stdout");
}
if ( fflush(stdout) == EOF )
err(1, "stdout");
exit(0);
}
execvp(argv[0], argv);
if ( interactive && errno == ENOENT )