RAD Studio |  |

putenv, _wputenv

Header File

stdlib.h

Category

Process Control Routines

Prototype

int putenv(const char *name);

int _wputenv(const wchar_t *name);

Description

Adds string to current environment.

putenv accepts the string name and adds it to the environment of the current process. For example,

putenv(“PATH=C:\\BC”);

putenv can also be used to modify an existing name. name can be either uppercase or lowercase. name must not include the equal sign (=). You can set a variable to an empty value by specifying an empty string on the right side of the ‘=’ sign.

putenv can be used only to modify the current program’s _environment. Once the program ends, the old _environment is restored. The _environment of the current process is passed to child processes, including any changes made by putenv.

Note that the string given to putenv must be static or global. Unpredictable results will occur if a local or dynamic string given to putenv is used after the string memory is released.

Return Value

On success, putenv returns 0; on failure, -1.

Example

Copy Code

    #include <stdio.h>
    #include <stdlib.h>
    #include <alloc.h>
    #include <string.h>
    int main(void)
    {
       char *path, *ptr;
       int i = 0;
       /* get the current path environment */
       ptr = getenv("PATH");
       /* set up new path */
       path = (char *) malloc(strlen(ptr)+15);
       strcpy(path,"PATH=");
       strcat(path,ptr);
       strcat(path,";c:\\temp");
       /* replace the current path and display current environment */
       putenv(path);
       while (_environ[i])
           printf("%s\n",_environ[i++]);
       return 0;
    }

Portability

| POSIX | Win32 | ANSI C | ANSI C++
putenv |  | + |  |
_wputenv |  | NT only |  |

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!