Path Resolution
You probably found that it was a pain to test your shell in the previous part because you had to type the full path of every program. Luckily, every program (including your shell program) has access to a set of environment variables, which is structured as a hashtable of string keys to string values. One of these environment variables is the PATH
variable. You can print the PATH
variable of your development environment.
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:...
When bash
or any other shell executes a program like wc
, it looks for a program called wc
in each directory listed in the PATH
environment variable and runs the first one that it finds. The directories in PATH
are separated with a colon. You may find the function strtok_r
to be useful for parsing the PATH
string. You might want to look at the stat
C library function to understand how you can check whether a given file exists.
Modify your shell so that it uses the PATH
variable from the environment to resolve program names.
Look at the getenv
C library function to see how you can read variables from the environment. Typing in the full pathname of the executable should still be supported (i.e. before going through the list of possible directories from the PATH
environment, check if the given command can be run directly). Do not use execvp
since the autograder looks for execvp
, and you won’t receive a grade if that word is found in your code. Use execv
instead and implement your own PATH
resolution logic.
Next up: read about Redirection.