Qp0zDltEnv()--Delete an Environment Variable


  Syntax
 #include <qp0z1170.h>

 int Qp0zDltEnv(const char *name);  

  Service Program Name: QP0ZCPA

  Default Public Authority: *USE

  Threadsafe: Yes. See Usage Notes for more information.

The Qp0zDltEnv() function deletes a single job-level environment variable or deletes all environment variables from the current job. If the name parameter is NULL, all environment variables in the job are deleted.

The name parameter does not include the equal (=) symbol or the value of the environment variable name=value pair.


Parameters

name
(Input) A pointer to the name part of the environment variable name=value string.

Authorities

None.


Return Value

0 Qp0zDltEnv() was successful.
-1 Qp0zDltEnv() was not successful. The errno variable is set to indicate the error.


Error Conditions

If Qp0zDltEnv() is not successful, errno indicates one of the following errors.

[ENOENT]

No such path or directory.

The directory or a component of the path name specified does not exist.

A named file or directory does not exist or is an empty string.

The parameter name is not NULL and does not point to an environment variable name that currently exists in the environment list.


Usage Notes

  1. Although Qp0zDltEnv() is threadsafe, if a thread calls an environment variable function while another thread is accessing an environment variable from the environ array the thread may see undefined results. The environ array can be accessed directly or by using a pointer returned from the getenv() or Qp0zGetEnv() functions. The environment contents are only protected during calls to the environment variable functions.

Related Information


Example

See Code disclaimer information for information pertaining to code examples.

The following example uses Qp0zDltEnv(), putenv() and the environ array.



#include <stdio.h>
#include <errno.h>
#include <qp0z1170.h>
#include <stdlib.h>

extern char **environ;

#define ASSERT(x, y)                        \
{ if (!(x)) {                               \
    printf("Assertion Failed: " #x          \
           ", Description: " y              \
           ", errno=%d", errno);            \
    exit(EXIT_FAILURE);                     \
  }                                         \
}

int main(int argc, char **argv)
{
  int                   rc=0;
  int                   e=0;
  printf("Enter Testcase - %s\n", argv[0]);

  rc = putenv("PATH=/usr/bin:/home/me:%LIBL%");
  ASSERT((rc == 0), "putenv(PATH)");
  rc = putenv("TEST0=42");
  ASSERT((rc == 0), "putenv(TEST0)");
  rc = putenv("TEST1=42");
  ASSERT((rc == 0), "putenv(TEST1)");
  printf("Before delete, these environment variables are set: \n");

  while (environ[e] != NULL) {
    printf("  %s\n", environ[e]);
    ++e;
  }

  printf("Delete the environment variables\n");
  rc = Qp0zDltEnv("TEST0");
  ASSERT((rc==0), "Qp0zDltEnv(TEST0)");
  rc = Qp0zDltEnv("TEST1");
  ASSERT((rc==0), "Qp0zDltEnv(TEST1)");

  printf("After delete, these environment variables are set: \n");
  e=0;
  while (environ[e] != NULL) {
    printf("  %s\n", environ[e]);
    ++e;
  }
  printf("Main completed\n");
  return 0;
}

Output:


Enter Testcase - QP0WTEST/TPZDLTE0
Before delete, these environment variables are set:
  PATH=/usr/bin:/home/me:%LIBL%
  TEST0=42
  TEST1=42
Delete the environment variables
After delete, these environment variables are set:
  PATH=/usr/bin:/home/me:%LIBL%
Main completed



API introduced: V4R3
Top | UNIX-Type APIs | APIs by category