assign2: get_env_value

Your first task is to implement the function get_env_value in env_util.c, with the following signature:

const char *get_env_value(const char *envp[], const char *key);

The get_env_value function takes in two parameters: envp, an array of environment variable strings, and key, the name of a specific variable of interest. Each element in envp is a string of the form "KEY=VALUE, e.g. "USER=someuser", and envp contains a NULL pointer as its last element - we can use this to know when we have reached the end of the array. Your function should search the array for the element corresponding to key and return its value, or return NULL if it was not found in the array. For example, asume envp looks like the following:

["USER=someuser", "VAR1=VALUE1", "VAR2=VALUE2", NULL]

If we called get_env_value with this array as envp and the following values for key, this is what would be returned:

if key is "USER", the function would return "someuser"
if key is "VAR1", the function would return "VALUE1"
if key is "NOTTHERE", the function would return NULL
if key is "VAR", the function would return NULL

Your function must iterate through the envp array in search of a matching entry, and if it finds the matching entry (an exact match with the variable name), should return a pointer to the portion of the string following the '=' character. It should not make a copy of the value string.

For each entry in the envp array, you can assume that neither KEY nor VALUE will contain an =.

Testing

For this part, you should add at least 3 additional tests of your own in the custom_tests file that show thoughtful effort to develop comprehensive test coverage.

This function can be tested in isolation with the provided myprintenv.c program, which you do not need to modify or comment, but whose code you should read in order to understand how it calls your get_env_value function. myprintenv behaves similarly to printenv in that you can specify one or more environment variable names as command line arguments and it will print out the value of each of those. You can run myprintenv without arguments to print out all environment variables. You can also write sanitycheck custom tests with myprintenv.

We recommend using the env command to help with testing, both manually and in sanitycheck custom tests! For instance, if you execute

env USER=otheruser ./myprintenv USER

this will change the USER environment variable just for executing myprintenv this one time to be otheruser instead of your SUNET ID. You can then ensure that myprintenv prints out the correct value, otheruser.

Note that there is one special environment variable, "_", whose set value will always differ between your solution and the sample solution. If you wish to test looking up the value for "_", use env to set it temporarily to a different value.

Before moving on: make sure you have thoroughly tested your get_env_value function, making sure to cover various different cases of possible inputs, and that you have written your custom tests. You will use this function later in the assignment, so it's vital to ensure it's thoroughly tested before moving on!