Lab 7 - Smart Shell

In this programming assignment, you will explore a number of "system calls" and other low level techniques for performing systems programming. You will create a "shell," which is a "command line interpreter" that will mimic (and optionally improve) some of the basic functionality of the Unix or Linux shell.

The project must be programmed in C, and may be created and run on the department Unix or Linux system, or in an install of Unix or Linux on your own machine.

The PATH environment

Shell is the term often used for the command line interpreter of an operating system (particularly for Unix systems). That is, the shell is a program that displays a prompt, reads and parses the user input command line, executes the command and then prompts for more user input when it has finished.

To be able to execute a command, the shell needs to find where the command is located first (in which folder or directory). For this, the shell uses an environment variable called $PATH that defines search paths for a command. The path environment is a list of directories, separated by colon ':'. To see the contents of your $PATH variable in Unix, type at the shell prompt:

    echo $PATH 
You should see something similar to this:

This information is used as follows. When you type in a command at the shell prompt, the shell looks for the executable file in the current directory first. If not found, the shell looks in the directories defined by the search paths in order: /bin first, then /usr/bin, and so on, until either the command is found or the search paths are exhausted. If the command is found, the shell executes it. If not found, the shell simply displays the message "Command not found".

Note: If the command name starts with the character '/' (root directory) or '.' (current directory), the search path is not used, because the path is explicitly mentioned.

Required Shell Features

In your csc8400 directory, create a new directory called smartshell and a C program file called smartshell.c. Using good program design practices, create one or more functions to implement the following:
  1. When the program starts running, it waits for user input
  2. Handle whatever the user typed each time the user hits the Enter key (use fgets to read user input)
  3. If the user types quit, exit the program
  4. If the user types path, print out a list of each of the values (or directories) of the current PATH environment variable, one per line. (Note that you may not use the command echo $PATH to accomplish this.)

    Use the function getenv to obtain a pointer to the string representing the $PATH variable. Copy this string into a local buffer, then use the function strtok to extract individual tokens from the string. The delimiter in this case is the colon character.

  5. For any other command, try executing it using the mysystem function implemented in class. If the command is a valid Unix command, it should produce the same output as if it were typed into the Unix shell.

Optional Shell Features

Up to 15 bonus points will be rewarded for incorporating the following commands into your smart shell (you may not use the function mysystem to implement this functionality):
  1. history -- display a numbered list of all previous commands
  2. run # -- run the command numbered # from the history list

One efficient way of doing this is to save each user command into an array of strings, and save this array in a file, say history.txt, just before your smartshell ends. (You will need to use fopen and fclose.) When you next invoke your smartshell executable, have it read the history of commands from history.txt before beginning to process user commands, so that so the history of commands is available to the current shell instance. The current shell instance may add more commands to the history (and save it before finishing execution). This way the history of shell commands is preserved between different shell executions.

Submission Instructions

Leave the source code in your directory csc8400/smartshell. If you work on your own computer, demonstrate your program and email the source code to your instructor.


100     Total points possible
30 Program compiles successfully and runs
30 Each command does something resembling the specified action
30 Each command works correctly and completely
10 You have added some extra "bells and whistles" (for example, add a "mem" command that prints out the amount of memory available -- use your imagination)

Have fun!