Os Project
Os Project
Project Report
Project Title: UNIX Shell and History
Feature
Description :
2.Executing a command without ‘&’: As shown in the below image parent process will wait for the
child process (gedit) to complete.
3.Executing a command with ‘&’: As shown in the below image parent process will not wait for the
child process (gedit) to complete.
4.Executing a invalid command
Output :
1.Executing history command to display previous 10 commands
2. Executing the recent command by using ‘!!’ in the terminal
3. Executing 8th recent command by using ‘!8’
6.Showing the error when 11th command is accessed (history will have only 10 commands
stored)
7. Showing the error when user access a command not in history.
1. We faced problem when trying to compare strings. The string compare can be easily done by using
strcmp() function but we have used ascii values for comparison. If strcmp() is used we have
compare the input with every case of !n like strcmp(str,”!1”), strcmp(str,”!2”), strcmp(str,”!3”) etc.
Instead we used ASCII value as described in below code for compact code and easy access of
command.
2. else if (args[0][0]-'!' ==0)
3. { int x = args[0][1]- '0';
4. int z = args[0][2]- '0';
5.
6. if(x>count) //second letter check
7. {
8. printf("\nNo Such Command in the history\n");
9. strcpy(inputBuffer,"Wrong command");
10. }
11. else if (z!=-48) //third letter check
12. {
13. printf("\nNo Such Command in the history. Enter <=!9 (buffer size is 10 along with
current command)\n");
14. strcpy(inputBuffer,"Wrong command");
15. }
16. else
17. {
18.
19. if(x==-15)//Checking for '!!',ascii value of '!' is 33.
20. { strcpy(inputBuffer,history[0]); // this will be your 10 th(last)
command
21. }
22. else if(x==0) //Checking for '!0'
23. { printf("Enter proper command");
24. strcpy(inputBuffer,"Wrong command");
25. }
26.
27. else if(x>=1) //Checking for '!n', n >=1
28. {
29. strcpy(inputBuffer,history[count-x]);
30.
31. }
32.
33. }
34. }
//declarations
char history[10][BUFFER_SIZE]; //history array to store history
commands
int count = 0;
int i;
int j = 0;
int histCount = count;
start = -1;
if (length == 0)
exit(0); //end of command
if (length < 0)
{
printf("Command not read\n");
exit(-1); //terminate
}
default :
if (start == -1)
start = i;
if (inputBuffer[i] == '&')
{
*flag = 1; //this flag is the differentiate
whether the child process is invoked in background
inputBuffer[i] = '\0';
}
}
}
if(strcmp(args[0],"history")==0)
{
if(count>0)
{
displayHistory();
}
else
{
printf("\nNo Commands in the history\n");
}
return -1;
}
}
}
for (i = 9;i>0; i--) //Moving the history elements one step higher
strcpy(history[i], history[i-1]);
int main(void)
{
char inputBuffer[MAX_LINE]; /* buffer to hold the input command */
int flag; // equals 1 if a command is followed by "&"
char *args[MAX_LINE/2 + 1];/* max arguments */
int should_run =1;
pid_t pid,tpid;
int i;
printf("Fork failed.\n");
exit (1);
}