Example: Saving and restoring system-level environment variables

These ILE C programs show how to save the current set of system-level environment variables and restore them later.

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

Saving system-level environment variables

This program saves the system-level environment variables and the associated CCSIDs in a file for restoring later.

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

Call this program with one parameter (the file to store the variable list and the CCSIDs).

/******************************************************************/
/******************************************************************/
/*                                                                */
/* FUNCTION:  Save the system-level environment variable list     */
/*            and the CCSIDs in a file                            */
/*                                                                */
/* LANGUAGE:  ILE C                                               */
/*                                                                */
/* APIs USED: Qp0zGetAllSysEnv()                                  */
/*                                                                */
/******************************************************************/
/******************************************************************/

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <qp0z1170.h>


int main(int argc, char *argv[])
{

    int fd, bw, rc;
    int listBufSize, ccsidBufSize, *ccsidBuf;
    char *listBuf;
    int numvar, sl, sc;

    if(argc != 2)
    {
      printf("Usage: call %s <filename>\n",argv[0]);
      printf("Example: call %s '/tmp/sev'\n",argv[0]);
      return -1;
    }

    sl = listBufSize = 1000;
    sc = ccsidBufSize = 1000;
    listBuf = (char *)malloc(listBufSize);
    ccsidBuf = (int *)malloc(ccsidBufSize);

    /* Create a file of specified name */
    /* If it exists, it is cleared out */
    /* Opened for writing              */
    fd = open(argv[1], O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
    if(fd == -1)
    {
      printf("open() failed. errno = %d\n", errno);
      return -1;
    }


    rc = Qp0zGetAllSysEnv(listBuf, &listBufSize, ccsidBuf,
                          &ccsidBufSize, NULL);
    if(rc != 0)
    {
      /* If there are no variables to save, write a  */
      /* zero into the file and return success       */

      if(rc == ENOENT)
      {
        numvar = 0;
        bw = write(fd, &numvar, sizeof(int));
        close(fd);
        printf("No system-level environment variables to save");
        return 0;
      }

      if(rc != ENOSPC)
      {
        printf("Error using  Qp0zGetAllSysEnv(), errno = %d\n", rc);
        return -1;
      }

      /* rc = ENOSPC. size of buffer is not enough */
      /* change buffer size and try again */

      /* If listBuf is not large enough, */
      /* allocate more space             */
      if(listBufSize > sl)
      {
        listBuf = (char *)realloc(listBuf, listBufSize);
      }

      /* If ccsidBuf is too small, allocate */
      /* more space                         */
      if(ccsidBufSize > sc)
      {
        ccsidBuf = (int *)realloc(ccsidBuf, ccsidBufSize);
      }

      rc = Qp0zGetAllSysEnv(listBuf, &listBufSize,
                            ccsidBuf, &ccsidBufSize, NULL);
      if(rc != 0)
      {
        printf("Error using Qp0zGetAllSysEnv(), errno = %d\n", rc);
        return -1;
      }
    }


    /* Write the contents of the buffer into the file */
    /* First write the total number of ccsid values   */
    /* This is the total number of variables          */

    numvar = ccsidBufSize/sizeof(int);

    bw = write(fd, &numvar, sizeof(int));
    if(bw == -1)
    {
      printf("write() of total number of ccsids failed. errno = %d\n", errno);
      return -1;
    }


    /* Next write the ccsid values */

    bw = write(fd, ccsidBuf, ccsidBufSize);
    if(bw == -1)
    {
      printf("write() of ccsid values failed. errno = %d\n", errno);
      return -1;
    }

    /* Now write the size (in bytes) of the listBuf */

    bw = write(fd, &listBufSize, sizeof(int));
    if(bw == -1)
    {
      printf("write() of listBufSize failed. errno = %d\n", errno);
      return -1;
    }

    /* Finally write the listBuf containing the variable strings*/

    bw = write(fd, listBuf, listBufSize);
    if(bw == -1)
    {
      printf("write() of listBuf failed. errno = %d\n", errno);
      return -1;
    }

    /* Close the file */
    rc = close(fd);
    if(rc != 0)
    {
      printf("close() failed. errno = %d\n", errno);
      return -1;
    }

    printf("System-level environment variables saved\n");
    return 0;
}

Restoring system-level environment variables

This program reads the system-level environment variable list from a file and then sets the system-level environment variables.

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

Call this program with one parameter (the name of the file in which the system-level environment variables were stored).

/******************************************************************/
/******************************************************************/
/*                                                                */
/* FUNCTION:  Restore the system-level environment variable list  */
/*            and the associated CCSIDs stored in a file          */
/*                                                                */
/* LANGUAGE:  ILE C                                               */
/*                                                                */
/* APIs USED: Qp0zPutSysEnv()                                     */
/*                                                                */
/******************************************************************/
/******************************************************************/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <qp0z1170.h>


int main(int argc, char *argv[])
{
    int fd, rc, br, i, numvar;
    int ccsidBufSize = 0, listBufSize = 0, *ccsidBuf;
    char *listBuf;


    if (argc != 2)
    {
      printf("Usage: call %s <filename>\n",argv[0]);
      printf("Example: call %s '/tmp/sev'\n",argv[0]);
      return -1;
    }

    /* Open the file specified */

    fd = open(argv[1], O_RDONLY);
    if(fd == -1)
    {
      printf("open() failed. errno = %d\n", errno);
      return -1;
    }

    /* Get the number of variables */
    br = read(fd, &numvar, sizeof(int));
    if(br == -1)
    {
      printf("read() failed. errno = %d\n", errno);
      return -1;
    }

    /* Could delete the existing system-level environment */
    /* variables and have only the restored values.       */
    /* If so desired, could call Qp0zDltSysEnv() to do so */

    /* If there aren't any elements in the file, skip the rest of */
    /* the reads and go to the end                                */

    if(numvar > 0)
    {
      ccsidBufSize = numvar*sizeof(int);
      ccsidBuf = (int *)malloc(ccsidBufSize);

      /* Read the ccsid values and put it in ccsidBuf */
      br = read(fd, ccsidBuf, ccsidBufSize);
      if(br == -1)
      {
        printf("read() failed. errno = %d\n", errno);
        return -1;
      }


      /* Read the size of the list buffer and put it in listBufSize */
      br = read(fd, &listBufSize, sizeof(int));
      if(br == -1)
      {
        printf("read() failed. errno = %d\n", errno);
        return -1;
      }

      listBuf = (char *)malloc(listBufSize);

      /* Finally read the strings themselves */
      br = read(fd, listBuf, listBufSize);
      if(br == -1)
      {
        printf("read() failed. errno = %d\n", errno);
        return -1;
      }
   }

   /* Walk through the buffer and get the   */
   /* name=value strings one by one         */
   /* Use Qp0zPutSysEnv() to set the values */

   for(i = 0; i < numvar; i++)
   {
     rc = Qp0zPutSysEnv(listBuf, ccsidBuf[i], NULL);
     if(rc != 0)
     {
       printf("Qp0zPutSysEnv() failed. rc=%d\n",rc);
       return -1;
     }

     listBuf += strlen(listBuf) + 1;
   }

   close(fd);
   printf("System-level environment variables restored\n");
   return 0;

}