📄 dup.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>dup</title></head><body bgcolor="white"><basefont size="3"> <a name="dup"></a> <a name="tag_03_114"></a><!-- dup --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_114_01"></a>NAME</h4><blockquote>dup, dup2 - duplicate an open file descriptor</blockquote><h4><a name="tag_03_114_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/unistd.h.html">unistd.h</a>><br><br> int dup(int</tt> <i>fildes</i><tt>);<br> int dup2(int</tt> <i>fildes</i><tt>, int</tt> <i>fildes2</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_114_03"></a>DESCRIPTION</h4><blockquote><p>The <i>dup</i>() and <i>dup2</i>() functions provide an alternative interface to the service provided by <a href="../functions/fcntl.html"><i>fcntl</i>()</a> using the F_DUPFD command. The call:</p><pre><tt>fid = dup(</tt><i>fildes</i><tt>);</tt></pre><p>shall be equivalent to:</p><pre><tt>fid = fcntl(</tt><i>fildes</i><tt>, F_DUPFD, 0);</tt></pre><p>The call:</p><pre><tt>fid = dup2(</tt><i>fildes</i><tt>,</tt> <i>fildes2</i><tt>);</tt></pre><p>shall be equivalent to:</p><pre><tt>close(</tt><i>fildes2</i><tt>);fid = fcntl(</tt><i>fildes</i><tt>, F_DUPFD,</tt> <i>fildes2</i><tt>);</tt></pre><p>except for the following:</p><ul><li><p>If <i>fildes2</i> is less than 0 or greater than or equal to {OPEN_MAX}, <i>dup2</i>() shall return -1 with <i>errno</i> set to[EBADF].</p></li><li><p>If <i>fildes</i> is a valid file descriptor and is equal to <i>fildes2</i>, <i>dup2</i>() shall return <i>fildes2</i> withoutclosing it.</p></li><li><p>If <i>fildes</i> is not a valid file descriptor, <i>dup2</i>() shall return -1 and shall not close <i>fildes2</i>.</p></li><li><p>The value returned shall be equal to the value of <i>fildes2</i> upon successful completion, or -1 upon failure.</p></li></ul></blockquote><h4><a name="tag_03_114_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion a non-negative integer, namely the file descriptor, shall be returned; otherwise, -1 shall bereturned and <i>errno</i> set to indicate the error.</p></blockquote><h4><a name="tag_03_114_05"></a>ERRORS</h4><blockquote><p>The <i>dup</i>() function shall fail if:</p><dl compact><dt>[EBADF]</dt><dd>The <i>fildes</i> argument is not a valid open file descriptor.</dd><dt>[EMFILE]</dt><dd>The number of file descriptors in use by this process would exceed {OPEN_MAX}.</dd></dl><p>The <i>dup2</i>() function shall fail if:</p><dl compact><dt>[EBADF]</dt><dd>The <i>fildes</i> argument is not a valid open file descriptor or the argument <i>fildes2</i> is negative or greater than orequal to {OPEN_MAX}.</dd><dt>[EINTR]</dt><dd>The <i>dup2</i>() function was interrupted by a signal.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_114_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_114_06_01"></a>Redirecting Standard Output to a File</h5><p>The following example closes standard output for the current processes, re-assigns standard output to go to the file referencedby <i>pfd</i>, and closes the original file descriptor to clean up.</p><pre><tt>#include <unistd.h>...int pfd;...close(1);dup(pfd);close(pfd);...</tt></pre><h5><a name="tag_03_114_06_02"></a>Redirecting Error Messages</h5><p>The following example redirects messages from <i>stderr</i> to <i>stdout</i>.</p><pre><tt>#include <unistd.h>...dup2(1, 2);...</tt></pre></blockquote><h4><a name="tag_03_114_07"></a>APPLICATION USAGE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_114_08"></a>RATIONALE</h4><blockquote><p>The <i>dup</i>() and <i>dup2</i>() functions are redundant. Their services are also provided by the <a href="../functions/fcntl.html"><i>fcntl</i>()</a> function. They have been included in this volume of IEEE Std 1003.1-2001primarily for historical reasons, since many existing applications use them.</p><p>While the brief code segment shown is very similar in behavior to <i>dup2</i>(), a conforming implementation based on otherfunctions defined in this volume of IEEE Std 1003.1-2001 is significantly more complex. Least obvious is the possibleeffect of a signal-catching function that could be invoked between steps and allocate or deallocate file descriptors. This could beavoided by blocking signals.</p><p>The <i>dup2</i>() function is not marked obsolescent because it presents a type-safe version of functionality provided in atype-unsafe version by <a href="../functions/fcntl.html"><i>fcntl</i>()</a>. It is used in the POSIX Ada binding.</p><p>The <i>dup2</i>() function is not intended for use in critical regions as a synchronization mechanism.</p><p>In the description of [EBADF], the case of <i>fildes</i> being out of range is covered by the given case of <i>fildes</i> notbeing valid. The descriptions for <i>fildes</i> and <i>fildes2</i> are different because the only kind of invalidity that isrelevant for <i>fildes2</i> is whether it is out of range; that is, it does not matter whether <i>fildes2</i> refers to an openfile when the <i>dup2</i>() call is made.</p></blockquote><h4><a name="tag_03_114_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_114_10"></a>SEE ALSO</h4><blockquote><p><a href="close.html"><i>close</i>()</a> , <a href="fcntl.html"><i>fcntl</i>()</a> , <a href="open.html"><i>open</i>()</a> , theBase Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/unistd.h.html"><i><unistd.h></i></a></p></blockquote><h4><a name="tag_03_114_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -