CSC 201 Grinnell College Spring, 2005
 
Data Representation, Memory Management, and Formal Methods
 

Laboratory Exercise on Environmental Variables

Goals

This laboratory exercise reviews several basic elements of environmental variables and provides practice using these variables within C programs.

Environmental Variables within a Shell

When working at a workstation, it is common to open a terminal window to perform various tasks. From your past experience, you know that when you start using a terminal window, the window seems positioned at your home directory. Thereafter you can move from one directory to another through the cd command. This familiar interaction illustrates a more general feature of shell windows: a shell maintains a collection of environmental variables. In working with users, a shell keeps track of various pieces information through variables that are kept behind the scenes within the shell.

On Unix systems, two such variables are PWD and PATH. PWD holds the user's current (working) directory, and PATH contains a sequence of directories −− separated by colons, indicating where to search for commands issued by the user.

  1. Open a terminal window, and type the commands

    
        pwd
    
    and
    
        echo $PATH
    

    The first of these commands (print working directory) returns the value of the PWD variable, while the second shows a more generic way of displaying the value of any environmental variable.

  2. Type the command echo $PWD to check that this returns the same directory as given by the pwd command.

In reviewing the results of the


    echo $PATH

command, you will see a list of file directories, separated by semi-colons. One such string is:


    /bin:/usr/bin:/net/bin:.

When you type a command (e.g., ls to list a directory), the terminal window must locate the program that corresponds to the command. To begin, the shell looks at the first directory in the $PATH variable −− in this case /bin. Since the ls program is located there, searching stops, and the shell runs it.

If you type the command matlab, the search of /bin would not find the required program, so the shell would go on to the next directory in the $PATH −− in this case, the directory /usr/bin. If that search fails as well, then the search continues with directory /net/bin. In this case, matlab is found in /net/bin, so searching is done.

In this list of directories, the dot ( . ) indicates the current directory −− that is, the directory given in PWD.

  1. At a practical level, once you know the search path, then you can find exactly which program is run in response to your command. Just list the directories from the $PATH variable, until you find the program's name.

    1. In what directory is the pwd command located?

    2. In what directory is the C-compiler gcc command located?

Working with Environmental Variables within C Programs

When working directly with a shell, the discussion so far demonstrates how you can retrieve an environmental variable directly. We now turn to use of these variables within a C program.

Program env-test.c illustrates the use of procedures getenv and setenv within a C program to read and modify environmental variables, respectively.

  1. Copy ~walker/c/shell-programs/env-test.c to your account, compile it with gcc, run it, and explain the results.

  2. Use the echo $PATH command to determine the initial path for the window's shell. Then run env-test and use echo $PATH. Is the PATH of the window shell changed? Explain briefly.

  3. Modify env-test.c so that it retrieves and changes the environmental variable PWD to refer to a designated directory in your account −− other than the directory containing the env-test.c program. Compile and run the revised program, and indicate its output.

Following the Search Path

Within a shell, a basic task involves reading a command from a user, finding the desired program based on the search path, and executing the program. While spawning a process to execute programs is beyond the scope of this course, the process of reading a command and locating the program is well within the scope of tasks already considered.

  1. Write a C program with the following characteristics:

    Note: One way to determine whether or not a command is in a directory is to form a string with the command appended to the full directory. Upon trying to open the file for reading, the file will exist if the file is present, but an error will be encountered if no file is present. (Of course, if you open a file, you will want to close it before continuing.)

Work to turn in:


This document is available on the World Wide Web as

     http://www.cs.grinnell.edu/~walker/courses/201.sp05/lab-env-variables.html

created 21 January 2005
last revised 3 May 2005
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.