A little command shell
Assigned 4/1/98, due Friday 4/10/98. Hand in using
HSP.
Write a command shell that
- prints a prompt,
- gets a command line from the user,
- searches the $PATH environment variable for a program
whose name is the first word
in the command string, and forks 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),
- waits for the execution to finish,
- prints out a line reporting the exit value of the
command being executed, and
- goes back to the first step.
Your shell should also trap CTL-C's--upon receiving a SIGINT,
it should tell the user to
type CTL-] followed by <return> to quit your shell.
Work in groups of 2 or 3. No solo programs, please.
Here are a few technical points that may interest you:
- Typing CTL-] sends a character with ASCII value 29.
This character, known as "GS" or "Group Separator," goes
along with "FS" (28, CTL-\, "File Separator"),
"RS" (30, CTL-^, "Record Separator"),
and "US" (31, CTL-_, "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 execl" will get you a start on using exec. The "l" in
"execl" stands for "list," which means that when you call
execl(), you give it a list of command-line argument strings.
The "p" in "execlp" stands for "path," which means that "execlp"
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 execlp(). If you
only want to look in the current directory, use execl().
You might also wish to consider using execv() or execvp().
Jeff Ondich,
Department of Mathematics and Computer Science,
Carleton College, Northfield, MN
55057,
(507) 646-4364,
jondich@carleton.edu