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:

unsetenv("PATH");

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>
   85
   86  #include "system.h"
   87  #include "error.h"
   88  #include "closeout.h"
   89
   90  /* The official name of this program (e.g., no 'g' prefix).  */
   91  #define PROGRAM_NAME "env"
   92
   93  #define AUTHORS N_ ("Richard Mlynarik and David MacKenzie")
   94
   95  int putenv();
   96
   97  extern char **environ;
   98
   99  /* The name by which this program was run. */
   100  char *program_name;
   101
   102  static struct option const longopts[] =
   103  {
   104    {"ignore-environment", no_argument, NULL, 'i'},
   105    {"unset", required_argument, NULL, 'u'},
   106    {GETOPT_HELP_OPTION_DECL},
   107    {GETOPT_VERSION_OPTION_DECL},
   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 9.1.5.3, "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;
   145
   146    program_name = argv[0];
   147    setlocale (LC_ALL, " ");
   148    bindtextdomain (PACKAGE, LOCALEDIR);
   149    textdomain (PACKAGE);
   150
   151    atexit (close_stdout);
   152
   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;
   165          case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
   166          default:
   167            usage (2);
   168          }
   169      }
   170
   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.  */
enum
{
  GETOPT_HELP_CHAR = (CHAR_MIN - 2),
  GETOPT_VERSION_CHAR = (CHAR_MIN - 3)
};

#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 9.1.5.1, "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;
   176
   177    if (!ignore_environment)
   178      for (; *envp; envp++)
   179        putenv (*envp);
   180
   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. */
   185
   186      if (optind != argc && !strcmp (argv[optind], "-"))    Skip options
   187        ++optind;
   188
   189    while (optind < argc && strchr (argv[optind], '='))    Set environment variables
   190      putenv (argv[optind++]);
   191
   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]);
   201
   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

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020