setenv

set one or more environment variables

Synopsis:

#include <env.h>
int setenv( const char *name,
        const char *newvalue,
        int overwrite );

Description:

The environment list consists of a number of environment names, each of which has a value associated with it.

The setenv() function searches the environment list for an entry of the form name=value. If no such string is present, setenv() adds an entry of the form name=newvalue to the environment list. Otherwise, if the overwrite argument is non-zero, setenv() either will change the existing value to newvalue, or will delete the string name=value and add the string name=newvalue.

If the newvalue pointer is NULL, all strings of the form name=value in the environment list will be deleted.

The value of the pointer environ may change across a call to the setenv() function.

The setenv() function makes copies of the strings associated with name and newvalue.

The matching is case-sensitive; all lowercase letters are treated as different from uppercase letters.

To assign a string to a variable and place it in the environment list, use the export command, as follows:

     export INCLUDE=/usr/include

To see what variables are in the environment list, and their current assignments, type the export command without any arguments. The output will be like the following:

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

Returns:

0
Success
non-zero
An error occurred. errno is set to indicate the error.

Errors:

ENOMEM
Not enough memory to allocate a new environment string.

See also:

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

Examples:

The following changes the string assigned to INCLUDE and then displays the new string.

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

void main()
  {
    char *path;

    if( setenv( "INCLUDE",
        "/usr/include:/home/fred/include", 
        1 ) == 0 )
      if( (path = getenv( "INCLUDE" )) != NULL )
        printf( "INCLUDE=%s\n", path );
  }

Classification:

POSIX 1003.1

Systems:

All (except Netware, DOS/PM)