ibm-information-center/dist/eclipse/plugins/i5OS.ic.apiref_5.4.0.1/apiexusvar.htm

138 lines
6.3 KiB
HTML
Raw Permalink Normal View History

2024-04-02 14:02:31 +00:00
<?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: Using environment variables" />
<meta name="abstract" content="This program displays the value of an environment variable and then sets the environment variable to a new value." />
<meta name="description" content="This program displays the value of an environment variable and then sets the environment variable to a new value." />
<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="apiexusvar" />
<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: Using environment variables</title>
</head>
<body id="apiexusvar"><a name="apiexusvar"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using environment variables</h1>
<div><p>This program displays the value of an environment variable and
then sets the environment variable to a new value.</p>
<div class="section"><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
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.</p>
<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>
<pre>/******************************************************************/
/******************************************************************/
/* */
/* 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 &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;
#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) &amp;&amp; (argc != 3)) {
printf("Usage: %s &lt;ENV_VAR&gt; &lt;new_value&gt;\n OR \n"
"Usage: %s &lt;ENV_VAR&gt;\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 &gt;= BUFLEN) {
printf("Only 1024 characters total allowed for this test\n");
exit(1);
}
memcpy(envstring, argv[1], l1);
envstring[l1] = '=';
memcpy(&amp;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 &lt; 0) {
printf("putenv failed, errno = %d\n", errno);
return -1;
}
printf("Environment variable set\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>