|
|
HP-UX Reference > Ssetenv(3C)HP-UX 11i Version 2: December 2007 Update |
|
NAMEsetenv(), unsetenv() — add, update or remove an environment variable SYNOPSIS#include <stdlib.h>
int setenv(const char *envname, const char *envval, int overwrite);
int unsetenv(const char *envname); Parameters
DESCRIPTIONTo use these functions, compile with the option -I/usr/include/ep and link in the libcext library by specifying -lcext, for example: cc -I/usr/include/ep [flag]... cfile[...] -lcext [...] setenv() and unsetenv() functions update the environment of the calling process. If envname does not exist, the setenv() function ignores the value of overwrite and adds the variable envname to the environment with the value envval. If envname exists and overwrite is non-zero, then the variable envname is updated with the new value envval. If envname exists and overwrite is zero, then the variable is not updated, and the function is considered to have completed successfully. The unsetenv() function deletes the variable envname from the environment, if it exists. If the envname variable does not exist in the current environment, the environment is unchanged, and the function is considered to have completed successfully. If the application modifies environ or the pointers to which it points, the behavior of setenv() and unsetenv() is undefined. EXTERNAL INFLUENCESRETURN VALUEThe setenv() and unsetenv() functions return zero on success; otherwise they return -1 and set errno to indicate the error. ERRORSIf the setenv() function fails, errno is set to one of the following values:
If the unsetenv() function fails, errno is set to the following value:
EXAMPLESThe following code adds a new environment variable NEWHOME to the current environment. setenv("NEWHOME","/tmp/HOME",1); The following code updates the variable HOME. const char *envname="HOME"; const char *envval="/tmp/home"; setenv(envname,envval,1); The following code removes the variable NEWHOME from the current environment. unsetenv("NEWHOME"); WARNINGSThe setenv() and unsetenv() functions manipulate the environment pointed to by environ, and can be used in conjunction with getenv(). However, envp (the third argument to main) is not changed. The setenv() uses malloc() to enlarge the environment (see malloc(3C)). After the setenv() or unsetenv() function is called, environment variables may not be in alphabetical order. Users of setenv() and unsetenv() should note that the libcext library will not be available in future releases of HP-UX. However these functions will be available as part of the C library libc. |
|