getenv_s, wgetenv_s

From RAD Studio
Jump to: navigation, search

Go Up to stdlib.h Index


Header File

stdlib.h

Category

Communication with the environment

Prototype

errno_t getenv_s(size_t * restrict len, char * restrict value, rsize_t maxsize, const char * restrict name);

errno_t wgetenv_s(size_t * restrict len, wchar_t * restrict value, rsize_t maxsize, const wchar_t * restrict name);

Description

The getenv_s function is used to access the value of an environment variable specified by name, store that value in the string pointed to by value, and set the length of the string in len, only if the value's length is smaller than or equal to maxsize.

The run-time constraints imposed on the function are that name and value must not be null and maxsize must not be zero or greater than RSIZE_MAX.

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

getenv_s sets the value of a specified variable in the character array pointed to by value. name can be either uppercase or lowercase. name must not include the equal sign (=). If the specified environment variable does not exist, getenv_s sets zero to the integer pointed to by len and sets null to value[0].

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

Return Value

On success, getenv_s returns 0, otherwise a nonzero value is returned.

Example

#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>
int main(void)
{
  char ptr[80];
  char *path;
  int i = 0;
  unsigned int len;
  /* Get the current path environment */
  getenv_s(&len, ptr, 80,"PATH");
  /* Set up new path */
  path = (char *) malloc(len+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("%s\n",_environ[i++]);
  return 0;
}

Portability

POSIX Win32 ANSI C ANSI C++

getenv_s

+

+

+

+

See Also