History implementation in a simple shell program in c -
i'm new , learning c on own. managed write simple shell code in c problem have commands entered stored when entering command "history" entered commands displayed on screen. sample code or materials read me have shell have history appreciated.
there many ways achieve this. use gnu readline library, nice sort of thing. provide more simple history
command.
but implementing simple history easier. if have fixed limit commands in history simple array sufficient, maybe this:
static const char *history[history_max_size]; static const unsigned history_count = 0; void add_command_to_history( const char *command ) { if (history_count < history_max_size) { history[history_count++] = strdup( command ); } else { free( history[0] ); (unsigned index = 1; index < history_max_size; index++) { history[index - 1] = history[index]; } history[history_max_size - 1] = strdup( command ); } }
Comments
Post a Comment