Computer science and IT assignments Undergraduate

Shells, Piping, Redirection (C)

In this assignment you shall take an existing implementation of a shell program and add some small improvements to it.

Preparation – Downloading, Compiling and Testing a simple Shell Implementation
Download and un-compress the following file:

shell.tgz
The contents of the file are C sources that implement a simple shell. The contents
are:

smsh.c – an implementation of a very simple shell program
smsh.h – a header file with function prototypes
execute.c – a set of helper functions for running processes within the program
splitline.c – some text processing utilities
To compile these sources run:

gcc -o smsh1 smsh1.c splitline.c execute.c
To run the shell type:

./smsh1
A prompt of the form “>” will appear and then you can type commands like:

> ls
execute.c shell.tgz smsh.h smsh1 smsh1.c splitline.c
> wc execute.c
37 113 725 execute.c
The shell is terminated by typing the Control-D key (which signals end of input).
In this assignment you shall add functionality to this shell command. Each part builds
upon the last.

Part 1 – Adding the ability to pipe commands to your shell
(30 marks)
At the moment smsh1 doesn’t support piping of commands. So, for example, if you
type:

ls | wc
at the prompt you get:

ls: wc: No such file or directory
ls: |: No such file or directory
Write a new program called smsh2.c that is based on smsh1.c which performs all of
the shell operations of smsh1.c but also allows commands to be piped as above so
that if you type:

ls | wc

You get output like:

6 6 53

instead. You are free to add and modify files as required to accomplish this task. You
must add a Makefile to your submission so that you can compile all the files for part1
by typing:

make part1
and the solution for part 1 can be run by typing:

./smsh2
Note, your program must still cater for all the behaviours that were correct in the
original version of smsh1. You may find the lectures on piping useful in completing
this part.

Part 2 – Redirecton of stdin and stdout (30 Marks)
Your version of smsh2 currently doesn’t support redirection of output and input of
commands such as:

ls > tmp.txt
cat < tmp.txt | wc > out.txt
Copy your smsh2.c program from part 1 to smsh3.c so that it can handle redirection
of standard input and standard output (don’t worry about stderr) using the “>” and “<“
symbols. Again, you are free to add and modify files as required to accomplish this
task (without affecting the ability of your code to correctly execute the behaviour
required for part 1). You must add a Makefile to your submission so that you can
compile all the files for part 2 by typing:

make part2
and the solution for part 2 can be run by typing:

./smsh3
Note, your program must still cater for all the behaviours that were correct in the
original version of smsh2.

Part 3 – Adding Globbing – (20 marks)
Your version of smsh3 currently doesn’t support wildcards in command lines such
as:

ls *.c
cat *.h

Copy your smsh3.c program from part 2 to smsh4.c so that it can handle wilcard symbols in the command line. This expansion of wildcards is called globbing.

Note, you can use the glob system call (type “man -s3 glob” to find out more) that helps you to expand the wildcards to a list of actual filenames.

Again, you are free to add and modify files as required to accomplish this task (without affecting the ability of your code to correctly execute the behaviour required for previous parts). You must add a Makefile to your submission so that you can compile all the files for part3 by typing:

make part3
and the solution for part 3 can be run by typing:

./smsh4
Note, your program must still cater for all the behaviours that were correct in the original version of smsh3.