Homework #2

Assigned: September 8
Due: September 15, 4:30pm


(71 points) Implement the UNIX env utility in C.

The env utility examines the environment and modifies it to execute another command. When called without arguments, the env command writes the current environment to standard output. The optional utility argument specifies the command to be executed under the modified environment. The optional -i argument means that env should ignore the environment inherited from the shell when executing utility. Without the -i option, env uses the [name=value] arguments to modify rather than replace the current environment to execute utility. The env command does not modify the environment of the shell which executes it. See the env manpage for more information.

      env [-i] [name=value] ... [utility [argument ...]]                                                 
POSIX: Shell and Utilities


Write a program which behaves in the same way as the env utility when executing another program.
  1. When called with no arguments, the env utility calls the getenv function and outputs the current environment to standard output.
  2. When env is called with the optional -i argument, the entire environment is replaced by the name=value pairs. Otherwise, the pairs modify or add to the current environment.
  3. If the utility argument is given, use system to execute utility after the environment has been appropriately changed. Otherwise, print the changed environment to standard output, one entry per line. Check the return value of system to handle any errors.
  4. One way to change the current environment in a program is to overwrite the value of the environ external variable. If you are completely replacing the old environment (-i option), count the number of name=value pairs, allocate enough space for the argument array (do not forget the extra NULL entry), copy the pointers for argv into the array, and set environ.
  5. If you are modifying the current environment by overwriting environ, allocate enough space to hold the old environ into the new one. For each name=value pair, determine whether the name is already in the old environment. If the name appears, just replace the pointer. Otherwise, add the new entry to the array.
  6. Note that it not safe to just append new entries to the old environ, since you cannot expand the old environ array with realloc. If all the name=value pairs correspond to entries already in the environment, just replace the corresponding pointers in environ.
  7. You must not use the clearenv or putenv functions in your implementation.
  8. Return a different integer as an exit status for an invalid option as that returned for an invalid utility. Mimic the behavior of env on our system.
  9. Your program must be written in C (not C++) and compile without errors or warnings using gcc on our system.
If designed properly, the program required to solve this homework should occupy no more than 200 lines of code.

How to submit

Note: All directory and filenames below are case-sensitive. You must use the directory and filenames exactly as shown below (i.e., all lower case).

Prepare your source code file as /home/<logname>/homeworks/hw2/env.c, where <logname> is your login name (e.g., cps444-n1.01). Only your source file env.c will be electronically collected from this location in your UNIX account on the deadline.

Failure to follow these submission requirements will result in a 10% penalty.


Ninety percent of your score will come from correctness and 10% of your score will come from following our programming style guide. Applicable submission penalties will then be applied.

Return Home