Writing own Unix shell in C - Problems with PATH and execv

user1287523 picture user1287523 · Sep 29, 2012 · Viewed 8.5k times · Source

I'm writing my own shell in C. It needs to be able to display the users current directory, execute commands based on the full path (must use execv), and allow the user to change the directory with cd.

This IS homework. The teacher only gave us a basic primer on C and a very brief skeleton on how the program should work. Since I'm not one to give up easily I've been researching how to do this for three days, but now I'm stumped.

This is what I have so far:

  • Displays the user's username, computername, and current directory (defaults to home directory).
  • Prompts the user for input, and gets the input
  • Splits the user's input by " " into an array of arguments
  • Splits the environment variable PATH by ":" into an array of tokens

I'm not sure how to proceed from here. I know I've got to use the execv command but in my research on google I haven't really found an example I understand. For instance, if the command is bin/ls, how does execv know the display all files/folders from the home directory? How do I tell the system I changed the directory?

I've been using this site a lot which has been helpful: http://linuxgazette.net/111/ramankutty.html but again, I'm stumped.

Thanks for your help. Let me know if I should post some of my existing code, I'm wasn't sure if it was necessary though.

Answer

MvG picture MvG · Sep 29, 2012

For instance, if the command is bin/ls, how does execv know the display all files/folders from the home directory? How do I tell the system I changed the directory?

Every process has a current working directory, which may be modified using chdir. Child processes will inherit the working directory from their parent. So in general your shell will manage its current working directory in response to cd commands entered by the user. When a command is entered which is not a builtin, then you'll fork to create a child process and then call execv there to execute the binary.

If you ant to take the PATH into account for program names that contain no directory part, then you should try all possible combinations of a PATH element and the program name. You can either check whether the named file does exist, or simply try to execute it and continue with the next if that fails. If all execv calls failed, you will have to call _exit in order to terminate the child process.

Note that most shells will treat any command which contains a / as a path which gets passed to execv directly. If the path does not start with a /, then it is a relative path, and the operating system will resolve it with respect to the current working directory. In other words, the bin/ls from your example would refer to the ls binary in the bin directory which is a subdirectory of the current working directory. Only commands which do not contain any / at all are either interpreted as a builtin command (like cd) or the name of some binary located on the PATH.

The first argument to execv is the path as you computed it. The first element of the argv list traditionally equals the name as it was enetered, i.e. without an added PATH directory. After that first argument, any additional command line parameters are passed, followed by a NULL to terminate the list.