Home > Articles

  • Print
  • + Share This
This chapter is from the book

2.4 The Environment

The environment is a set of 'name=value' pairs for each program. These pairs are termed environment variables. Each name consists of one to any number of alphanumeric characters or underscores ('_'), but the name may not start with a digit. (This rule is enforced by the shell; the C API can put anything it wants to into the environment, at the likely cost of confusing subsequent programs.)

Environment variables are often used to control program behavior. For example, if POSIXLY_CORRECT exists in the environment, many GNU programs disable extensions or historical behavior that isn't compatible with the POSIX standard.

You can decide (and should document) the environment variables that your program will use to control its behavior. For example, you may wish to use an environment variable for debugging options instead of a command-line argument. The advantage of using environment variables is that users can set them in their startup file and not have to remember to always supply a particular set of command-line options.

Of course, the disadvantage to using environment variables is that they can silently change a program's behavior. Jim Meyering, the maintainer of the Coreutils, put it this way:

It makes it easy for the user to customize how the program works without changing how the program is invoked. That can be both a blessing and a curse. If you write a script that depends on your having a certain environment variable set, but then have someone else use that same script, it may well fail (or worse, silently produce invalid results) if that other person doesn't have the same environment settings.

2.4.1 Environment Management Functions

Several functions let you retrieve the values of environment variables, change their values, or remove them. Here are the declarations:

#include <stdlib.h>

char *getenv(const char *name);                    ISO C: Retrieve environment variable
   int setenv(const char *name, const char *value,    POSIX: Set environment variable
   int overwrite);
   int putenv(char *string);                          XSI: Set environment variable, uses string
   void unsetenv(const char *name);                   POSIX: Remove environment variable
   int clearenv(void);                                Common: Clear entire environment

The getenv() function is the one you will use 99 percent of the time. The argument is the environment variable name to look up, such as "HOME" or "PATH". If the variable exists, getenv() returns a pointer to the character string value. If not, it returns NULL. For example:

char *pathval;

/* Look for PATH; if not present, supply a default value */
if ((pathval = getenv("PATH")) == NULL)
    pathval = "/bin:/usr/bin:/usr/ucb";

Occasionally, environment variables exist, but with empty values. In this case, the return value will be non-NULL, but the first character pointed to will be the zero byte, which is the C string terminator, '\0'. Your code should be careful to check that the return value pointed to is not NULL. Even if it isn't NULL, also check that the string is not empty if you intend to use its value for something. In any case, don't just blindly use the returned value.

To change an environment variable or to add a new one to the environment, use setenv():

if (setenv("PATH", "/bin:/usr/bin:/usr/ucb", 1) != 0) {
    /* handle failure */

It's possible that a variable already exists in the environment. If the third argument is true (nonzero), then the supplied value overwrites the previous one. Otherwise, it doesn't. The return value is -1 if there was no memory for the new variable, and 0 otherwise. setenv() makes private copies of both the variable name and the new value for storing in the environment.

A simpler alternative to setenv() is putenv(), which takes a single "name=value" string and places it in the environment:

if (putenv("PATH=/bin:/usr/bin:/usr/ucb") != 0) {
    /* handle failure */

putenv() blindly replaces any previous value for the same variable. Also, and perhaps more importantly, the string passed to putenv() is placed directly into the environment. This means that if your code later modifies this string (for example, if it was an array, not a string constant), the environment is modified also. This in turn means that you should not use a local variable as the parameter for putenv(). For all these reasons setenv() is preferred.

The GNU putenv() has an additional (documented) quirk to its behavior. If the argument string is a name, then without an = character, the named variable is removed. The GNU env program, which we look at later in this chapter, relies on this behavior.

The unsetenv() function removes a variable from the environment:


Finally, the clearenv() function clears the environment entirely:

if (clearenv() != 0) {
    /* handle failure */

This function is not standardized by POSIX, although it's available in GNU/Linux and several commercial Unix variants. You should use it if your application must be very security conscious and you want it to build its own environment entirely from scratch. If clearenv() is not available, the GNU/Linux clearenv(3) manpage recommends using 'environ = NULL;' to accomplish the task.

2.4.2 The Entire Environment: environ

The correct way to deal with the environment is through the functions described in the previous section. However, it's worth a look at how things are managed "under the hood."

The external variable environ provides access to the environment in the same way that argv provides access to the command-line arguments. You must declare the variable yourself. Although standardized by POSIX, environ is purposely not declared by any standardized header file. (This seems to evolve from historical practice.) Here is the declaration:

extern char **environ;   /* Look Ma, no header file! */              POSIX

Like argv, the final element in environ is NULL. There is no "environment count" variable that corresponds to argc, however. This simple program prints out the entire environment:

/* ch02-printenv.c --- Print out the environment. */

#include <stdio.h>

extern char **environ;

int main(int argc, char **argv)
    int i;

    if (environ != NULL)
        for (i = 0; environ[i] != NULL; i++)
            printf("%s\n", environ[i]);
    return 0;

Although it's unlikely to happen, this program makes sure that environ isn't NULL before attempting to use it.

Variables are kept in the environment in random order. Although some Unix shells keep the environment sorted by variable name, there is no formal requirement that this be so, and many shells don't keep them sorted.

As something of a quirk of the implementation, you can access the environment by declaring a third parameter to main():

int main(int argc, char **argv, char **envp)

You can then use envp as you would have used environ. Although you may see this occasionally in old code, we don't recommend its use; environ is the official, standard, portable way to access the entire environment, should you need to do so.

2.4.3 GNU env

To round off the chapter, here is the GNU version of the env command. This command adds variables to the environment for the duration of one command. It can also be used to clear the environment for that command or to remove specific environment variables. The program serves double-duty for us, since it demonstrates both getopt_long() and several of the functions discussed in this section. Here is how the program is invoked:

$ env --help
   Usage: env [OPTION] ... [-] [NAME=VALUE] ... [COMMAND [ARG] ...]
   Set each NAME to VALUE in the environment and run COMMAND.
   -i, --ignore-environment   start with an empty environment
   -u, --unset=NAME           remove variable from the environment
   --help     display this help and exit
   --version  output version information and exit
   A mere - implies -i.  If no COMMAND, print the resulting environment.
   Report bugs to <bug-coreutils@gnu.org>.

Here are some sample invocations:

$ env - myprog arg1                              Clear environment, run program with args
   $ env - PATH=/bin:/usr/bin myprog arg1           Clear environment, add PATH, run program
   $ env -u IFS PATH=/bin:/usr/bin myprog arg1      Unset IFS, add PATH, run program

The code begins with a standard GNU copyright statement and explanatory comment. We have omitted both for brevity. (The copyright statement is discussed in Appendix C, "GNU General Public License", page 657. The --help output shown previously is enough to understand how the program works.) Following the copyright and comments are header includes and declarations. The 'N_("string")' macro invocation (line 93) is for use in internationalization and localization of the software, topics covered in Chapter 13, "Internationalization and Localization," page 485. For now, you can treat it as if it were the contained string constant.

 80  #include <config.h>
   81  #include <stdio.h>
   82  #include <getopt.h>
   83  #include <sys/types.h>
   84  #include <getopt.h>
   86  #include "system.h"
   87  #include "error.h"
   88  #include "closeout.h"
   90  /* The official name of this program (e.g., no 'g' prefix).  */
   91  #define PROGRAM_NAME "env"
   93  #define AUTHORS N_ ("Richard Mlynarik and David MacKenzie")
   95  int putenv();
   97  extern char **environ;
   99  /* The name by which this program was run. */
   100  char *program_name;
   102  static struct option const longopts[] =
   103  {
   104    {"ignore-environment", no_argument, NULL, 'i'},
   105    {"unset", required_argument, NULL, 'u'},
   108    {NULL, 0, NULL, 0}
   109  };

The GNU Coreutils contain a large number of programs, many of which perform the same common tasks (for example, argument parsing). To make maintenance easier, many common idioms are defined as macros. GETOPT_HELP_OPTION_DECL and GETOPT_VERSION_OPTION (lines 106 and 107) are two such. We examine their definitions shortly. The first function, usage(), prints the usage information and exits. The _("string") macro (line 115, and used throughout the program) is also for internationalization, and for now you should also treat it as if it were the contained string constant.

111  void
   112  usage (int status)
   113  {
   114    if (status != 0)
   115      fprintf (stderr, _("Try '%s --help' for more information.\n"),
   116               program_name);
   117    else
   118      {
   119        printf (_("   120  Usage: %s [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG] ...]\n"),
   121                program_name);
   122        fputs (_("   123  Set each NAME to VALUE in the environment and run COMMAND.\n   124  \n   125    -i, --ignore-environment   start with an empty environment\n   126    -u, --unset=NAME           remove variable from the environment\n   127  "), stdout);
   128        fputs (HELP_OPTION_DESCRIPTION, stdout);
   129        fputs (VERSION_OPTION_DESCRIPTION, stdout);
   130        fputs (_("   131  \n   132  A mere - implies -i. If no COMMAND, print the resulting environment.\n   133  "), stdout);
   134        printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
   135      }
   136    exit (status);
   137  }

The first part of main() declares variables and sets up the internationalization. The functions setlocale(), bindtextdomain(), and textdomain() (lines 147–149) are all discussed in Chapter 13, "Internationalization and Localization," page 485. Note that this program does use the envp argument to main() (line 140). It is the only one of the Coreutils programs to do so. Finally, the call to atexit() on line 151 (see Section, "Exiting Functions," page 302) registers a Coreutils library function that flushes all pending output and closes stdout, reporting a message if there were problems. The next bit processes the command-line arguments, using getopt_long().

139  int
   140  main (register int argc, register char **argv, char **envp)
   141  {
   142    char *dummy_environ[1];
   143    int optc;
   144    int ignore_environment = 0;
   146    program_name = argv[0];
   147    setlocale (LC_ALL, " ");
   148    bindtextdomain (PACKAGE, LOCALEDIR);
   149    textdomain (PACKAGE);
   151    atexit (close_stdout);
   153    while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
   154      {
   155        switch (optc)
   156          {
   157          case 0:
   158            break;
   159          case 'i':
   160            ignore_environment = 1;
   161            break;
   162          case 'u':
   163            break;
   164          case_GETOPT_HELP_CHAR;
   166          default:
   167            usage (2);
   168          }
   169      }
   171    if (optind != argc && !strcmp (argv[optind], "-"))
   172      ignore_environment = 1;

Here are the macros, from src/sys2.h in the Coreutils distribution, that define the declarations we saw earlier and the 'case_GETOPT_xxx' macros used above (lines 164–165):

/* Factor out some of the common --help and --version processing code.  */

/* These enum values cannot possibly conflict with the option values
   ordinarily used by commands, including CHAR_MAX + 1, etc. Avoid
   CHAR_MIN - 1, as it may equal -1, the getopt end-of-options value.  */

#define GETOPT_HELP_OPTION_DECL   "help", no_argument, 0, GETOPT_HELP_CHAR
#define GETOPT_VERSION_OPTION_DECL   "version", no_argument, 0, GETOPT_VERSION_CHAR

#define case_GETOPT_HELP_CHAR                     case GETOPT_HELP_CHAR:                            usage (EXIT_SUCCESS);                           break;

#define case_GETOPT_VERSION_CHAR(Program_name, Authors)                  case GETOPT_VERSION_CHAR:                                                version_etc (stdout, Program_name, PACKAGE, VERSION, Authors);         exit (EXIT_SUCCESS);                                                   break;

The upshot of this code is that --help prints the usage message and --version prints version information. Both exit successfully. ("Success" and "failure" exit statuses are described in Section, "Defining Process Exit Status," page 300.) Given that the Coreutils have dozens of utilities, it makes sense to factor out and standardize as much repetitive code as possible.

Returning to env.c:

174    environ = dummy_environ;
   175    environ[0] = NULL;
   177    if (!ignore_environment)
   178      for (; *envp; envp++)
   179        putenv (*envp);
   181    optind = 0;                              /* Force GNU getopt to re-initialize. */
   182    while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != –1)
   183      if (optc == 'u')
   184        putenv (optarg);               /* Requires GNU putenv. */
   186      if (optind != argc && !strcmp (argv[optind], "-"))    Skip options
   187        ++optind;
   189    while (optind < argc && strchr (argv[optind], '='))    Set environment variables
   190      putenv (argv[optind++]);
   192    /* If no program is specified, print the environment and exit. */
   193    if (optind == argc)
   194      {
   195        while (*environ)
   196          puts (*environ++);
   197        exit (EXIT_SUCCESS);
   198      }

Lines 174–179 copy the existing environment into a fresh copy of the environment. The global variable environ is set to point to an empty local array. The envp parameter maintains access to the original environment.

Lines 181–184 remove any environment variables as requested by the -u option. The program does this by rescanning the command line and removing names listed there. Environment variable removal relies on the GNU putenv() behavior discussed earlier: that when called with a plain variable name, putenv() removes the environment variable.

After any options, new or replacement environment variables are supplied on the command line. Lines 189–190 continue scanning the command line, looking for environment variable settings of the form 'name=value'.

Upon reaching line 192, if nothing is left on the command line, env is supposed to print the new environment, and exit. It does so (lines 195–197).

If arguments are left, they represent a command name to run and arguments to pass to that new command. This is done with the execvp() system call (line 200), which replaces the current program with the new one. (This call is discussed in Section 9.1.4, "Starting New Programs: The exec() Family," page 293; don't worry about the details for now.) If this call returns to the current program, it failed. In such a case, env prints an error message and exits.

200    execvp (argv[optind], &argv[optind]);
   202    {
   203      int exit_status = (errno == ENOENT ? 127 : 126);
   204      error (0, errno, "%s", argv[optind]);
   205      exit (exit_status);
   206    }
   207  }

The exit status values, 126 and 127 (determined on line 203), conform to POSIX. 127 means the program that execvp() attempted to run didn't exist. (ENOENT means the file doesn't have an entry in the directory.) 126 means that the file exists, but something else went wrong.

  • + Share This
  • 🔖 Save To Your Account