getenv
environment variable functions
You're viewing an older version of this page (#3266). View the current version.
NAME
<span class="Nm" id="getenv">getenv</span>, <span class="Nm" id="putenv">putenv</span>, <span class="Nm" id="setenv">setenv</span>, <span class="Nm" id="unsetenv">unsetenv</span>
INTERFACE
#include <stdlib.h> char getenv ( const char *name ) int setenv ( const char *name, const char *value, int overwrite ) int putenv ( const char *string ) void unsetenv ( const char *name )
DESCRIPTION
These functions set, unset and fetch environment variables from the host <span class="Em">environment</span> <span class="Em">list</span>. For compatibility with differing environment conventions, the given arguments \c name and \c value may be appended and prepended, respectively, with an equal sign "<span class="Dq"><span class="Li">=</span>.</span>" \n \n The getenv function obtains the current value of the environment variable, \c name. \n \n The setenv function inserts or resets the environment variable \c name in the current environment list. If the variable \c name does not exist in the list, it is inserted with the given \c value. If the variable does exist, the argument \c overwrite is tested; if \c overwrite is zero, the variable is not reset, otherwise it is reset to the given \c value. \n \n The putenv function takes an argument of the form ``name=value'' and is equivalent to:
setenv(name, value, 1);\n \n The unsetenv function deletes all instances of the variable name pointed to by \c name from the list.
RETURN VALUES
The getenv function returns the value of the environment variable as a <span class="Dv">NUL</span> -terminated string. If the variable \c name is not in the current environment, <span class="Dv">NULL</span> is returned. \n \n <span class="Rv">-std</span> <span class="Rv">setenv</span> <span class="Rv">putenv</span>
ERRORS
<table cellspacing="0" class="refpage" style="margin-left:25px"> <tr> <td valign="top" nowrap> [<span class="Bq"><span class="Er">ENOMEM</span></span>] </td> <td valign="top"> The function setenv or putenv failed because they were unable to allocate memory for the environment. </td> </tr> </table>
SEE ALSO
reference:csh (1) , reference:sh (1) , reference:execve (2) , reference:environ (7)
STANDARDS
The getenv function conforms to
BUGS
Successive calls to setenv or putenv assigning a differently sized \c value to the same \c name will result in a memory leak. The FreeBSD semantics for these functions (namely, that the contents of \c value are copied and that old values remain accessible indefinitely) make this bug unavoidable. Future versions may eliminate one or both of these semantic guarantees in order to fix the bug.
HISTORY
The functions setenv and unsetenv appeared in Version 7 AT&T UNIX The putenv function appeared in RenoBSD