RAD Studio |  |

getenv, _wgetenv

Header File

stdlib.h

Category

Process Control Routines

Prototype

char *getenv(const char *name);

wchar_t *_wgetenv(const wchar_t *name);

Description

Find or delete an environment variable from the system environment.

The environment consists of a series of entries that are of the form name=string\0.

getenv returns the value of a specified variable. name can be either uppercase or lowercase. name must not include the equal sign (=). If the specified environment variable does not exist, getenv returns a NULL pointer.

To delete the variable from the environment, use getenv("name=").

Note: Environment entries must not be changed directly. If you want to change an environment value, you must use putenv.
Return Value

On success, getenv returns the value associated with name.

If the specified name is not defined in the environment, getenv returns a NULL pointer.

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++
getenv | + | + | + | +
_wgetenv |  | + |  |

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