418 lines
11 KiB
HTML
418 lines
11 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
<meta name="Copyright" content="Copyright (c) 2006 by IBM Corporation">
|
|
<title>dup2()--Duplicate Open File Descriptor to Another Descriptor</title>
|
|
<!-- Begin Header Records ========================================== -->
|
|
<!-- 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. -->
|
|
<!-- Change History: -->
|
|
<!-- YYMMDD USERID Change description -->
|
|
<!-- Unix2 SCRIPT J converted by B2H R4.1 (346) (CMS) by V2KEA304 -->
|
|
<!-- at RCHVMW2 on 17 Feb 1999 at 11:05:09 -->
|
|
<!-- 011022 JTROUS Changes from API Review 1, V5R2 -->
|
|
<!-- 020313 JTROUS Changes for scan processing, V5R3, DCR 98680 -->
|
|
<!-- 020618 EMIG Updates for NFS threadsafety, V5R3 -->
|
|
<!-- 0206?? JET This file has undergone html cleanup -->
|
|
<!-- 020718 MFENLON: updated for QFileSvr.400 threadsafety, V5R3 -->
|
|
<!-- 050323 JTROUS: fix enums, no change flag, V5R4 -->
|
|
<!-- 050817 JTROUS: add cross link to close API, v5r4 -->
|
|
<!-- End Header Records -->
|
|
<link rel="stylesheet" type="text/css" href="../rzahg/ic.css">
|
|
</head>
|
|
<body>
|
|
<!-- Java sync-link -->
|
|
<script language="Javascript" src="../rzahg/synch.js" type="text/javascript">
|
|
</script>
|
|
|
|
<a name="Top_Of_Page"></a>
|
|
|
|
<h2>dup2()--Duplicate Open File Descriptor to Another Descriptor</h2>
|
|
|
|
<div class="box" style="width: 70%;">
|
|
<br>
|
|
Syntax<br>
|
|
|
|
|
|
<pre>
|
|
#include <unistd.h>
|
|
|
|
int dup2(int <em>fildes</em>, int <em>fildes2</em>);
|
|
</pre>
|
|
|
|
Service Program Name: QP0LLIB1<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Default Public Authority: *USE<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
Threadsafe: Conditional; see <a href="#usage_notes">Usage Notes</a>.<br>
|
|
<!-- iddvc RMBR -->
|
|
<br>
|
|
</div>
|
|
|
|
<p>The <strong>dup2()</strong> function returns a descriptor with the value
|
|
<em>fildes2</em>. The descriptor refers to the same file as <em>fildes</em>,
|
|
and it will close the file that <em>fildes2</em> was associated with.
|
|
<img src="delta.gif" alt="Start of change">
|
|
For more information on the processing which may occur when the file is
|
|
closed, see <a href="close.htm">close()</a>--Close File or Socket Descriptor.
|
|
<img src="deltaend.gif" alt="End of change">
|
|
</p>
|
|
|
|
<p>If the original file descriptor was opened in text mode, data conversion is
|
|
also done on the duplicated file descriptor.</p>
|
|
|
|
<p>The FD_CLOEXEC flag that is associated with the new file descriptor is
|
|
cleared. Refer to <a href="fcntl.htm">fcntl()</a>--Perform File Control Command
|
|
for additional information about the FD_CLOEXEC flag.</p>
|
|
|
|
<p>The following conditions apply:</p>
|
|
|
|
<ul>
|
|
<li>If <em>fildes2</em> is less than zero or greater than or equal to OPEN_MAX,
|
|
<strong>dup2()</strong> returns -<samp>1</samp> and sets the <em>errno</em>
|
|
global variable to [EBADF].<br>
|
|
</li>
|
|
|
|
<li>If <em>fildes</em> is a valid descriptor and is equal to <em>fildes2</em>,
|
|
<strong>dup2()</strong> returns <em>fildes2</em> without closing it.<br>
|
|
</li>
|
|
|
|
<li>If <em>fildes</em> is not a valid descriptor, <strong>dup2()</strong> fails
|
|
and does not close <em>fildes2</em>.</li>
|
|
</ul>
|
|
|
|
<p>This function works with descriptors for any type of object.</p>
|
|
|
|
<br>
|
|
<h3>Parameters</h3>
|
|
|
|
<dl>
|
|
<dt><strong><em>fildes</em></strong></dt>
|
|
|
|
<dd>(Input) A descriptor to be duplicated.<br>
|
|
</dd>
|
|
|
|
<dt><strong><em>fildes2</em></strong></dt>
|
|
|
|
<dd>(Input) The descriptor to which the duplication is made.</dd>
|
|
</dl>
|
|
|
|
<br>
|
|
<h3>Authorities</h3>
|
|
|
|
<p>No authorization is required.</p>
|
|
|
|
<br>
|
|
<h3>Return Value</h3>
|
|
|
|
<dl compact>
|
|
<dt><em>value</em></dt>
|
|
|
|
<dd><strong>dup2()</strong> was successful. The value of <em>fildes2</em> is
|
|
returned.</dd>
|
|
|
|
<dt><em>-1</em></dt>
|
|
|
|
<dd><strong>dup2()</strong> was not successful. The <em>errno</em> global
|
|
variable is set to indicate the error.</dd>
|
|
</dl>
|
|
|
|
<br>
|
|
<h3>Error Conditions</h3>
|
|
|
|
<p>If <strong>dup2()</strong> is not successful, <em>errno</em> usually
|
|
indicates one of the following errors. Under some conditions, <em>errno</em>
|
|
could indicate an error other than those listed here.</p>
|
|
|
|
<table cellpadding="5">
|
|
<!-- cols="25 75" -->
|
|
|
|
<tr>
|
|
<th align="left" valign="bottom">Error condition</th>
|
|
<th align="left" valign="bottom">Additional information</th>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#EACCES">EACCES</a>]</em></td>
|
|
|
|
|
|
<td align="left" valign="top">
|
|
<p>If you are accessing a remote file through the Network File System, update
|
|
operations to file permissions at the server are not reflected at the client
|
|
until updates to data that is stored locally by the Network File System take
|
|
place. (Several options on the Add Mounted File System (ADDMFS) command
|
|
determine the time between refresh operations of local data.) Access to a
|
|
remote file may also fail due to different mappings of user IDs (UID) or group
|
|
IDs (GID) on the local and remote systems.</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#EBADF">EBADF</a>]</em></td>
|
|
|
|
<td align="left" valign="top">
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#EBADFID">EBADFID</a>]</em></td>
|
|
|
|
<td align="left" valign="top">
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#EIO">EIO</a>]</em></td>
|
|
|
|
<td align="left" valign="top">
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#ENOTSAFE">ENOTSAFE</a>]</em></td>
|
|
|
|
<td align="left" valign="top">
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#ENOTSUP">ENOTSUP</a>]</em></td>
|
|
|
|
<td align="left" valign="top">
|
|
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#ESTALE">ESTALE</a>]</em></td>
|
|
|
|
<td align="left" valign="top">
|
|
<p>If you are accessing a remote file through the Network File System, the file
|
|
may have been deleted at the server.</p>
|
|
</td>
|
|
</tr>
|
|
|
|
|
|
<tr>
|
|
<td align="left" valign="top">
|
|
<em>[<a href="unix14.htm#EUNKNOWN">EUNKNOWN</a>]</em></td>
|
|
|
|
<td align="left" valign="top">
|
|
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<br>
|
|
<br>
|
|
<h3>Error Messages</h3>
|
|
|
|
<p>The following messages may be sent from this function:</p>
|
|
|
|
<table width="100%" cellpadding="5">
|
|
<!-- cols="15 85" -->
|
|
<tr>
|
|
<th align="left" valign="top">Message ID</th>
|
|
<th align="left" valign="top">Error Message Text</th>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td width="15%" valign="top">CPE3418 E</td>
|
|
<td width="85%" valign="top">Possible APAR condition or hardware failure.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td valign="top">CPFA0D4 E</td>
|
|
<td valign="top">File system error occurred. Error number &1.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td valign="top">CPF3CF2 E</td>
|
|
<td valign="top">Error(s) occurred during running of &1 API.</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td valign="top">CPF9872 E</td>
|
|
<td valign="top">Program or service program &1 in library &2 ended.
|
|
Reason code &3.</td>
|
|
</tr>
|
|
</table>
|
|
|
|
<br>
|
|
<h3><a name="usage_notes">Usage Notes</a></h3>
|
|
|
|
<ol type="1">
|
|
<li>If this function is called
|
|
by a thread executing one of the scan-related exit programs (or any of its
|
|
created threads), it will fail with error code [ENOTSUP]. See <a href=
|
|
"ifsopenexit.htm">Integrated File System Scan on Open Exit Programs</a> and <a
|
|
href="ifscloseexit.htm">Integrated File System Scan on Close Exit Programs</a>
|
|
for more information. <br>
|
|
<br>
|
|
</li>
|
|
|
|
<li>This function will fail with error code [ENOTSAFE] when all the following
|
|
conditions are true:
|
|
|
|
<ul>
|
|
<li>Where multiple threads exist in the job.<br>
|
|
</li>
|
|
|
|
<li>The object on which this function is operating resides in a file system
|
|
that is not threadsafe. Only the following file systems are threadsafe for this
|
|
function:
|
|
|
|
<ul>
|
|
<li>"Root" (/)</li>
|
|
|
|
<li>QOpenSys</li>
|
|
|
|
<li>User-defined</li>
|
|
|
|
<li>QNTC</li>
|
|
|
|
<li>QSYS.LIB</li>
|
|
|
|
<li>Independent ASP QSYS.LIB</li>
|
|
|
|
<li>QOPT</li>
|
|
|
|
<li>Network File System</li>
|
|
|
|
|
|
<li>QFileSvr.400</li>
|
|
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ol>
|
|
|
|
<br>
|
|
<h3>Related Information</h3>
|
|
|
|
<ul>
|
|
<li>The <<strong>unistd.h</strong>> file (see <a href="unix13.htm">Header
|
|
Files for UNIX-Type Functions</a>)<br>
|
|
</li>
|
|
|
|
<li><a href="close.htm">close()</a>--Close File or Socket Descriptor<br>
|
|
</li>
|
|
|
|
<li><a href="creat.htm">creat()</a>--Create or Rewrite File<br>
|
|
</li>
|
|
|
|
<li><a href="dup.htm">dup()</a>--Duplicate Open File Descriptor<br>
|
|
</li>
|
|
|
|
<li><a href="fcntl.htm">fcntl()</a>--Perform File Control Command<br>
|
|
</li>
|
|
|
|
<li><a href="open.htm">open()</a>--Open File</li>
|
|
</ul>
|
|
|
|
<br>
|
|
<h3>Example</h3>
|
|
|
|
<p>See <a href="../apiref/aboutapis.htm#codedisclaimer">Code disclaimer information</a>
|
|
for information pertaining to code examples.</p>
|
|
|
|
<p>The following example duplicates an open descriptor:</p>
|
|
|
|
<pre>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
|
|
void print_file_id(int file_descriptor) {
|
|
struct stat info;
|
|
if (fstat(file_descriptor, &info) != 0)
|
|
fprintf(stderr, "stat() error for file_descriptor %d: %s\n",
|
|
strerror(errno));
|
|
else
|
|
printf("The file id of file_descriptor %d is %d\n", file_descriptor,
|
|
(int) info.st_ino);
|
|
}
|
|
|
|
main() {
|
|
int file_descriptor, file_descriptor2;
|
|
char fn[] = "original.file";
|
|
char fn2[] = "dup2.file";
|
|
|
|
/* create original file */
|
|
if((file_descriptor = creat(fn, S_IRUSR | S_IWUSR)) < 0)
|
|
perror("creat() error");
|
|
/* create file to dup to */
|
|
else if((file_descriptor2 = creat(fn2, S_IWUSR)) < 0)
|
|
perror("creat()error");
|
|
/* dup file_descriptor to file_descriptor2; print results */
|
|
else {
|
|
print_file_id(file_descriptor);
|
|
print_file_id(file_descriptor2);
|
|
if ((file_descriptor2 = dup2(file_descriptor, file_descriptor2)) < 0)
|
|
perror("dup2() error");
|
|
else {
|
|
puts("After dup2()...");
|
|
print_file_id(file_descriptor);
|
|
print_file_id(file_descriptor2);
|
|
puts("The file descriptors are different but they");
|
|
puts("point to the same file which is different than");
|
|
puts("the file that the second file_descriptor originally pointed to.");
|
|
close(file_descriptor);
|
|
close(file_descriptor2);
|
|
}
|
|
unlink(fn);
|
|
unlink(fn2);
|
|
}
|
|
}
|
|
</pre>
|
|
|
|
<p><strong>Output:</strong></p>
|
|
|
|
<pre>
|
|
The file id of file_descriptor 0 is 30
|
|
The file id of file_descriptor 3 is 58
|
|
After dup2()...
|
|
The file id of file_descriptor 0 is 30
|
|
The file id of file_descriptor 3 is 30
|
|
The file descriptors are different, but they
|
|
point to the same file, which is different than
|
|
the file that the second file_descriptor originally pointed to.
|
|
</pre>
|
|
|
|
<br>
|
|
<hr>
|
|
API introduced: V3R1
|
|
|
|
<hr>
|
|
<center>
|
|
<table cellpadding="2" cellspacing="2">
|
|
<tr align="center">
|
|
<td valign="middle" align="center"><a href="#Top_Of_Page">Top</a> | <a href=
|
|
"unix.htm">UNIX-Type APIs</a> | <a href="aplist.htm">APIs by category</a></td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
</body>
|
|
</html>
|
|
|