CS-550 Section 01 Spring 2020 Assignment 01 : I/O Redirection and Processes in Shell

Goals

This assignment teaches about managing processes, inter-process communication using pipelines and I/O redirection, and how a UNIX shell works.

[Description] [Restrictions] [Hints] Grading Guidelines

Description

Write a program in C called mysh which acts like a shell. Your shell should contain a loop that:

  1. Writes a prompt to standard output of the form mysh>
  2. Reads a command from standard input, terminated by a newline (enter on the keyboard)
  3. Execute the command it just read.

Since these commands may not function cleanly, mysh must run each command in its own process, and wait for that process to end.

The loop should be terminated when end-of-file is reached on standard input, or when the single word "exit" is typed on the command line. (You can type an "end-of-file" character to stdin using Ctrl-D.)

For instance, here is an example of the output from mysh:

  ~>mysh
  mysh> cat input.txt
  This is text inside input.txt
  It may go on for several lines
  mysh>

The commands themselves consist of any valid UNIX command, including parameters to that command. These commands must also support any valid combination of the following special features:

  1. Input Redirection - If the command is followed by a less than sign (<) and a path and/or file name, such as < input.txt, then the file input.txt should become the command's standard input stream. For example:
        mysh> grep text < input.txt
        This is text inside input.txt
        mysh>
      
  2. Output Redirection - If the command is followed by a greather than sign (>) and a path and/or file name, such as > output.txt, then the command's output stream should be redirected to the file output.txt. For example:
        mysh> grep text input.txt > output.txt
        mysh>
      

    After this command, output.txt will contain "This is text inside input.txt".

  3. Pipes - If a command is followed by a vertical bar (|), and a second command, then the standard output stream of the first command should be connected to the standard input stream of the second command. mysh must be able to support an arbitrary number of pipes. For example:
        mysh> cat input.txt | grep text | wc -w
        5
        mysh>
      

Any valid combination of input redirection, output redirection, or pipelining should be supported. Not all combinations are valid. For instance, a command on the left side of a pipe cannot use output redirection. mysh should detect these invalid combinations, and write error messages to indicate that there is a problem.

Restrictions

Hints

Grading Guidelines