1. Print out the custom made prompt;
printbeer();
printf("%s%: ",userName());
The both commands run once prior to the while loop. This is to initialize the shell the printf will run at the end of each cycle of the while. userName will pull the first valid environment variable associated with a users name.
2. get in the command line; 3.parse the command line;
fgets(com,LIM,stdin);
args[0]=strtok(com," \n");
buffer=args[0];
First we pull in the full command (com) only taking in as many characters as allowed by LIM from the standard in stream (stdin). We get the command we want using string tokenizer and set it to the buffer.
4. Execute the command line;
if(strcmp(buffer,"quit")==0)
exit(0);
else if(strcmp(buffer,"cd")==0)
cd(args);
else
run(buffer,args);
These lines check to se what type of command is being run and chooses the proper method if the buffer matches the string quit the shell will close. If buffer matches cd it will run the cd method. Otherwise it will default. Note the code will not run if buffer is empty.
run(char *buf,char *args[])
{
char *temporary=strtok(NULL,"\n");
int c=1;
while(temporary!=NULL)
{
args[c++]=temporary;
temporary=strtok(NULL,"\n");
}
if(fork()==0)
{
execvp(buf,args);
exit(1);
}
wait(NULL);
}
The first line has strtok pull of the next object of the command string(com) and set it to temp. The first argument is set to NULL because if it is set to com it will pull from the start of the string. We add each temp to the argument array we do this until we run out of arguments. Then we run execvp on the file named in the buffer string (buf) with the set of arguments (args). Execvp requires that the actual command to exist in args and buffer is set to args[0].
cd(char *args[])
{
char *current = getenv("PWD");
char *toDir;
char *entDir;
args[1] = strtok(NULL, " \n");
entDir = args[1];
if(entDir == NULL)
toDir = current;
else if(args[1][0] == '/')
toDir=down(current,entDir,1);
else if(strcmp(entDir,"..") != 0)
toDir=down(current,entDir,0);
else if(strcmp(entDir,"..") == 0)
toDir = up(current);
printf("%s\n", toDir);
if(chdir(toDir)!=0)
toDir=chd(toDir);
setenv("PWD", toDir, 1);
}
cd takes in the argument associated with cd. We set the Parent Working Directory of the environment and set it to the string current. We run a set of comparisons on the args to see if is going to “/dir”, “dir”, “..”, and then if the dir exists or if it is empty. If its empty we jump right to the setenv where we set the directory to the Parent Working Directory.
char *down(char *cur,char *edir,int test)
{
char *tdir;
if(test==1)
{
tdir=cur;
strcat(tdir,edir);
}
else if(test==0)
{
tdir=strcat(cur,"/");
strcat(tdir,edir);
}
return tdir;
}
Down() Takes the current directory the directory we want to go to and a test value that says whether or not it has a / preceding it. If it does not have a proceeding / it will concatenate it to the string. Then it will concatenate destination to the final directory string and returns it to cd where it will get run through chd and finally setenv.
char *up(char *cur)
{
char *dir = cur;
char *mark;
mark = strrchr(dir, '/');
*mark = '\0';
return dir;
}
Up() works like down but instead finds the last location of / (the end of the parent directory) and replaces it with an end of file and then goes back to cd and finally to setenv.
char *chd(char *tdir)
{
char *mark;
mark = strrchr(tdir, '/');
*mark = '\0';
printf("Directory does not exist\n");
return tdir;
}
chd is the last part it checks to see if a directory exists if it does not it will look for the last / which is the end of the current directory (we have already concatenated something to the end and we need to remove it) and it notifies the user that the directory does not exist. We return this path to cd to run setenv on it.