Assignment #6 Addendum

 

Intoduction:
Several interesting points have been raised about Assignment #6 (as it
stands), and I'm goin to attempt to clear a few of them up here.
Sending a CR to parsecmd():
Basically, the string that you send to parsecmd() should not end with a
Carriage-Return character ("\n", 0x0A). If your string does end this way,
parsecmd() will not strip it, and when you attempt to call execvp(), things
will fail miserably.

So, the solution is to stay from fgets() (which leaves the CR character in
the string that it finds), or to strip any trailing CRs from the string before
you send it to parsecmd().
Trapping CTRL-C:
In a "real-world" shell, if you hit CTRL-C, the program that you will
executing will quit, and you will be returned to your shell prompt. However,
in the shell that you code, if you are running it and hit CTRL-C, it will
exit from your shell, returning you to your original login shell.

This behavior is not "wrong", because Tekin didn't specify what he wanted
in the assignment. However, if you wish to make your shell work in the
"proper" behavior, then you will have to take advantage of UNIX signals.
Basically, you will need to trap the CTRL-C signal, and instead of exiting
from your program (the default behavior), you will have to terminate the
child process, and then return to your shell-waiting-for-input mode.

You can read about signals in my lecture from March 1st. If you implemented
this, it would be considered to be a form of "error-handling", and would
return points to you that have been lost in other areas of the assignment.
Using ~ with chdir():
In most modern shells, the '~' character is supported as a shortcut for a
users' home directory. For example, if I typed

	cd ~/bin

then bash (my shell), which execute

	chdir ("/u/reitz/bin");

How does bash know that for me, '~' expands to "/u/reitz"? Simple. When it
initializes, it sets up an environment variable, $HOME. Witness this
exchange:

	goldie:~$ echo $HOME
	/u/reitz

Furthermore, when bash execs another program, these environment variables are
still accessable. So, if you wanted to support this same functionality in your
shell, it is possible to simply query your environment for the value of
the $HOME variable, and then pass the appropriate string to chdir().

The function that you need to use in order to support this activity is
getenv() (3c). You can read the man page (which is very straightforward), or
just look at this sample code:

	#include < stdlib.h >
	#include < stdio.h >
 
	int main (void)
	{
	        char *dir = NULL;
 
	        dir = getenv ("HOME");
	        if (dir == NULL)
	                {
	                perror ("getenv");
	                exit (2);
	                }
 
	        printf ("%s\n", dir);
	        return (0);
	}



PLEASE NOTE: You don't need to do this if you don't want to.
I can't even say that you will get any extra credit for it. It's just "for
fun", so that you can learn a bit more about shell internals.