setenv

Sets the value of an environment variable.

Library:LibC
Classification:ANSI
Service:Environment Variable

Syntax

  #include <stdlib.h> 
   
  int setenv (
     const char   *name,
     const char   *value,
     int           rewrite);
  

Parameters

name

(IN) Points to the name of an environment variable.

value

(IN) Points to a string that is the value to assign to the environment variable. If the string is "", this is the same as calling unsetenv.

rewrite

(IN) Indicates whether this new value should replace the existing value.

  • Zero An existing variable is not overwritten.
  • Nonzero An existing variable is overwritten.

Return Values

If successful, returns 0. Otherwise, returns -1, leaves the environment unchanged, and sets errno to one of the following:

Decimal

Constant

Description

5

ENOMEM

Insufficient memory to add the variable or its value to the environment.

7

EEXIST

The environment variable already exists and the rewrite parameter is set so that existing variables cannot be modified.

65

ENAMETOOLONG

The name for the environment variable is too long.

77

ESRCH

The environment variable was not found.

105

ENOCONTEXT

No thread context is present.

Remarks

If the environment variable already exists and the value of the rewrite parameter is zero, the setenv function returns -1 and errno is set to EEXIST.

See Also