1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include "parser.h" //header file that implements shell features
#include "shell.h" //header file that implements shell features
#include <string.h>
#include <stdio.h>
const char* HISFILE = "simpleshell_history"; //name of history file
int main(void) {
char input[MAXINPUTLINE] = "\0";
char* command = NULL;
FILE* history = NULL;
fpos_t pos; //this is the struct returned by fgetpos(). I am trying to use it to save the file state so I can do the history read, and then let it write again from the same position
signal_c_init();
history = fopen(HISFILE, "ab+"); //is this the write input mode?
//some screen output stuff
printf("Welcome to the sample shell! You may enter commands here, one\n");
printf("per line. When you're finished, press Ctrl+D on a line by\n");
printf("itself. I understand basic commands and arguments separated by\n");
printf("spaces, redirection with < and >, up to two commands joined\n");
printf("by a pipe, tilde expansion, and background commands with &.\n\n");
printf("\nktsh$ ");
while (fgets(input, sizeof(input), stdin))//input is the command from keyboard
{
fputs(input, history); //trying to write this to a file exactly, with the new line
if(strncmp(input, "history\n", sizeof("history\n")) == 0)//compares input to "history" using strncmp.
{
fgetpos(history, &pos); //this returns a file pointer structure. Using this to save the position of the file when it was writing.
rewind(history); //resets file pointer to beginning of the file
while (feof(history) == 0) //checks for end of file
{
int lineCount = 1;
fgets(command, sizeof(input), history);
printf("%d",lineCount);
printf(" - %s",command); //some output. Trying to get "1 - (command)
lineCount++;
}
fsetpos(history, &pos); //trying to return file pointer to initial position
}
stripcrlf
parse(input);
printf("\nktsh$ ");
}
fclose(history);
return 0;
}
| |