


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



due Tues Nov. 25, 2008, 6:30 PM
Specifications:
In this assignment, you will implement a microshell in C/C++ on the departmental Linux machines. This shell does the following:
You can test your program using a text file named “myfile” with the contents of: Illinois USA DeKalb
Output: turing%>mysh myshell>cat myfile || sort DeKalb Illinois USA myshell>quit
turing%>mysh myshell>less myfile || grep DeKalb DeKalb myshell>ls -Alst myfile (etc.) mysh myshell>q
turing%>
Background Knowledge:
Suggestions:
Tackle the problem step by step. First make sure that your shell is taking inputs correctly. Then you should test the execution of the commands without involving any pipes. Afterwards you can go ahead and solve the pipe problem.
Requirements:
The programs should 1) work according to the specifications; 2) be comprehensible and well commented; 3) check the return value of the system calls and have proper error handling. Grading will be based on the correctness of the program, as well as documentation and formatting.
Submission:
Same as program #1.
Execvp() is one of the six exec functions. You can also use others in the assignment. If you plan to use execvp, its syntax is:
int execvp(const char *filename, char *const argv[]);
You will need to build up the array of argv[], which contains all the arguments, and ends with NULL.
When you call it, it could be something like:
execvp(comargs[0],comargs);
Where the comargs[0] contains the name of the command, comargs starts with the command, and then all the arguments and the NULL character.
The execv function replaces your program with a new program. (The system function runs a new program, then continues running your program. You should not use system in the assignment.)
How do I use strtok()
strtok() can help you parse the commands you get from input. If you choose to use it, the syntax is:
char *strtok(char *s1, const char *s2);
Example: string is “abcd efgh || ijk lmn”
number =0; command[0]=strtok(string,"||"); while((ptr=strtok(NULL, "||")) != NULL) { number++; command[number]=ptr; }
Then command[0] will contain “abcd efgh”, command[1] will contain “ijk lmn”.
How do I do error handling?
You need to check the return value of a system call. E.g.: if the return value of – indicates an error, you should have a block that handles it. The simplest way is to print out an error and then exit the program.