You may work alone or with one other classmate, and each person is expected to contribute equally to the effort.
int system(const char * command);This function executes the command specified as argument by invoking the Unix shell. The shell executes the command in the background, and passes the result to the calling program (-1 in case of an error, the value returned by the main function of program specified as command otherwise). To be able to use the system function, you need a working shell in the first place. Given that our ultimate goal is to write a shell from scratch, we'll have to assume that the system function is unavailable to us.
The first goal of this assignment is to write your own mysystem function with the
void mysystem(const char * command);
The mysystem should execute the command supplied as an argument. If the command fails, the function should print an
Standard C offers a system function called execv that executes the command specified as the first argument:
execv(char * fullcmd, char * cmdargv);Here fullcmd is the full path for the file (command) to be executed, and cmdargv is an array of strings representing the arguments for fullcmd. By convention, the first string in cmdargv is the name of the file to be executed. The function execv returns to the calling program only if there is an error, such as not being able to find the executable. After a successful execv, there is no return to the calling program.
In building the full command for execv, you will make use of the global variable searchpaths built by the function tokenize_PATH from your previous assignment.
cp -r ~/systems/smartshell ~/systems/smartshell-oneVerify that the new directory has been successfully created (use ls). The solution to this assignment should be in the directory ~/systems/smartshell.
void mysystem(char * command)that mimics the behavior of the standard system function. Your code should implement the following steps:
char * fullcmd;To implement this step, spawn a child process using fork. In a loop, the child process will be an exact copy of the calling process. The child process scans searchpaths and uses each entry i as follows:
fullcmd = (char *)malloc(strlen(searchpaths[i]) + strlen(cmdargv) + 2);
sprintf(fullcmd, "%s/%s", searchpaths[i], cmdargv);
execv(fullcmd, cmdargv);If the executable fullcmd exists, then execv succeeds and your child process ends (because the child code, which includes this loop, gets overwritten by the code of the executable cmdargv). If the executable fullcmd does not exist, then the child continues with the instruction following the execv call. This instruction must free the memory dynamically allocated in this iteration, in preparation for the next loop iteration:
Note: You may not use calls to execvp or exceve in implementing the mysystem function.
mysystem("ls -a");The output should be identical to the one obtained by typing ls -a at the shell prompt.
gcc smartshell.c stringarray.c -o xsmartshellThe two source files will be linked into one executable file called xsmartshell.
To simplify your development process, you may use this Makefile and simply type in make at the shell prompt to compile your code. This saves you from typing in a long command line every time you need to compile your code.
Makefiles are special format files that help build and manage projects composed of multiple modules. The key point to remember is that you need to insert a TAB character (not spaces) before each command in the Makefile. If interested, you may read more about using make and writing Makefiles.
smartshelldirectory create a text file named
Readme, etc.) that contains:
Turn in a printout copy of the
smartshell.c files, and a sample output (one printout per team). If you work on the department Unix system, you will need to copy and paste your code into a text or a Word document in Windows, since you cannot print directly from Unix.
Leave the source code for all exercises in your directory systems/smartshell. Do not make any changes to these files after the due date for this assignment. If you wish to continue working on these exercises after the due date, make a copy of your directory smartshell using the following Unix command:
cp -r ~/systems/smartshell ~/systems/smartshell-copy2A new directory called smartshell-copy2 will be created in your systems directory. You may now make any changes you want to the files in your smartshell-copy2 directory, at any time.
|No credit will be given for code that does not compile.|
|100||Total points possible|
|20||Program compiles and runs without crashing|
|20||Each command does something resembling the specified action|
|45||The mysystem function works correctly and completely|
|5||You have added some extra "bells and whistles" (use your imagination)|
|5||Submission includes readme file|
|5||Submission includes sample output|