PURPOSE: The purpose of this lab is to allow students to learn a user interface aspect of a UNIX shell.
Student will work with process management and some basic system calls.
Important note: please use sp1, sp2, sp3, or atoz servers for this lab.
UNIX Shell
We will start with 3 built-in commands:
• cd (change directory),
• pwd (print working directory), and
• exit (exit shell).
The built-in functions are not executed by forking and executing an executable. Instead, the shell process executes them itself.
Your shell is basically an interactive loop:
• it repeatedly prints a prompt "csc60msh ",
• parses the input, executes the command specified on that line of input,
• and waits for the command to finish.
FILES TO COPY:
To get the file you need, first move to your class folder by typing: cd csc60
The following command will create a directory named lab9 and put all the needed file into it below your csc60 directory.
Type: cp -R /gaia/home/faculty/bielr/classfiles_csc60/lab9 .
Spaces needed: (1) After the cp ↑ Don’t miss the space & dot.
(2) After the -R
(3) After the directory name at the end & before the dot.
After the files are in your account and you are still in csc60, you need to type: chmod 755 lab9
This will give permissions to the directory.
Next move into lab9 directory, and type: chmod 644 *
This will give permissions to the file.
Your new lab9 directory should now contain: lab9.c
Please follow these steps:
• Review the source codes, compile, and execute the programs. Examine the output texts to understand the behavior of each program.
• I have provided you with a simple shell template (lab9.c) that you can work from. You build your program from it. Read the template closely to identify the key components and understand its execution flow. You can compile the shell using the following command: gcc lab9.c
• Function main. Handling built-in Commands: There are three special cases where your shell should execute a command directly itself instead of running a separate process.
o First, if the user enters "exit" as a command, the shell should terminate.
o Second, if the user enters "cd dir", you should change the current directory to "dir" by using the chdir system call. If the user simply types "cd" (no dir specified), change to the user's home directory. The $HOME environment stores the desired path; use getenv("HOME") to obtain this.
o Third, if the user enters "pwd", print the current working directory. This can be obtained with getcwd() function.
o Additionally, we have to deal with the fact that a user might just type an Enter Key with no command.
Pseudo Code (Highlight indicates provided code.)
/*----------------------------------------------------------*/
int main (void)
{
while (TRUE)
{
int childPid;
char *cmdLine;
print the prompt(); /* i.e. csc60mshell , Use printf*/
fgets(cmdline, MAXLINE, stdin);
/* You have to write the call. The function itself is provided: function parseline */
Call the function parseline, sending in cmdline & argv, getting back argc
/* code to print out the argc and the agrv list to make sure it all came in. Required.*/
Print a line. Ex: “Argc = %i”
loop starting at zero, thru less than agrc, increment by one.
print each argv[loop counter]
/* Start processing the built-in commands */
if ( argc compare equal to zero)
/* a command was not entered, might have been just Enter or a space&Enter */
continue to end of while(TRUE)-loop
// next deal with the built-in commands
// Use strcmp to do the test
// after each command, do a continue to end of while(TRUE)-loop
if (“exit”)
issue an exit call
else if (“pwd”)
declare a char variable array of size MAX_PATH_LENGTH to hold the path
do a getcwd
print the path
else if (“cd”)
declare a char variable dir as a pointer (with an *)
if the argc is 1 use the getenv call with “HOME” and return the value from the call to variable dir else
variable dir gets assigned the value of argv[1]
execute a call to chdir(dir) with error checking. Message = “error changing directory”
} /* end of the while loop
} /* end of main
Resources
Useful Unix System Calls (Also see PowerPoint Slides named Lab9 Slides):
getenv/setenv: get/setenv the value of an environment variable
path = getenv("PATH");
cwd = getenv("PWD");
setenv("PWD", tempbuf, 1);
getcwd: get current working directory.
chdir: change the current working directory (use this to implement cd) C Library functions:
#include <string.h
String compare:
int strcmp(const char *s1, const char *s2);
strcmp(argv[0],"cd")
strcmp(argv[0],"exit")
strcmp(argv[0],"pwd")
strcmp(….,"")
strcmp(….,"<")
print a system error message:
perror("Shell Program error");
Input of characters and strings:
fgets(cmdline, MAXLINE, stdin);
Compilation & Building your program
The use of gcc is just fine. If you want to have the output go elsewhere from a.out, type:
gcc –o name-of-executable name-of-source-code
Partnership
Students may form a group of 2 students (maximum) to work on this lab. As usual, please always contact your instructor for questions or clarification.
Hints
Writing your shell in a simple manner is a matter of finding the relevant library routines and calling them properly. Please see the resources section above.
Keep versions of your code. This is in case you need to go back to your older version due to an unforeseen bug/issue.
A lot of code to be used in Lab10 is currently commented out. Leave it there. At the start of Lab10, you will receive instructions about removing the extra comment marks.