Go to the first, previous, next, last section, table of contents.


Environment Access

The value of an environment variable can be accessed with the getenv function. This is declared in the header file `stdlib.h'. All of the following functions can be safely used in multi-threaded programs. It is made sure that concurrent modifications to the environment do not lead to errors.

Function: char * getenv (const char *name)
This function returns a string that is the value of the environment variable name. You must not modify this string. In some non-Unix systems not using the GNU library, it might be overwritten by subsequent calls to getenv (but not by any other library function). If the environment variable name is not defined, the value is a null pointer.

Function: int putenv (const char *string)
The putenv function adds or removes definitions from the environment. If the string is of the form `name=value', the definition is added to the environment. Otherwise, the string is interpreted as the name of an environment variable, and any definition for this variable in the environment is removed.

This function is part of the extended Unix interface. Since it was also available in old SVID libraries you should define either _XOPEN_SOURCE or _SVID_SOURCE before including any header.

Function: int setenv (const char *name, const char *value, int replace)
The setenv function can be used to add a new definition to the environment. The entry with the name name is replaced by the value `name=value'. Please note that this is also true if value is the empty string. A null pointer for the value parameter is illegal. If the environment already contains an entry with key name the replace parameter controls the action. If replace is zero, nothing happens. otherwise the old entry is replaced by the new one.

Please note that you cannot remove an entry completely using this function.

This function is part of the BSD library. The GNU C Library provides this function for compatibility but it may not be available on other systems.

Function: void unsetenv (const char *name)
Using this function one can remove an entry completely from the environment. If the environment contains an entry with the key name this whole entry is removed. A call to this function is equivalent to a call to putenv when the value part of the string is empty.

This function is part of the BSD library. The GNU C Library provides this function for compatibility but it may not be available on other systems.

There is one more function to modify the whole environment. This function is said to be used in the POSIX.9 (POSIX bindings for Fortran 77) and so one should expect it did made it into POSIX.1. But this never happened. But we still provide this function as a GNU extension to enable writing standard compliant Fortran environments.

Function: int clearenv (void)
The clearenv function removes all entries from the environment. Using putenv and setenv new entries can be added again later.

If the function is successful it returns 0. Otherwise the return value is nonzero.

You can deal directly with the underlying representation of environment objects to add more variables to the environment (for example, to communicate with another program you are about to execute; see section Executing a File).

Variable: char ** environ
The environment is represented as an array of strings. Each string is of the format `name=value'. The order in which strings appear in the environment is not significant, but the same name must not appear more than once. The last element of the array is a null pointer.

This variable is declared in the header file `unistd.h'.

If you just want to get the value of an environment variable, use getenv.

Unix systems, and the GNU system, pass the initial value of environ as the third argument to main. See section Program Arguments.


Go to the first, previous, next, last section, table of contents.