Transcribed TextTranscribed Text

Programming Assignment: Shell Description In this assignment you will write your own shell program, Mav shell (msh), similar to bourne shell (bash), c-shell (csh), or korn shell (ksh). It will accept commands, fork a child process and execute those commands. The shell, like csh or bash, will run and accept commands until the user exits the shell. Your file must be named msh.c Functional Requirements Requirement 1: Your program will print out a prompt of msh> when it is ready to accept input. It must read a line of input and, if the command given is a supported shell command, it shall execute the command and display the output of the command. Requirement 2: If the command is not supported your shell shall print the invalid command followed by “: Command not found.” Requirement 3: If the command option is an invalid option then your shell shall print the command followed by “: invalid option --” and the option that was invalid as well as a prompt to try —help. exec() outputs this automatically make sure you pass it on to your user. Requirement 4: After each command completes, your program shall print the msh> prompt and accept another line of input. Requirement 5: Your shell will exit with status zero if the command is “quit” or “exit”. Requirement 6: If the user types a blank line, your shell will, quietly and with no other output, print another prompt and accept a new line of input. Requirement 7: Your shell shall support suspending the process with ctrl-z Requirement 8: Your shell shall background a suspended process with bg Requirement 9: Your version of Mav shell shall support up to 10 command line parameters in addition to the command. Requirement 10: Your shell shall support and execute any command entered. Any command in /bin, /usr/bin/, /usr/local/bin/ and the current working directory is to be considered valid for testing. Your shell shall search in the following PATH order: 1. Current working directory, 2. /usr/local/bin 3. /usr/bin 4. /bin Parameters may also be combined. For example, ps may be executed as: ps –aef or ps –a –e -f Requirement 11: Mav shell shall be implemented using fork(), wait() and one of the exec family of functions. Your Mav shell shall not use system(). Use of system() will result in a grade of 0. Requirement 12: Typing ctrl-c or ctrl-z shall not stop or kill your shell. ctrl-z shall suspend the currently running process. ctrl-c shall stop the currently running process. Requirement 13: Your shell shall support the cd command to change directories. Your shell must handle cd .. Requirement 14: Your shell shall support the listpids command to list the PIDs of the last 15 processes spawned by your shell. If there have been less than 15 processes spawned then it shall print only those process PIDs Requirement 15: Your shell shall support the history command which will list the last 50 commands entered by the user. Typing !n, where n is a number between 1 and 15 will result in your shell re-running the nth command. If the nth command does not exist then your shell will state “Command not in history.”. The output shall be a list of numbers 1 through n and their commands, each on a separate line, single spaced. If there are less than 50 commands in the history only list the commands the user has entered up to that point. Requirement 16: A list of commands separated by semicolons should be executed in sequence. msh> echo foo; echo bar; echo baz foo bar baz Requirement 17: Your source file shall be named msh.c. The source files must be ASCII text files. No binary submissions will be accepted. Requirement 18: Tabs or spaces shall be used to indent the code. Your code must use one or the other. All indentation must be consistent. Requirement 19: No line of code shall exceed 100 characters. Requirement 20: Each source code file shall have the following header filled out: Requirement 21: All code must be well commented. This means descriptive comments that tell the intent of the code, not just what the code is executing. The following are poor comments. The following explains the intent: When in doubt over comment your code. Requirement 22: Keep your curly brace placement consistent. If you place curly braces on a new line , always place curly braces on a new end. Don’t mix end line brace placement with new line brace placement. Requirement 23: Each function should have a header that describes its name, any parameters expected, any return values, as well as a description of what the function does. For example Requirement 24: Remove all extraneous debug output before submission. The only output shall be the output of the commands entered or the shell prompt.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#define _GNU_SOURCE

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>

#define WHITESPACE " \t\n"      // We want to split our command line up into tokens
// so we need to define what delimits our tokens.
// In this case white space
// will separate the tokens on our command line

#define MAX_COMMAND_SIZE 255    // The maximum command-line size

#define MAX_NUM_ARGUMENTS 10    // Mav shell only supports five arguments

#define MAX_PID_COUNT 15         // The maximum child process pid history size

#define MAX_HISTORY_COUNT 50    // The maximum command history to store

#define MAX_BATCH_COMMANDS 10    // The maximum batch commands to run one by one

#define BATCH_SEP ";"            // Batch commands separator delimiter

// execute is the method which will take one
// command at a time and execute it.
// Declaring prototype of the method.
int execute(char *, char *[], unsigned int *);

// This will hold the process id of last executed child.
pid_t child_pid;

// This method keeps count of history commands.
int count = 0;

// This flag keeps track if process is suspended or not.
int is_suspended = 0;

// This flag keeps track if process is running or not.
int is_running = 0;

// This method handles the ctrl+z signal.
void ctrlzHandler(int sig)
signal(SIGTSTP, ctrlzHandler);
//kill(child_pid, SIGSTOP);
if(is_suspended == 0)
      is_suspended = 1;
else if(is_suspended == 1)
      is_suspended = 0;


// This method handles the ctrl+c signal.
void ctrlcHandler(int sig)
//signal(SIGKILL, ctrlzHandler);
if(is_suspended == 1)
      is_suspended = 0;
      //signal(SIGINT, ctrlcHandler);
      kill(child_pid, SIGKILL);

// This method adds the current command
// to the history array. If history array is
// full it will shift previous history commands
// by one and adds the new command at the end of the array.
void addToHistory(char *hist[], char *command, int count)
if (count >= MAX_HISTORY_COUNT)
    int i = 0;
    for (i = 0; i < MAX_HISTORY_COUNT - 1; i++)
      hist[i] = strdup(hist[i + 1]);
    hist[i] = strdup(command);
    hist[count] = strdup(command);

// This method removes the trailing new line
// from the string.
char* removeTrailingNewLine(char *str)
int len = strlen(str);
int i = len - 1;
int count = 0;
while (str != NULL && str[i] == '\n')

i = 0;
while (count > 0)
    str[len - i - 1] = '\0';

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Operating Systems Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats