Node:Environment variables, Next:, Previous:Processing command-line options, Up:Putting a program together



Environment variables

Sometimes it is useful to communicate with a program in a semi-permanent way, so that you do not need to specify a command-line option every time you type the command to execute the program. One way to do this is to generate a configuration file, in which you can store data that will be used by the program every time it is run. This approach is typically useful if you have a large amount of data that you want to pass to a program every time it runs, or if you want the program itself to be able to change the data.

However, environment variables provide a more lightweight approach. Environment variables, sometimes called shell variables, are usually set with the export command in the shell. (This section assumes you are using the GNU Bash shell.) Standard environment variables are used for information about your home directory, terminal type, and so on; you can define additional variables for other purposes. The set of all environment variables that have values is collectively known as the environment.

Names of environment variables are case-sensitive, and it is good form to use all upper-case letters when defining a new variable; certainly this is the case for all system-defined environment variables.

The value of an environment variable can be any string that does not contain a null character (since the null character is used to terminate the string value).

Environment variables are stored in a special array that can be read by your main function. Here is the skeleton for a main function that can read environment variables; notice we have added a third parameter to main, called envp, which comes after argc and argv.

#include <stdio.h>

/* To shorten example, not using argp */
int main (int argc, char *argv[], char *envp[])
{

  return 0;
}

Notice that envp is an array of strings, just as argv is. It consists of a list of the environment variables of your shell, in the following format:

NAME=value

Just as you can manually process command-line options from argv, so can you manually process environment variables from envp. However, the simplest way to access the value of an environment variable is with the getenv function, defined in the system header stdlib.h. It takes a single argument, a string containing the name of the variable whose value you wish to discover. It returns that value, or a null pointer if the variable is not defined.

#include <stdio.h>
#include <stdlib.h>

/* To shorten example, not using argp */
int main (int argc, char *argv[], char *envp[])
{
  char *home, *host;

  home = getenv("HOME");
  host = getenv("HOSTNAME");

  printf ("Your home directory is %s on %s.\n", home, host);

  return 0;
}

When you run this code, it will print out a line like the following one.

Your home directory is /home/rwhe on linnaeus.

Note: Do not modify strings returned from getenv; they are pointers to data that belongs to the system. If you want to process a value returned from getenv, copy it to another string first with strcpy. (See Strings.) If you want to change an environment variable from within your program (not usually advisable), use the putenv, setenv, and unsetenv functions. See Environment Access, for more information on these functions.