Os Package - The Shell
Os Package - The Shell
The shell will give a prompt for the user to type in a command, take the command,
execute it, and then give the prompt back for the next command (i.e., actually give the
functionality of a shell). Your program for the assignment should do the following:
o Give a prompt (like the $ prompt you get in Linux) for the user to type in a
command. The prompt should have the current working directory name (full path)
followed by the “>” sign (for ex., /usr/home/agupta/temp> ).
The commands are the same as the corresponding Linux commands by the same
name. Do "man" to see the descriptions. You can use the standard C library
functions chdir, getcwd, mkdir, rmdir, readdir, stat etc. to implement the calls.
All calls should handle errors properly, with an informative error message. For
example, cp will fail if the user calling it does not have read permission on
“file1”; the call should print a proper error message. Look up the perror call.
These commands are called builtin commands since your shell program will have
a function corresponding to each of these commands to execute them; no new
process will be created to execute them. (Note that all these commands are not
builtin commands in the bash shell, but we will make them so in our shell).
o Any other command typed at the prompt should be executed as if it is the name of
an executable file. For example, typing "a.out" should execute the file a.out. The
file can be in the current directory or in any of the directories specified by the
PATH environment variable (use getenv to get the value of PATH). The file
should be executed after creating a new process and then exec'ing the file onto it.
The parent process should wait for the file to finish execution and then go on to
read the next command from the user. The command typed can have any number
of command line arguments.
o Should be able to redirect the output of a program to a file using ">" and read the
input of a program from a file using "<". For example, typing "a.out > outfile"
should send whatever was supposed to be displayed on the screen by a.out to the
file outfile . Similarly, typing "a.out < infile" should make a.out take the inputs
from the file infile instead of the keyboard.
o Should be able to redirect the output of one command to the input of another by
using the "|" symbol. For example, if there is a program a.out that writes a string
"abcde" to the display, and there is a program b.out that takes as input a string
typed from the keyboard, counts the number of characters in the string, an
displays it, then typing "a.out | b.out" at your shell prompt should display 5 (the
output "abcde" from a.out was fed as input to b.out, and 5, the number of
characters in "abcde", is printed). Use the pipe command. Any number of
redirections should be allowed (for ex., a | b | c, a| b | c| d |e,…).
To run your shell, write another C program that will create a child process and call an
appropriate form of exec to run the program above from the linux shell. The parent
process simply waits for the child to finish (execute the "exit" command), after which it
also exits.