To confine your debugging efforts to the new code you write in this workshop, please start with the complete working version of the smartshell code provided by the instructor.
smartshell2.c
and name it smartshell3.c
history
-- display a numbered list of all previous commands
run #
-- run the command numbered # from the history list
history.txt
in your smartshell
directory (use fopen and
fclose).
Before the smartshell starts processing user commands, read the history of commands from history.txt
,
so they are available to the current shell instance. This way the history of shell commands is
preserved between different shell executions.
You have developed the skills needed to complete this task in implementing the SaveSearchPaths
function.
Here you will need to build a datastructure, say
char * history[MAX_HISTORY];where you keep inserting shell commands as the user issues them at your shell prompt. Initialize this data structure with InitArray, and update it using the Insert function from the
stringarray
library.
Then the history
command reduces to simply calling PrintAll.
The run
command reduces to calling the mysystem
function.
In implementing the last piece (item 3 above) of the shell, write two functions with the following prototypes:
void ReadHistory(char * filename); /* Open (fopen) the file with the name passed as argument (this is the file containing the history of commands typed into the shell). Read each line from the file (use fgets) and save it in the history array (use Insert). Close (fclose) the file. */ void SaveHistory(char * filename); /* Open the file with the name passed as argument. Write each string from the history array into the file (use fprintf), one per line. Close the file. */
Compile your code using the command
gcc smartshell3.c mysystem.c stringarray.c -o smartshell3The three source files will be linked into one executable file called smartshell3.