CS 332: Operating Systems
A little command shell
Due 11:10 AM Wednesday, 4/28. Hand in via the Courses system.
Write a small command shell. In particular, your shell should:
- Print a prompt.
- Get a command line from the user. The command line may include
redirections (<, >), pipes (|), and possibly an ampersand (&)
at the end of the line.
- Search the $PATH environment variable for a program
whose name is the first word in the command string, and fork a child process
to execute the first such program found, passing the command-line arguments
(including the program name itself) to the program (or
printing an error message if no command is found). (If the command string
includes <, >, or |, the execution of the command will be
more complex than this, but analogous.)
- wait for the execution to finish (unless the command was
terminated with an &, in which case you should go to step 5 immediately).
- Go to step 1.
Your shell should also trap Ctrl-C's. Upon receiving a SIGINT,
it should tell the user to
type Ctrl-] followed by <return> to quit your shell.
You may work with a partner or alone, as you wish.
Here are a few technical points that may interest you:
- Typing Ctrl-] sends a character with ASCII value 29.
This character, known as "GS" or "Group Separator," goes
along with "FS" (28, Ctrl-\, "File Separator"),
"RS" (30, Ctrl-^, "Record Separator"),
and "US" (31, Ctrl-_, "Unit Separator"). The ASCII standard
says "These information separators may be used with data
in optional fashion, except that their hierarchical relationship
shall be: FS as the most inclusive, then GS, then RS, and US
as least inclusive [is this a political statement?]. The
content and length of a file, group, record, or unit are not
specified." So in other words, nobody uses these characters
anymore, except when they need a character that nobody uses anymore.
- "man execv" will get you a started using exec. The "v" in
"execv" stands for "vector," which means that when you call
execv(), you give it a pre-allocated argv of command-line argument strings.
The "p" in "execvp" stands for "path," which means that "execvp"
looks for the file-to-be-executed in all the directories specified
in your PATH environment variable. To see what your PATH is,
type "echo $PATH" at the Unix prompt. If you want to look all
over the place for the file-to-be-executed, use execvp(). If you
only want to look in the current directory, use execv().
Important constraints
I would appreciate your assistance in making this
program logistically easy to grade. Following these rules will be
worth one point.
- Write your program in a single source file named shell.c or shell.cpp (all
lower case).
- If you need to submit a revised version of your program, call
it shell1.c. A revision of the revision should be called
shell2.c, etc.
- Don't submit a folder for this assignment--just shell.c. If
you have "readme" information to communicate to me, just put
it in the comment at the top of shell.c.