Question: This assignment is referring to using a Linux Virtual Machine terminal and writing in C. Instructions: Write a command line shell ezshell: Follow the template

This assignment is referring to using a Linux Virtual Machine terminal and writing in C.

Instructions: Write a command line shell ezshell: Follow the template skeleton code in ezshell.c, and implement the three functions in shfunctions.c file, both described below:

ezshell.c : Skeleton code for shell /***************************** * * * FILE ezshell.c * * * ******************************/ #include  #include  #include  #include  #include  /* The three functions declared here must be implemented in shfunctions.c */ void type_prompt(void); int tokenize_line(char *linebuf, int nr, char *argp[]); int do_cd_command(char *argp[], int nargs); /* Global variable for the home directory of this shell */ char *homedir = "/tmp"; int main(void) { char linebuf[BUFLEN]; /* Read buffer to hold the line typed by user */ char *argp[NMAX]; /* Pointers to null-terminated token strings */ /*********************************************************************** * You must #define reasonable values for BUFLEN and NMAX beforehand. * * Taking NMAX == BUFLEN/2 suffices (why?). * ************************************************************************/ int nr, nargs, status, forkpid; while (1) { type_prompt(); /* Print the prompt string */ /******************************************************************* * Get a input line (at most BUFLEN chars) via the read syscall, * * saving the user-typed string in linebuf[] (must end in ' '). * ********************************************************************/ nr = read(STDIN_FILENO, linebuf, BUFLEN); /* Got nr chars */ if (nr == 0) { /* EOF from user, end our shell gracefully */ return 0; } if (nr < 0) { /* Handle error: Print an error message and return non-zero */ } /*************************************************************** * If input string doesn't end with ' ', treat as an error. * * We are not handling long lines (yet). * ****************************************************************/ if ( /* Last char read is not ' ' */ ) { /* Print informative error message and return non-zero */ } /******************************************************************* * The function tokenize_line() parses the input-line string held * * in linebuf into whitespace-separated tokens, and returns the * * the number of tokens found. It populates the argp[] array as * * pointers to the individual token strings (null-terminated) and * * adds a NULL pointer to terminate the array, as in this figure. * * Makes argp[] a suitable second parameter for an execvp() call. * ********************************************************************/ nargs = tokenize_line(linebuf, nr, argp); if (nargs == 0) { continue; /* Line typed by user was blank */ } /***************************************************************** * Handle special internal command: cd [dir] * *****************************************************************/ if (strncmp("cd", argp[0], 3) == 0) { /* Why did we use 3? */ do_cd_command(argp, nargs); continue; } /***************************************************************** * Handle special internal command: exit * *****************************************************************/ if ( /* user types "exit" */ ) { /* End the shell gracefully */ } /* Now fork-exec the command with appropriate arguments */ forkpid = fork(); if (forkpid == 0) { /* Child; exec the command using execvp(3) */ execvp(argp[0], argp); /* Won't return on success */ /* But on error, we (child) must exit to avoid fork loop! */ perror("execvp()"); return 3; } else { /* Parent; wait for child to terminate and get its exit code */ wait(&status); /* Print last command's termination status code */ } } } 

shfunctions.c : Functions for the shell

/***************************** * * * FILE shfunctions.c * * * ******************************/ #include  #include  #include  #include  extern char *homedir; /***************************************************************** * * * Print the prompt string --- must end with a space character. * * Preferably it should display the current working directory, * * embedded as a substring within the prompt. * * * * Requires: Nothing. * * Modifies: Nothing. * * Effects: Prints prompt string on standard output. * * Returns: Nothing (void). * * * ******************************************************************/ void type_prompt(void) { /* Write your code here */ } /********************************************************************* * * * Parse the input line into whitespace-separated tokens, terminate * * each token with a null char, find the number of tokens (nargs), * * set pointers in the argp[] array so that the first narg pointers * * argp[0], ..., argp[nargs - 1] point to the null-terminated token * * strings and argp[nargs] is the null pointer, as in this figure. * * Return the number of tokens (nargs) * * * * Requires: linubuf[] must be a ' '-terminated char string (not * * null-terminated!). * * nr must specify the length of that string, including * * the terminating ' ' in the count. * * argp[] array size (number of entries in the array) * * must be larger than the number of tokens. * * * * Modifies: linebuf[] (tokens in it become null-terminated and so * * whitespaces in linebuf[] become null characters). * * argp[] (entries of this array get populated with * * pointers to the null-terminated tokens) * * * * Effects: Nothing. * * * * Returns: The number of tokens found (int). * * * **********************************************************************/ int tokenize_line(char *linebuf, int nr, char *argp[]) { /* Write your code here */ } /******************************************************************* * * * Handle the special command cd [dir]: User has typed "cd" and * * zero or more tokens, so change the current working directory * * of this shell process to the specified directory. * * If the user types "cd" by itself, change to homedir. * * * * Requires: argp[] must be an array of pointers to chars. * * nargs must be a positive int. * * argp[0] must point to start of a string "cd". * * argp[nargs] must be the null pointer. * * * * Modifies: Nothing. * * * * Effects: Changes the working directory of the shell. * * Can print error message in case of error/failure. * * * * Returns: 0 on success, -1 on failure or error. * * * ********************************************************************/ int do_cd_command(char *argp[], int nargs) { /* Write your code here */ }

Requirements: Your shell's behavior must match the output shown in the sample session below. Below is a sample of running an implemented version of ezshell. Note how:

  • Extra whitespace get ignored;
  • The cd command is implemented;
  • The current working directory is shown embedded in the prompt string.
  • Multi-token commands work, but command pipelines don't work.

Sample session from an implmented ezshell:

bash $ ./ezshell ezshell [/home/mint] > date Fri Mar 1 02:25:05 UTC 2019 ezshell [/home/mint] > pwd /home/mint ezshell [/home/mint] > cd ezshell [/tmp] > pwd /tmp ezshell [/tmp] > pwd /tmp ezshell [/home/mint] > cd /foobar chdir() failed: No such file or directory ezshell [/home/mint] > cd /home/mint/code/shells ezshell [/home/mint/code/shells] > pwd /home/mint/code/shells ezshell [/home/mint/code/shells] > cd .. ezshell [/home/mint/code] > cd .. ezshell [/home/mint] > pwd /home/mint ezshell [/home/mint] > cd ezshell [/tmp] > ezshell [/tmp] > tr a-z A-Z Hello 123 HELLO 123 ^D ezshell [/tmp] > foofoofoo execvp(): No such file or directory ezshell [/tmp] > wc -l /etc/passwd 42 /etc/passwd ezshell [/tmp] > ls | wc ls: cannot access |: No such file or directory ls: cannot access wc: No such file or directory ezshell [/tmp] > grep -i -c tcp /etc/services 323 ezshell [/tmp] > exit bash $

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!