ICS412 - Fall 2009 - Programming Assignment #2 -

Work alone
You are expected to do your own work on all homework assignments. You may (and are encouraged to) engage in general discussions with your classmates regarding the assignments, but specific details of a solution, including the solution itself, must always be your own work. (See the statement of Academic Dishonesty on the course's syllabus.)

What to turn in?
Turn in an archive (.zip, .tar, .tar.gz, .tgz, etc.) that contains: The archive must be turned in via e-mail to henric@hawaii.edu and to altunkay@hawaii.edu with a subject line like "ICS412: PA#2" before 11:59PM on the day the assignment is due.


In this assignment you'll implement ics412sh, a very simple UNIX Shell. Each question makes ics412sh a little bit closer to a real shell.

Question #1 (20 pts): ics412sh Version 1
Implement a Shell, ics412sh_v1, that prints a prompt to the screen, accepts a command typed by the user, executes that command, and prompts the use again. The Shell provides the three following built-in commands:

The Shell also allows the user to run standard system programs (ls, cp, cat, sleep, etc.) When the user types a command, say, "ls", the Shell should look for it in "/bin", "/usr/bin". Looking for the existence of a file can be done with the stat() system call. The Shell then attempts to execute the command, printing an error message if it can't. Execution of the command is of course done via fork() and one of the exec() system calls.

Important: The Shell should print out error messages when the command doesn't exist, or when arguments to built-in commands are illegal. (System programs will deal with errors in their arguments themselves.)

Here is a transcript of an interaction with ics412sh_v1 (user input is in bold):

ics412sh% pwd
/home/casanova/ics412/pa2
ics412sh% ls
ics412sh_v1.c      Makefile
ics412sh% cd
ics412sh% pwd
/home/casanova
ics412sh% cd ..
ics412sh% pwd
/home
ics412sh% sleep 10
^C ics412sh% logout
To get you started, and so that you don't struggle with commandline parsing, here is a starting point: ics412sh_skeleton.c. You can compile this program and run it. It doesn't execute the commands or handles the ^C, but it parses the commandline and prints out their arguments. Parsing is done via a single call to function parse_commandline(char *commandline, char ***args1, char ***args2, char **output), which is documented in the source code. (For this question, you do not need to worry about args2 or about output.)

Question #2 (5 pts): ics412sh Version 2
Implement a more evolved Shell, called ics412sh_v2, which extends the Shell from Question #1 as follows. The user must be able to stop the execution of a running system programs by hitting ^C. The Shell itself must not exit if the user types ^C. (Typing ^C sends a SIGINT signal to the process.)


Question #3 (10 pts): ics412sh Version 3
Implement a more evolved Shell, called ics412sh_v3, which extends the Shell from Question #2 by allowing the user to do pipes:

ics412sh% pwd
/home/casanova/ics412/pa2
ics412sh% ls | grep ke
Makefile
ics412sh% exit
Function parse_commandline() will do the parsing of the commandline for you, and place the arguments to the second command in a NULL-terminated arrays (read the documentation of parse_commandline()).

Question #4 (10 pts): ics412sh Version 4
Implement a more evolved Shell, called ics412sh_v4, which extends the Shell from Question #3 by allowing the user to redirect output to a file:

ics412sh% ls > somefile
ics412sh% ls | grep ke > someotherfile
ics412sh% cat someotherfilels
Makefile
ics412sh% exit
Function parse_commandline() will do the parsing of the commandline for you, and place the name of the file to which output should be redirected in a string (read the documentation of parse_commandline()).


Question #5 (10 pts): ics412sh Version 5 [EXTRA CREDIT]
Implement a more evolved Shell, called ics412sh_v5, which extends the Shell from Question #4 by allowing the user to start jobs in the background, kill them, and wait for them:

ics412sh% ls -R / > foo &
job [1]
ics412sh% ls -R /home/ > foo2 &
job [2]
ics412sh% sleep 5 &
job [3]
ics412sh% sleep 10
[3] Done
ics412sh% jobs
[1] Running
[2] Running
ics412sh% kill 1
[1] Killed
ics412sh% kill 3
No such job
ics412sh% fg 2

henric@hawaii.edu