Example: Using environment variables

This ILE C program displays the value of an environment variable and sets the environment variable to a new value.

Use the Create C Module (CRTCMOD) and the Create Program (CRTPGM) commands to create this program.

Call this program with one parameter to display the environment variable specified by that parameter. Call this program with two parameters to set the environment variable specified by the first parameter to the value specified by the second parameter.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

/******************************************************************/
/******************************************************************/
/*                                                                */
/* FUNCTION:  Display the value of an environment variable and    */
/*            then set the environment variable to a new value.   */
/*                                                                */
/* LANGUAGE:  ILE C                                               */
/*                                                                */
/* APIs USED: getenv(), putenv()                                  */
/*                                                                */
/******************************************************************/
/******************************************************************/

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

#define BUFLEN 1024

int main(int argc, char *argv[])
{
    int   num=0;              /* counter                           */
    int   rc;                 /* API return code                   */
    int   l1, l2;             /* lengths of the two parameters     */
    char  *envvar=NULL;       /* pointer to an environment variable*/
    char  **envvaridx=NULL;   /* pointer to an envvar pointer      */
    char  envstring[BUFLEN];
                              /* buffer to construct putenv request*/

                              /* Show a small usage message.       */
    if ((argc != 2) && (argc != 3)) {
        printf("Usage: %s <ENV_VAR> <new_value>\n OR \n"
               "Usage: %s <ENV_VAR>\n", argv[0], argv[0]);
        printf("Sets an environment variable to a user requested\n"
            "value\n"
            "OR\nDisplays the value of a single environment variable\n");
        exit(1);
      }

    if (argc == 2) {
        /* Called just to display the environment variables.       */
        envvar = getenv(argv[1]);
        if (envvar == NULL) {
            printf("No environment variable %s set\n",
                   argv[1]);
          }
        else {
            printf("Environment variable: %s\n", envvar);
          }
        return 0;
      }

    /* ELSE, called to set an environment variable.                */

    /* Check the size of the parameters and construct a string of  */
    /* the form "VAR=string" which is valid input to putenv.       */
    l1 = strlen(argv[1]);
    l2 = strlen(argv[2]);
    if (l1+l2+2 >= BUFLEN) {
        printf("Only 1024 characters total allowed for this test\n");
        exit(1);
      }
    memcpy(envstring, argv[1], l1);
    envstring[l1] = '=';
    memcpy(&envstring[l1+1], argv[2], l2);
    envstring[l1+l2+1]='\0';

    /* Now that the string is built, let's see if the environment  */
    /* variable was already set.                                   */
    envvar = getenv(argv[1]);
    if (envvar == NULL) {
        printf("Setting NEW environment variable %s\n",
               envstring);
      }
    else {
        printf("Resetting OLD environment variable from: %s\n to %s\n",
               envvar, envstring);
      }

    /* Now actually set the environment variable.                  */
    rc = putenv(envstring);
    if (rc < 0) {
        printf("putenv failed, errno = %d\n", errno);
        return -1;
      }
    printf("Environment variable set\n");
    return 0;
}