



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Professor: Damian-Iordache; Class: Operating Systems; Subject: Computer Science; University: Villanova University; Term: Unknown 1989;
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




This is a group assignment. The goal of this assignment is to expand your understanding of how a shell works by write a shell similar to, but much simpler, than the one you run on Unix (the bash shell).
A shell is an interactive command-line interpreter that runs programs on behalf of the user. A shell repeatedly prints a prompt, waits for a command line on stdin, and then carries out some action, as directed by the contents of the command line.
The command line is a sequence of ASCII text words delimited by whitespace. The first word in the command line is either the name of a built-in command (such as exit or quit) or the pathname of an executable file. The remaining words are command-line arguments. If the first word is a built-in command, the shell immediately executes the command in the current process. Otherwise, the word is assumed to be the pathname of an executable program. In this case, the shell forks a child process, then loads and runs the program in the context of the child. For example, typing the command line
bash > ls -l -d
runs the ls program. By convention, the shell ensures that when the ls program begins executing its main routine
int main(int argc, char *argv[])
the argc and argv arguments have the following values:
argc == 3 argv[0] == “/bin/ls” argv[1] == “-l” argv[2] == “-d”
An outline of the shell code is given on the next page. However, feel free to use your own code design.
Recall:
If you have a printf statement AFTER an execv call, that printf statement will get executed ONLY if execv fails. Otherwise, execv will delete the image of the calling process (including the printf line)!
Here is an example. Suppose that your program reads in commands from the user into a local buffer called command:
#define MAXCMD 20 char usercmd[MAXCMD];
However, the user command may be longer than the buffer you have allocated for it. In this case, your program should (a) Read and discard the remaining characters in the line (use fgets in a loop) (b) Print out an error message
To check if the user has typed in a line longer than MAXCMD:
if(usercmd[strlen(usercmd)-1] != '\n') // line longer than the buffer size // read the rest of the line and ignore it
To get rid of the extra characters in the input line:
if(usercmd[strlen(usercmd)-1] != '\n') do { fgets(usercmd, MAX_CMD, stdin); } while(usercmd[strlen(usercmd)-1] != '\n'); // now return to the shell prompt
Hi, I am shell 12715.
12715 > ls
test.c test a.out
12715 > pwd
/mnt/a/mdamian/test
12715 > hi
Command not found.
12715 > Loooooooooooooooooooooooooooooooooooooong
Command too long! Use 20 characters or less.
12715 > quit
Bye
Hand in a printout of your code, a sample output and a short README files documenting your program. The README should have three sections:
Your C source file (tinyshell.c) and the README file should be in directory ~/csc1600/tinyshell.