Projects‎ > ‎


Implementing a Shell and ls

Due Tuesday, October 4th at 11:59pm. Turn into your Project02 git repo.

For this project you are going to develop a mini UNIX shell called usfsh and a mini version of ls called usfls. In order to implement these applications you will need to understand how to use several UNIX systems calls and how to manipulate kernel provided abstractions such as the file descriptor table.

Note your implementation of usfsh and usfls must both run on your RPi in Raspbian. For interactive grading you must demo on your RPi. You should develop entirely on your RPi. Some of the system calls are not identical on Mac OS X and Linux, so if you develop on your Mac, you solution may not compile and run on your RPi. The moral here is to figure out a way to edit and compile directly on your RPi.

The Shell: usfsh

Your shell should print the “$” prompt and wait for user input. For example:


You should be able to execute commands that are in the current directory using the “./” notation:

$ ./usfls

You should also be able to execute an command available in the PATH environment variable. For example:

$ ls

If the user types in a command that is not on the PATH, then you should print an error message:

$ foobar
foobar: command not found

You should be able to pass arguments to commands:

$ echo hi there

You need to support file output redirection:

$ echo hi there > hi.txt

This will create or overwrite a file called hi.txt and put the output of the first command into the file.

You need to support pipe redirection:

$ echo world hello | wc

      1       2      12

In both file redirection and pipe redirection you need to support multiple arguments.

You need to support a built-in “cd” command to change directories:

$ cd foo

$ ls

$ cd ..

(Hint: use the chdir system call)

You also need to support a built-in "exit" command that will exit usfsh.

List files: usfls

You need to implement your own version of ls called usfls. Your implementation needs to use the readdir system calls to list the contents of the current directory. You can list the files and directories on separate lines:

$ ./usfls



Your usfls should not list files that start with a dot “.”.

General Requirements

  • You must only use system calls for I/O, with the exception of printf().
  • You can use strtok() for parsing the command line.

Extra Credit

Each worth one point.


  • [sh-1] Support file input redirection with "<" (e.g., $ sort < file.txt)

  • [sh-2] Support chaining multiple commands via pipes (e.g., $ ls | sort -r | head -n 3)

  • [sh-3] Support mixing pipe redirection and file redirection (e.g., $ ls | sort -r > out.txt)

  • [sh-4] Support command history (history)

  • [sh-5] Support <TAB> command completion (this is hard).

  • [sh-6] Support showing the current directory in the prompt (e.g. [/home/benson]$ )

  • [sh-7] Support background processing with & (e.g., $ some_command &)

  • [sh-8] cd by itself brings you to the $HOME directory

  • [sh-9] Support for the && shell operator (e.g., $ c1 && c2) Note you must check that c1 succeeded in order to execute c2.

  • [sh-10] Support for the || shell operator

  • [sh-11] Add support for showing the duration (in seconds) of each command in the history list (e.g., 1 [0.02 seconds] ls)


  • [ls-1] Sort output similar to real ls

  • [ls-2] ls -a (all files, include ones that start with a dot “.”)

  • [ls-3] ls -l (long output)

  • [ls-4] Support simple regular expressions as filters (e.g., ls *.c)

  • [ls-5] Support an argument that can be a relative or absolute path (e.g., ls ../foo or ls /usr/bin)