ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzau8_5.4.0.1/ifsexamples.htm

226 lines
9.6 KiB
HTML
Raw 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="Examples: Integrated file system" />
<meta name="abstract" content="These examples can help you program your optical file system using the integrated file system." />
<meta name="description" content="These examples can help you program your optical file system using the integrated file system." />
<meta name="DC.Relation" scheme="URI" content="ifs.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2000, 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2000, 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="ifsexamples" />
<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>Examples: Integrated file system</title>
</head>
<body id="ifsexamples"><a name="ifsexamples"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Examples: Integrated file system</h1>
<div><p>These examples can help you program your optical file system using
the integrated file system.</p>
<div class="section"><p>This topic demonstrates the use of the integrated file system
UNIX-type APIs that pertain to the QOPT physical file system and are used
with the ILE C for i5/OS™ programming language.</p>
<div class="p">The
programming examples demonstrate the following functions: <ul><li>Retrieving optical directory entries</li>
<li>Creating an optical file</li>
<li>Writing a file</li>
<li>Closing a file</li>
<li>Opening a file</li>
<li>Reading a file</li>
<li>Changing the offset into a file</li>
</ul>
</div>
<p>For more information about UNIX-Type APIs, see the <a href="../apis/unix.htm">UNIX-Type APIs</a> topic in the iSeries™ Information
Center.</p>
</div>
<div class="example"><h4 class="sectiontitle">Sample code</h4>This sample program demonstrates the use
of various integrated file system APIs. <div class="note"><span class="notetitle">Note:</span> By using the following code
examples, you agree to the terms of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
<pre>/**********************************************************************/
/* */
/* This program demonstrates the use of various integrated file */
/* system functions applied to the QOPT physical file system */
/* including: */
/* chdir() - change current directory */
/* close() - close file */
/* closedir() - close directory */
/* creat() - create file */
/* lseek() - seek file (change file offset) */
/* open() - open file */
/* opendir() - open directory */
/* read() - read file */
/* readdir() - read directory entry */
/* rewinddir() - rewind directory entries */
/* stat() - directory statistics */
/* write() - write file */
/* */
/**********************************************************************/
#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;dirent.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;fcntl.h&gt;
void main (void)
{
/*****************************************************************/
/* local variables, contents and defines */
/*****************************************************************/
char path[294]; /* optical path */
DIR *dirP; /* pointer to the directory */
int filedes; /* open file descriptor */
struct dirent *direntP; /* directory entry structure */
struct stat info; /* dir/file information */
int volume_number; /* what it says... */
int rc = 0; /* function return codes */
int kk = 0; /* local counter */
char data[] = "The quick red fox jumped over the fence";
/*****************************************************************/
/* Retrieve the list of volumes from the QOPT physical file */
/* system by opening the QOPT pfs root directory and reading the */
/* directory entries. */
/*****************************************************************/
memset(path, /* clear path name */
0x00,
sizeof(path));
strcpy(path, /* set physical file system */
"/QOPT");
rc = stat("/QOPT", &amp;info);; /* determine number of files */
if (rc != 0)
perror("stat() failed:");
dirP = opendir(path); /* open the directory */
if (dirP == NULL)
perror("opendir() failed:");
for (kk = 1; kk &lt;= info.st_nlink; kk++)
{
direntP = readdir(dirP);
if (direntP == NULL)
perror("readdir() failed:");
printf("%d) %s\n", kk, direntP-&gt;d_name);
}
/*****************************************************************/
/* Prompt user for the volume they want to work with and make it */
/* the current directory. */
/*****************************************************************/
printf("\nEnter the number the volume you want to work with:\n");
scanf("%d", &amp;volume_number);;
rewinddir(dirP); /* beginning of directory */
for (kk = 1; kk &lt;= volume_number; kk++)
direntP = readdir(dirP); /* get requested dir. entry */
strcat(path, "/");
strcat(path, direntP-&gt;d_name);
rc = chdir(path); /* set current working dir. */
if (rc != 0)
perror("chdir() failed:");
if (getcwd(path, sizeof(path)) == NULL)
perror("getcwd() failed:");
printf("\nThe current working directory is: %s\n", path);
rc = closedir(dirP); /* close the directory */
if (rc != 0)
perror("closedir() failed:");
/*****************************************************************/
/* Create and open a file write only. If the file exists it */
/* will be truncated. The owner will have read, write, and */
/* execute authority to the file. */
/*****************************************************************/
strcat(path, "/");
printf("\nEnter a file name:\n");
scanf("%s", &amp;path[strlen(path)]);
filedes = creat(path, S_IRWXU);
if (filedes == -1)
{
perror("creat() failed");
return;
}
rc = write(filedes, data, sizeof(data));
if (rc == -1)
perror("write() failed:");
close(filedes);
/*****************************************************************/
/* Read back the file and print it. */
/*****************************************************************/
memset(data, 0x00, sizeof(data));
filedes = open(path, O_RDWR);
if (filedes == -1)
{
perror("open() failed");
return;
}
read(filedes, data, sizeof(data));
if (filedes == -1)
{
perror("read() failed");
return;
}
printf("\nThe data written to file is: %s\n", data);
/*****************************************************************/
/* Change the offset into the file and change part of it. Read */
/* the entire file, print it out and close the file. */
/*****************************************************************/
lseek(filedes, 4, SEEK_SET);
rc = write(filedes, "slow old ", 9);
if (rc == -1)
{
perror("write() failed");
return;
}
lseek(filedes, 18, SEEK_SET);
rc = write(filedes, "went under ", 11);
if (rc == -1)
{
perror("write() failed");
return;
}
lseek(filedes, 0, SEEK_SET);
read(filedes, data, sizeof(data));
if (filedes == -1)
{
perror("read() failed");
return;
}
printf("\nThe data now is: %s\n", data);
close(filedes);
printf("Done...\n");
return;
}</pre>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="ifs.htm" title="Read this topic collection to learn how to use UNIX-type APIs to program your optical file system.">Integrated file system programming</a></div>
</div>
</div>
</body>
</html>