ibm-information-center/dist/eclipse/plugins/i5OS.ic.ifs_5.4.0.1/rzaaxcfunctionexample.htm

396 lines
15 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: Integrated file system C functions" />
<meta name="abstract" content="This simple C language program illustrates the use of several integrated file system functions." />
<meta name="description" content="This simple C language program illustrates the use of several integrated file system functions." />
<meta name="DC.subject" content="API, example program, authority, in example program, C language program, example, directory, in example program, program using integrated file system APIs, functions, link, in example program, stream file, file, operations (example program), integrated file system, programming interfaces" />
<meta name="keywords" content="API, example program, authority, in example program, C language program, example, directory, in example program, program using integrated file system APIs, functions, link, in example program, stream file, file, operations (example program), integrated file system, programming interfaces" />
<meta name="DC.Relation" scheme="URI" content="rzaaxpgmifs.htm" />
<meta name="DC.Relation" scheme="URI" content="rzaaxprogapi.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 1999, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rzaaxcfunctionexample" />
<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: Integrated file system C functions</title>
</head>
<body id="rzaaxcfunctionexample"><a name="rzaaxcfunctionexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Integrated file system C functions</h1>
<div><p>This simple C language program illustrates the use of several integrated
file system functions.</p>
<div class="section"><p>The program performs the following operations:</p>
<dl><dt class="dlterm"><span>1</span></dt>
<dd>Uses the <span class="apiname">getuid()</span> function to determine the real user
ID (uid).</dd>
<dt class="dlterm"><span>2</span></dt>
<dd>Uses the <span class="apiname">getcwd() </span>function to determine the current
directory.</dd>
<dt class="dlterm"><span>3</span></dt>
<dd>Uses the <span class="apiname">open()</span> function to create a file. It establishes
read, write, and execute authority to the file for the owner (the person who
created the file).</dd>
<dt class="dlterm"><span>4</span></dt>
<dd>Uses the <span class="apiname">write()</span> function to write a byte string to
the file. The file descriptor that was provided in the open operation (<span>3</span>),
identifies the file.</dd>
<dt class="dlterm"><span>5</span></dt>
<dd>Uses the <span class="apiname">close()</span> function to close the file.</dd>
<dt class="dlterm"><span>6</span></dt>
<dd>Uses the <span class="apiname">mkdir()</span> function to create a new subdirectory
in the current directory. The owner is given read, write, and execute access
to the subdirectory.</dd>
<dt class="dlterm"><span>7</span></dt>
<dd>Uses the <span class="apiname">chdir()</span> function to change the new subdirectory
to the current directory.</dd>
<dt class="dlterm"><span>8</span></dt>
<dd>Uses the <span class="apiname">link()</span> function to create a link to the file
that was previously created (<span>3</span>).</dd>
<dt class="dlterm"><span>9</span></dt>
<dd>Uses the <span class="apiname">open()</span> function to open the file for read
only. The link that was created in (<span>8</span>) allows access to the file.</dd>
<dt class="dlterm"><span>10</span></dt>
<dd>Uses the <span class="apiname">read()</span> function to read a byte string from
the file. The file descriptor that was provided in the open operation (<span>9</span>)
identifies the file.</dd>
<dt class="dlterm"><span>11</span></dt>
<dd>Uses the <span class="apiname">close()</span> function to close the file.</dd>
<dt class="dlterm"><span>12</span></dt>
<dd>Uses the <span class="apiname">unlink()</span> function to remove the link to the
file.</dd>
<dt class="dlterm"><span>13</span></dt>
<dd>Uses the <span class="apiname">chdir()</span> function to change the current directory
back to the parent directory in which the new subdirectory was created.</dd>
<dt class="dlterm"><span>14</span></dt>
<dd>Uses the <span class="apiname">rmdir()</span> function to remove the subdirectory
that was previously created (<span>6</span>).</dd>
<dt class="dlterm"><span>15</span></dt>
<dd>Uses the <span class="apiname">unlink()</span> function to remove the file that
was previously created (<span>3</span>).</dd>
</dl>
<div class="note"><span class="notetitle">Note:</span> This sample program will run correctly on systems where
the CCSID of the job in which it is run is 37. The integrated file system
APIs must have the object and path names encoded in the job's CCSID; however,
the C compiler stores character constants in CCSID 37. For complete compatibility,
translate character constants, such as object and path names, before passing
APIs to the job's CCSID.</div>
</div>
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples,
you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
<pre>
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#define BUFFER_SIZE 2048
#define NEW_DIRECTORY "testdir"
#define TEST_FILE "test.file"
#define TEST_DATA "Hello World!"
#define USER_ID "user_id_"
#define PARENT_DIRECTORY ".."
char InitialFile[BUFFER_SIZE];
char LinkName[BUFFER_SIZE];
char InitialDirectory[BUFFER_SIZE] = ".";
char Buffer[32];
int FilDes = -1;
int BytesRead;
int BytesWritten;
uid_t UserID;
void CleanUpOnError(int level)
{
printf("Error encountered, cleaning up.\n");
switch ( level )
{
case 1:
printf("Could not get current working directory.\n");
break;
case 2:
printf("Could not create file %s.\n",TEST_FILE);
break;
case 3:
printf("Could not write to file %s.\n",TEST_FILE);
close(FilDes);
unlink(TEST_FILE);
break;
case 4:
printf("Could not close file %s.\n",TEST_FILE);
close(FilDes);
unlink(TEST_FILE);
break;
case 5:
printf("Could not make directory %s.\n",NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 6:
printf("Could not change to directory %s.\n",NEW_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 7:
printf("Could not create link %s to %s.\n",LinkName,InitialFile);
chdir(PARENT_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 8:
printf("Could not open link %s.\n",LinkName);
unlink(LinkName);
chdir(PARENT_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 9:
printf("Could not read link %s.\n",LinkName);
close(FilDes);
unlink(LinkName);
chdir(PARENT_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 10:
printf("Could not close link %s.\n",LinkName);
close(FilDes);
unlink(LinkName);
chdir(PARENT_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 11:
printf("Could not unlink link %s.\n",LinkName);
unlink(LinkName);
chdir(PARENT_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 12:
printf("Could not change to directory %s.\n",PARENT_DIRECTORY);
chdir(PARENT_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 13:
printf("Could not remove directory %s.\n",NEW_DIRECTORY);
rmdir(NEW_DIRECTORY);
unlink(TEST_FILE);
break;
case 14:
printf("Could not unlink file %s.\n",TEST_FILE);
unlink(TEST_FILE);
break;
default:
break;
}
printf("Program ended with Error.\n"\
"All test files and directories may not have been removed.\n");
}
int main ()
{
<span><span>1</span></span>
/* Get and print the real user id with the getuid() function. */
UserID = getuid();
printf("The real user id is %u. \n",UserID);
<span><span>2</span></span>
/* Get the current working directory and store it in InitialDirectory. */
if ( NULL == getcwd(InitialDirectory,BUFFER_SIZE) )
{
perror("getcwd Error");
CleanUpOnError(1);
return 0;
}
printf("The current working directory is %s. \n",InitialDirectory);
<span><span>3</span></span>
/* Create the file TEST_FILE for writing, if it does not exist.
Give the owner authority to read, write, and execute. */
FilDes = open(TEST_FILE, O_WRONLY | O_CREAT | O_EXCL, S_IRWXU);
if ( -1 == FilDes )
{
perror("open Error");
CleanUpOnError(2);
return 0;
}
printf("Created %s in directory %s.\n",TEST_FILE,InitialDirectory);
<span><span>4</span></span>
/* Write TEST_DATA to TEST_FILE via FilDes */
BytesWritten = write(FilDes,TEST_DATA,strlen(TEST_DATA));
if ( -1 == BytesWritten )
{
perror("write Error");
CleanUpOnError(3);
return 0;
}
printf("Wrote %s to file %s.\n",TEST_DATA,TEST_FILE);
<span><span>5</span></span>
/* Close TEST_FILE via FilDes */
if ( -1 == close(FilDes) )
{
perror("close Error");
CleanUpOnError(4);
return 0;
}
FilDes = -1;
printf("File %s closed.\n",TEST_FILE);
<span><span>6</span></span>
/* Make a new directory in the current working directory and
grant the owner read, write and execute authority */
if ( -1 == mkdir(NEW_DIRECTORY, S_IRWXU) )
{
perror("mkdir Error");
CleanUpOnError(5);
return 0;
}
printf("Created directory %s in directory %s.\n",NEW_DIRECTORY,InitialDirectory);
<span><span>7</span></span>
/* Change the current working directory to the
directory NEW_DIRECTORY just created. */
if ( -1 == chdir(NEW_DIRECTORY) )
{
perror("chdir Error");
CleanUpOnError(6);
return 0;
}
printf("Changed to directory %s/%s.\n",InitialDirectory,NEW_DIRECTORY);
/* Copy PARENT_DIRECTORY to InitialFile and
append "/" and TEST_FILE to InitialFile. */
strcpy(InitialFile,PARENT_DIRECTORY);
strcat(InitialFile,"/");
strcat(InitialFile,TEST_FILE);
/* Copy USER_ID to LinkName then append the
UserID as a string to LinkName. */
strcpy(LinkName, USER_ID);
sprintf(Buffer, "%d\0", (int)UserID);
strcat(LinkName, Buffer);
<span><span>8</span></span>
/* Create a link to the InitialFile name with the LinkName. */
if ( -1 == link(InitialFile,LinkName) )
{
perror("link Error");
CleanUpOnError(7);
return 0;
}
printf("Created a link %s to %s.\n",LinkName,InitialFile);
<span><span>9</span></span>
/* Open the LinkName file for reading only. */
if ( -1 == (FilDes = open(LinkName,O_RDONLY)) )
{
perror("open Error");
CleanUpOnError(8);
return 0;
}
printf("Opened %s for reading.\n",LinkName);
<span><span>10</span></span>
/* Read from the LinkName file, via FilDes, into Buffer. */
BytesRead = read(FilDes,Buffer,sizeof(Buffer));
if ( -1 == BytesRead )
{
perror("read Error");
CleanUpOnError(9);
return 0;
}
printf("Read %s from %s.\n",Buffer,LinkName);
if ( BytesRead != BytesWritten )
{
printf("WARNING: the number of bytes read is "\
"not equal to the number of bytes written.\n");
}
<span><span>11</span></span>
/* Close the LinkName file via FilDes. */
if ( -1 == close(FilDes) )
{
perror("close Error");
CleanUpOnError(10);
return 0;
}
FilDes = -1;
printf("Closed %s.\n",LinkName);
<span><span>12</span></span>
/* Unlink the LinkName link to InitialFile. */
if ( -1 == unlink(LinkName) )
{
perror("unlink Error");
CleanUpOnError(11);
return 0;
}
printf("%s is unlinked.\n",LinkName);
<span><span>13</span></span>
/* Change the current working directory
back to the starting directory. */
if ( -1 == chdir(PARENT_DIRECTORY) )
{
perror("chdir Error");
CleanUpOnError(12);
return 0;
}
printf("changing directory to %s.\n",InitialDirectory);
<span><span>14</span></span>
/* Remove the directory NEW_DIRECTORY */
if ( -1 == rmdir(NEW_DIRECTORY) )
{
perror("rmdir Error");
CleanUpOnError(13);
return 0;
}
printf("Removing directory %s.\n",NEW_DIRECTORY);
<span><span>15</span></span>
/* Unlink the file TEST_FILE */
if ( -1 == unlink(TEST_FILE) )
{
perror("unlink Error");
CleanUpOnError(14);
return 0;
}
printf("Unlinking file %s.\n",TEST_FILE);
printf("Program completed successfully.\n");
return 0;
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="rzaaxpgmifs.htm" title="The addition of the integrated file system to the iSeries server server in V3R1M0 did not affect existing iSeries server applications. The programming languages, utilities, and system support (such as data description specifications) operate in the same way as they did before the addition of the integrated file system.">Programming support</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="rzaaxprogapi.htm" title="Many of the application program interfaces (APIs) that perform operations on integrated file system objects are in the form of C language functions.">Perform operations using APIs</a></div>
</div>
</div>
</body>
</html>