[Previous]
[Contents]
[Next]

getenv()

get an environment variable

Synopsis:

#include <stdlib.h>

char *getenv( const char *name );

Description:

The getenv() function searches the environment list for an entry matching the string pointed to by name. The matching is case-sensitive; all lowercase letters are treated as different from uppercase letters.

Entries can be added to the environment list with the export shell command, the env utility, or with the putenv() or setenv() functions. All entries in the environment list can be displayed by using the export shell command or env utility with no arguments.

To assign a string to a variable and place it in the environment list, type a command such as the following:

export INCLUDE=/usr/include

To see what variables are in the environment list, as well as their current assignments, type:

export

The output will be something like the following:

    SHELL=ksh
    TERM=qnx
    LOGNAME=fred
    PATH=:/bin:/usr/bin
    HOME=/home/fred
    INCLUDE=/usr/include
    LIB=/usr/lib

Returns:

A pointer to the string assigned to the environment variable if found, or NULL if no match was found.


Note: The value returned should be duplicated if you intend to modify the contents of the string.

Examples:

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

int main( void )
  {
    char *path;

    path = getenv( "INCLUDE" );
    if( path != NULL )
      printf( "INCLUDE=%s\n", path );
    return( EXIT_SUCCESS );
  }

Classification:

ANSI

Safety:
Interrupt handler No
Signal handler No
Thread No

See also:

clearenv(), exec... functions, putenv(), _searchenv(), searchenv(), setenv(), spawn... functions, system()


[Previous]
[Contents]
[Next]