Question
I am trying to write a C program that creates new shell and then saves the commands from the shell into an array. Here is
I am trying to write a C program that creates new shell and then saves the commands from the shell into an array. Here is the basic program where it takes the commands and output according to the commands. Assignment is an extension to the UNIX Shell interface. You will add a history feature into your UNIX Shell which will allow users to access up to ten most recently entered commands. These commands will be numbered starting at 1 (the least recent of the last 10 commands) and will continue to grow to 10 (the most recent of the last 10 commands). If the user executes the same command twice consecutively, it will appear twice in the history. The history commands will also be saved to a file when the shell is exited and input from the file the next time the shell is started (for Part B, below). Here is an extension of Lab 1, I am attaching the lab1 here. I dont know How to save the 10 commands and save it to an array to access it for later.
Part A:
A user will be able to list the ten most recently entered commands when s/he types the command historyor its alias/shortcut h . With this list of previous commands, the user can run any of those previous 10 commands by entering r num where num is the number of that command in the history list (and numwill always be from 1 to 10, inclusive,not 0 to 9). Also, the user
should be able to run the most recent command again by entering rr for run most recent. You can assume that one space will separate the r and the number and that the number will be followed by . Also, when rr is used to execute the most recent command you may assume that it is immediately followed by . To simplify the code, we will assume that the user can only enter either r num or rr
immediately after entering history or h, and you can
assume that a user of your shell will comply with this restriction.
#include
#include
#include
#define MAXLINE 50 /* 80 chars per line, per command, should be enough. */
/** The setup() routine reads in the next command line string storing it in the input buffer.
The line is separated into distinct tokens using whitespace as delimiters. Setup also
modifies the args parameter so that it holds points to the null-terminated strings which
are the tokens in the most recent user command line as well as a NULL pointer, indicating the
end of the argument list, which comes after the string pointers that have been assigned to
args. ***/
char array[510];
char *var = array;
void setup(char inputBuff[], char *args[],int *background)
{
int length, /* Num characters in the command line */
i, /* Index for inputBuff arrray */
j, /* Where to place the next parameter into args[] */
start; /* Beginning of next command parameter */
/* Read what the user enters */
length = read(STDIN_FILENO, inputBuff, MAXLINE);
j = 0;
start = -1;
if (length == 0)
exit(0); /* Cntrl-d was entered, end of user command stream */
if (length < 0){
perror("error reading command");
exit(-1); /* Terminate with error code of -1 */
}
/* Examine every character in the input buffer */
for (i = 0; i < length; i++) {
switch (inputBuff[i]){
case ' ':
case '\t' : /* Argument separators */
if(start != -1){
args[j] = &inputBuff[start]; /* Set up pointer */
j++;
}
inputBuff[i] = '\0'; /* Add a null char; make a C string */
start = -1;
break;
case ' ': /* Final char examined */
if (start != -1){
args[j] = &inputBuff[start];
j++;
}
inputBuff[i] = '\0';
args[j] = NULL; /* No more arguments to this command */
break;
case '&':
*background = 1;
inputBuff[i] = '\0';
break;
default : /* Some other character */
if (start == -1)
start = i;
}
}
args[j] = NULL; /* Just in case the input line was > 80 */
}
int main(void)
{
char inputBuff[MAXLINE]; /* Input buffer to hold the command entered */
char *args[MAXLINE/2+1];/* Command line arguments */
int background; /* Equals 1 if a command is followed by '&', else 0 */
int i = 0;
int j = 0;
while (1){ /* Program terminates normally inside setup */
background = 0;
pid_t pid;
printf("CSE2431Sh$ "); /* Shell prompt */
fflush(0);
setup(inputBuff, args, &background); /* Get next command */
for(i=0; i inputBuff[i] = args[i]; } pid = fork(); if (pid == 0) execvp(args[0], args); else if (background == 0) { wait(pid); } sleep(1); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started