COHERENT manpages

This page displays the COHERENT manpage for putenv() [Add a string to the environment].

List of available manpages
Index


putenv() -- General Function (libc)

Add a string to the environment
#include <stdlib.h>
int putenv (envstring)
char *envstring;

The function putenv() puts  envstring into the user's environment.  You can
use this  function to set  a new environmental  variable, or to  change the
definition of an existing variable.

envstring must point to a string of the form VARIABLE=value, where VARIABLE
is the environmental variable being set, and value is the value to which it
is being set.

putenv()  returns zero  if  all goes  well.   If something  goes wrong,  it
returns a value other than zero.

See Also

environ,
environmental variables,
getenv(),
libc,
stdlib.h

Notes

The  global variable  environ,  which points  to  a process's  environment,
points  to an  array of  pointers  to strings  rather than  to an  array of
strings.  When  putenv() inserts envstring  into the environment,  it calls
malloc() to  enlarge the array of string pointers  to which environ points,
then inserts  a pointer  to envstring  into that array.   It does  not copy
envstring anywhere.

If a process uses putenv() to insert a string pointer into the environment,
it can also  call getenv() to read back that  string; however, the array of
strings passed to the process via  envp (the third argument to the function
main()) is not  affected by a call to putenv().  For details on environ and
envp, see their entries in the Lexicon.

It is an error to call  putenv() with a pointer to an automatic variable as
the argument,  and then exit the calling function  while envstring is still
part of  the environment.  For  safety's sake, envstring should  point to a
string that is static or global.   See the Lexicon entry for static, or see
the ANSI Standard §3.5.1.