327 lines
11 KiB
HTML
327 lines
11 KiB
HTML
<?xml version="1.0" encoding="UTF-8"?>
|
|
<!DOCTYPE html
|
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html lang="en-us" xml:lang="en-us">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="security" content="public" />
|
|
<meta name="Robots" content="index,follow" />
|
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
|
<meta name="DC.Type" content="reference" />
|
|
<meta name="DC.Title" content="Example: Saving and restoring system-level environment variables" />
|
|
<meta name="abstract" content="This two-part example illustrates how to save the current set of system-level environment variables and restore them later." />
|
|
<meta name="description" content="This two-part example illustrates how to save the current set of system-level environment variables and restore them later." />
|
|
<meta name="DC.Relation" scheme="URI" content="apiexmp.htm" />
|
|
<meta name="copyright" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1998, 2006" />
|
|
<meta name="DC.Format" content="XHTML" />
|
|
<meta name="DC.Identifier" content="apiexsavvar" />
|
|
<meta name="DC.Language" content="en-us" />
|
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
|
<!-- US Government Users Restricted Rights -->
|
|
<!-- Use, duplication or disclosure restricted by -->
|
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
|
<title>Example: Saving and restoring system-level environment variables</title>
|
|
</head>
|
|
<body id="apiexsavvar"><a name="apiexsavvar"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: Saving and restoring system-level environment variables</h1>
|
|
<div><p>This two-part example illustrates how to save the current set of
|
|
system-level environment variables and restore them later.</p>
|
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm">Code license and disclaimer information</a> for important
|
|
legal information.</div>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Saving system-level environment variables</h4><p>This program
|
|
stores the system-level environment variables and the associated CCSIDs in
|
|
a file for restoring later.</p>
|
|
<p>Use the Create C Module (CRTCMOD) and the
|
|
Create Program (CRTPGM) commands to create this program.</p>
|
|
<p>Call this program
|
|
with one parameter (the file to store the variable list and the CCSIDs).</p>
|
|
<pre>/******************************************************************/
|
|
/******************************************************************/
|
|
/* */
|
|
/* 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;
|
|
}</pre>
|
|
</div>
|
|
<div class="section"><h4 class="sectiontitle">Restoring system-level environment variables</h4><p>This
|
|
program reads the system-level environment variable list from a file and then
|
|
sets the system-level environment variables.</p>
|
|
<p>Use the Create C Module
|
|
(CRTCMOD) and the Create Program (CRTPGM) commands to create this program.</p>
|
|
<p>Call
|
|
this program with one parameter (the name of the file in which the system-level
|
|
environment variables were stored).</p>
|
|
<pre>/******************************************************************/
|
|
/******************************************************************/
|
|
/* */
|
|
/* 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;
|
|
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="apiexmp.htm" title="Contains example programs that use APIs and exit programs.">Examples: APIs</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |