Smart Shell, Part I

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 improve upon some of the basic functionality of the Unix or Linux shell. In this first part, you will craft the basic mechanism for handling user input along with a few basic and necessary features. 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. You may work alone or with one other classmates, and each person is expected to contribute equally to the effort

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:

    /bin:/usr/bin:/opt/gnu/bin:/usr/gnu/bin
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".

Important 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.

Project Specifications

In your csc1600 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.
For the last step, learn about the function getenv using the provided link or other web resources. You need to use this function 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.

One specific implementation requirement is to save the paths in an array of strings before printing them out. This will come in handy when we will build more shell functionality in future assignments. Incorporate in your shell a function

    void SaveSearchPaths();
that creates an array of strings, with each string being one path from the PATH environment variable. For example, if the $PATH variable has the value shown above, then this function allocates memory for a global variable searchpaths as shown below:

    

To implement the SaveSearchPaths function, use the functions InitArray and Insert from the previous assignment. Then step 4 of this assignment reduces to simply calling PrintAll. Do not copy these functions into your smartshell1.c file. Just include the line

    #include "stringarray.h"
at the beginning of your smartshell1.c code, and compile it using the command

    gcc smartshell1.c stringarray.c -o smartshell1
The two source files will be linked into one executable file called smartshell1.

Submission Instructions

In your smartshell directory create a text file named readme (not readme.txt, or README, or Readme, etc.) that contains:
  1. Your name(s) and assignment number.
  2. A description of whatever help (if any) you received from others outside of class.
  3. An indication of how much time you spent doing the assignment outside of class.
  4. Your assessment of the assignment: Did it help you to learn? What did it help you to learn? Do you have any suggestions for improvement? Etc.
  5. Any information that will help us to grade your work in the most favorable light. In particular you should describe all known bugs.
Descriptions of your code should not be in the readme file. Instead they should be integrated into your code as comments. Your readme file should be a plain text file. Do not create your readme file using Microsoft Word or any other word processor.

Turn in a printout copy of the readme file, and each of the three files (one printout per group). You will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.

Leave the source code for all exercises in your directory csc1600/smartshell. Do not make any changes to these files after the due date for this assignment.

Grading

50     Total points possible
 
10 Program compiles successfully and runs
15 Each command does something resembling the specified action
15 Each command works correctly and completely
  5 You have added some extra "bells and whistles" (use your imagination)
  5 Your readme file is present

Have fun!