-.\" Hey Emacs! This file is -*- nroff -*- source.
-.\"
.\" This manpage is Copyright (C) 1992 Drew Eckhardt;
.\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
.\" and Copyright (C) 2005, 2008 Michael Kerrisk <mtk.manpages@gmail.com>
+.\" and Copyright (C) 2014 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
+.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
.\"
.\" Modified 1993-07-21, Rik Faith <faith@cs.unc.edu>
.\" Modified 1994-08-21, Michael Chastain <mec@shell.portal.com>:
.\" details for dup2().
.\" 2008-10-09, mtk: add description of dup3()
.\"
+.\"*******************************************************************
+.\"
+.\" This file was generated with po4a. Translate the source file.
+.\"
+.\"*******************************************************************
+.\"
.\" Japanese Version Copyright (c) 1996 Takeshi Ueno
.\" all rights reserved.
.\" Translated 1996-07-03, Takeshi Ueno <tueno@vio.co.jp>
.\" Modified 1997-12-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Modified 2003-01-16, Akihiro Motoki <amotoki@dd.iij4u.or.jp>
.\" Updated & Modified 2004-05-19, Yuichi SATO <ysato444@yahoo.co.jp>
-.\" Updated & Modified 2005-09-07 Akihiro MOTOKI
+.\" Updated & Modified 2005-09-07, Akihiro MOTOKI
.\" Updated 2008-02-10, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>, LDP v2.77
.\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13
.\"
-.TH DUP 2 2010-09-10 "Linux" "Linux Programmer's Manual"
-.\"O .SH NAME
-.\"O dup, dup2, dup3 \- duplicate a file descriptor
-.SH ̾Á°
-dup, dup2, dup3 \- ¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊ£À½¤¹¤ë
-.\"O .SH SYNOPSIS
-.SH ½ñ¼°
+.TH DUP 2 2015\-01\-22 Linux "Linux Programmer's Manual"
+.SH 名前
+dup, dup2, dup3 \- ファイルディスクリプターを複製する
+.SH 書式
.nf
-.B #include <unistd.h>
+\fB#include <unistd.h>\fP
.sp
-.BI "int dup(int " oldfd );
-.BI "int dup2(int " oldfd ", int " newfd );
+\fBint dup(int \fP\fIoldfd\fP\fB);\fP
+\fBint dup2(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB);\fP
.sp
-.\"O .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
-.BR "#define _GNU_SOURCE" " /* feature_test_macros(7) »²¾È */"
-.B #include <unistd.h>
+\fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */
+\fB#include <fcntl.h>\fP /* 定数 O_* の定義の取得 */
+\fB#include <unistd.h>\fP
.sp
-.BI "int dup3(int " oldfd ", int " newfd ", int " flags );
+\fBint dup3(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB, int \fP\fIflags\fP\fB);\fP
.fi
-.\"O .SH DESCRIPTION
-.SH ÀâÌÀ
-.\"O These system calls create a copy of the file descriptor
-.\"O .IR oldfd .
-¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¡¢¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
-.I oldfd
-¤ÎÊ£À½¤òºî¤ë¡£
+.SH 説明
+\fBdup\fP() システムコールは、 ファイルディスクリプター \fIoldfd\fP のコピーを作成し、 最も小さい番号の未使用のディスクリプターを
+新しいディスクリプターとして使用する。
-.\"O .BR dup ()
-.\"O uses the lowest-numbered unused descriptor for the new descriptor.
-.BR dup ()
-¤ÏºÇ¤â¾®¤µ¤¤ÈÖ¹æ¤Î̤»ÈÍѤΥǥ£¥¹¥¯¥ê¥×¥¿¤ò
-¿·¤·¤¤¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æ»ÈÍѤ¹¤ë¡£
+成功が返された場合には、 古いファイルディスクリプターと新しいファイルディスクリプターは 互いに可換なものとして使うことができる。
+2つのファイルディスクリプターは同じファイル記述 (description) (\fBopen\fP(2) 参照)
+を参照しており、したがってファイルオフセットやファイル状態フラグが 共有される。例えば、一方のディスクリプターに対して \fBlseek\fP(2)
+を使ってファイルオフセットを変更した場合、もう一方のディスクリプターの オフセットも変化する。
-.\"O .BR dup2 ()
-.\"O .RI "makes " newfd " be the copy of " oldfd ", closing " newfd
-.\"O first if necessary, but note the following:
-.BR dup2 ()
-¤Ï
-.I newfd
-¤ò
-.I oldfd
-¤ÎÊ£À½¤È¤·¤ÆºîÀ®¤¹¤ë¡£
-ɬÍפǤ¢¤ì¤ÐºÇ½é¤Ë
-.I newfd
-¤ò¥¯¥í¡¼¥º¤¹¤ë¡£
-°Ê²¼¤ÎÅÀ¤ËÃí°Õ¤¹¤ë¤³¤È¡£
-.IP * 3
-.\"O If
-.\"O .I oldfd
-.\"O is not a valid file descriptor, then the call fails, and
-.\"O .I newfd
-.\"O is not closed.
-.I oldfd
-¤¬Í¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤¤¾ì¹ç¡¢¤½¤Î¸Æ¤Ó½Ð¤·¤Ï¼ºÇÔ¤·¡¢
-.I newfd
-¤Ï¥¯¥í¡¼¥º¤µ¤ì¤Ê¤¤¡£
-.IP *
-.\"O If
-.\"O .I oldfd
-.\"O is a valid file descriptor, and
-.\"O .I newfd
-.\"O has the same value as
-.\"O .IR oldfd ,
-.\"O then
-.\"O .BR dup2 ()
-.\"O does nothing, and returns
-.\"O .IR newfd .
-.I oldfd
-¤¬Í¸ú¤Ê¥Õ¥¡¥¤¥ë¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¡¢
-.I newfd
-¤¬
-.I oldfd
-¤ÈƱ¤¸Ãͤξì¹ç¡¢
-.BR dup2 ()
-¤Ï²¿¤â¤»¤º¡¢
-.I newfd
-¤òÊÖ¤¹¡£
-.PP
-.\"O After a successful return from one of these system calls,
-.\"O the old and new file descriptors may be used interchangeably.
-.\"O They refer to the same open file description (see
-.\"O .BR open (2))
-.\"O and thus share file offset and file status flags;
-.\"O for example, if the file offset is modified by using
-.\"O .BR lseek (2)
-.\"O on one of the descriptors, the offset is also changed for the other.
-¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Î¤¤¤º¤ì¤«¤¬À®¸ù¤òÊÖ¤·¤¿¾ì¹ç¤Ë¤Ï¡¢
-¸Å¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¿·¤·¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï
-¸ß¤¤¤Ë²Ä´¹¤Ê¤â¤Î¤È¤·¤Æ»È¤¦¤³¤È¤¬¤Ç¤¤ë¡£
-2¤Ä¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÏƱ¤¸¥Õ¥¡¥¤¥ëµ½Ò (description)
-.RB ( open (2)
-»²¾È) ¤ò»²¾È¤·¤Æ¤ª¤ê¡¢¤·¤¿¤¬¤Ã¤Æ¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤ä¥Õ¥¡¥¤¥ë¾õÂ֥ե饰¤¬
-¶¦Í¤µ¤ì¤ë¡£Î㤨¤Ð¡¢°ìÊý¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ
-.BR lseek (2)
-¤ò»È¤Ã¤Æ¥Õ¥¡¥¤¥ë¥ª¥Õ¥»¥Ã¥È¤òÊѹ¹¤·¤¿¾ì¹ç¡¢¤â¤¦°ìÊý¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
-¥ª¥Õ¥»¥Ã¥È¤âÊѲ½¤¹¤ë¡£
+.\"
+2つのディスクリプターはファイルディスクリプターフラグ (close\-on\-exec flag) を共有しない。複製されたディスクリプターの
+close\-on\-exec flag (\fBfcntl\fP(2) 参照) は off となる。
+.SS dup2()
+\fBdup2\fP() システムコールは \fBdup\fP() と同じ処理を実行するが、
+番号が最も小さい未使用のファイルディスクリプターを使用する代わりに、
+\fInewfd\fP で指定されたディスクリプター番号を使用する。
+ディスクリプター \fInewfd\fP が以前にオープンされていた場合には、
+黙ってそのディスクリプターをクローズしてから再利用する。
-.\"O The two descriptors do not share file descriptor flags
-.\"O (the close-on-exec flag).
-.\"O The close-on-exec flag
-.\"O .RB ( FD_CLOEXEC ;
-.\"O see
-.\"O .BR fcntl (2))
-.\"O for the duplicate descriptor is off.
-2¤Ä¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ï¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¡¦¥Õ¥é¥° (close-on-exec flag)
-¤ò¶¦Í¤·¤Ê¤¤¡£Ê£À½¤µ¤ì¤¿¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î
-close-on-exec flag
-.RB ( fcntl (2)
-»²¾È) ¤Ï off ¤È¤Ê¤ë¡£
+ファイルディスクリプター \fInewfd\fP をクローズして再利用する処理は
+\fIアトミック(不可分)に\fP実行される。これは重要な点である。 なぜなら、
+等価な機能を \fBclose\fP(2) と \fBdup\fP() を使って実装しようとすると、
+2 つの処理の間に \fInewfd\fP が再利用されてしまうという、
+競合状態にさらされることになるからだ。
+このような再利用が起こるのは、
+メインプログラムがファイルディスクリプターを割り当てる
+シグナルハンドラーにより割り込まれたり、並行動作するスレッドが
+ファイルディスクリプターを割り当てたりすることがあるからだ。
-.\"O .BR dup3 ()
-.\"O is the same as
-.\"O .BR dup2 (),
-.\"O except that:
-.BR dup3 ()
-¤Ï
-.BR dup2 ()
-¤ÈƱ¤¸¤À¤¬¡¢°Ê²¼¤ÎÅÀ¤¬°Û¤Ê¤ë¡£
+以下の点について注意すること:
+.IP * 3
+\fIoldfd\fP が有効なファイルディスクリプターでない場合、その呼び出しは失敗し、 \fInewfd\fP はクローズされない。
+.IP *
+.\"
+\fIoldfd\fP が有効なファイルディスクリプターで、 \fInewfd\fP が \fIoldfd\fP と同じ値の場合、 \fBdup2\fP() は何もせず、
+\fInewfd\fP を返す。
+.SS dup3()
+\fBdup3\fP() は \fBdup2\fP() と同じだが、以下の点が異なる。
.IP * 3
-.\"O The caller can force the close-on-exec flag to be set
-.\"O for the new file descriptor by specifying
-.\"O .BR O_CLOEXEC
-.\"O in
-.\"O .IR flags .
-.\"O See the description of the same flag in
-.\"O .BR open (2)
-.\"O for reasons why this may be useful.
-¸Æ¤Ó½Ð¤·¸µ¤¬¡¢¿·¤·¤¤¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ËÂФ·¤Æ
-close-on-exec ¥Õ¥é¥°¤ò¶¯À©Åª¤ËÀßÄꤹ¤ë¤³¤È¤¬¤Ç¤¤ë¡£
-¤³¤ì¤ò¹Ô¤¦¤Ë¤Ï¡¢
-.I flags
-¤Ë
-.B O_CLOEXEC
-¤ò»ØÄꤹ¤ë¡£
-¤³¤Î¥Õ¥é¥°¤¬Ìò¤ËΩ¤ÄÍýͳ¤Ë¤Ä¤¤¤Æ¤Ï¡¢
-.BR open (2)
-¤Î
-.B O_CLOEXEC
-¥Õ¥é¥°¤ÎÀâÌÀ¤ò»²¾È¤Î¤³¤È¡£
+呼び出し元が、新しいファイルディスクリプターに対して close\-on\-exec フラグを強制的に設定することができる。 これを行うには、
+\fIflags\fP に \fBO_CLOEXEC\fP を指定する。 このフラグが役に立つ理由については、 \fBopen\fP(2) の \fBO_CLOEXEC\fP
+フラグの説明を参照のこと。
.IP *
.\" FIXME . To confirm with Al Viro that this was intended, and its rationale
-.\"O If
-.\"O .IR oldfd
-.\"O equals
-.\"O .IR newfd ,
-.\"O then
-.\"O .BR dup3 ()
-.\"O fails with the error
-.\"O .BR EINVAL .
-.I oldfd
-¤¬
-.I newfd
-¤ÈƱ¤¸¾ì¹ç¡¢
-.BR dup3 ()
-¤Ï
-.B EINVAL
-¥¨¥é¡¼¤Ç¼ºÇÔ¤¹¤ë¡£
-.\"O .SH "RETURN VALUE"
-.SH ÊÖ¤êÃÍ
-.\"O On success, these system calls
-.\"O return the new descriptor.
-.\"O On error, \-1 is returned, and
-.\"O .I errno
-.\"O is set appropriately.
-À®¸ù¤¹¤ë¤È¡¢¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï¿·¤·¤¤¥Ç¥£¥¹¥¯¥ê¥×¥¿¤òÊÖ¤¹¡£
-¥¨¥é¡¼¤Î¾ì¹ç¡¢\-1 ¤òÊÖ¤·¡¢
-.I errno
-¤òŬÀÚ¤ËÀßÄꤹ¤ë¡£
-.\"O .SH ERRORS
-.SH ¥¨¥é¡¼
-.TP
-.\"O .B EBADF
-.\"O .I oldfd
-.\"O isn't an open file descriptor, or
-.\"O .I newfd
-.\"O is out of the allowed range for file descriptors.
-.B EBADF
-.I oldfd
-¤¬¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ê¤¤¤«¡¢
-.I newfd
-¤¬¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤È¤·¤Æµö¤µ¤ì¤ëÈϰϤ«¤é³°¤ì¤Æ¤¤¤ë¡£
-.TP
-.B EBUSY
-.\"O (Linux only) This may be returned by
-.\"O .BR dup2 ()
-.\"O or
-.\"O .BR dup3 ()
-.\"O during a race condition with
-.\"O .BR open (2)
-.\"O and
-.\"O .BR dup ().
-(Linux ¤Î¤ß)
-.BR open (2)
-¤ä
-.BR dup ()
-¤È¤Î¶¥¹ç¾õÂ֤ξì¹ç¤Ë¡¢
-.BR dup2 ()
-¤ä
-.BR dup3 ()
-¤Ï¤³¤Î¥¨¥é¡¼¤òÊÖ¤¹¤«¤â¤·¤ì¤Ê¤¤¡£
-.TP
-.B EINTR
-.\"O The
-.\"O .BR dup2 ()
-.\"O or
-.\"O .BR dup3 ()
-.\"O call was interrupted by a signal; see
-.\"O .BR signal (7).
-.BR dup2 ()
-¤ä
-.BR dup3 ()
-¤Î¸Æ¤Ó½Ð¤·¤¬¥·¥°¥Ê¥ë¤Ë¤è¤ê³ä¤ê¹þ¤Þ¤ì¤¿¡£
-.BR signal (7)
-»²¾È¡£
-.TP
-.B EINVAL
-.\"O .RB ( dup3 ())
-.\"O .I flags
-.\"O contain an invalid value.
-.RB ( dup3 ())
-.I flags
-¤Ë̵¸ú¤ÊÃͤ¬Æþ¤Ã¤Æ¤¤¤ë¡£
+\fIoldfd\fP が \fInewfd\fP と同じ場合、 \fBdup3\fP() は \fBEINVAL\fP エラーで失敗する。
+.SH 返り値
+成功すると、これらのシステムコールは新しいディスクリプターを返す。 エラーの場合、\-1 を返し、 \fIerrno\fP を適切に設定する。
+.SH エラー
+.TP
+\fBEBADF\fP
+\fIoldfd\fP がオープンされたファイルディスクリプターではない。
+.TP
+\fBEBADF\fP
+\fInewfd\fP がファイルディスクリプターとして許可されている範囲ではない (\fBgetrlimit\fP(2) の \fBRLIMIT_NOFILE\fP
+の議論を参照)。
+.TP
+\fBEBUSY\fP
+(Linux のみ) \fBopen\fP(2) や \fBdup\fP() との競合状態の場合に、 \fBdup2\fP() や \fBdup3\fP()
+はこのエラーを返すかもしれない。
+.TP
+\fBEINTR\fP
+\fBdup2\fP() や \fBdup3\fP() の呼び出しがシグナルにより割り込まれた。 \fBsignal\fP(7) 参照。
+.TP
+\fBEINVAL\fP
+(\fBdup3\fP()) \fIflags\fP に無効な値が入っている。
+.TP
+\fBEINVAL\fP
.\" FIXME . To confirm with Al Viro that this was intended, and its rationale
-.\"O Or,
-.\"O .I oldfd
-.\"O was equal to
-.\"O .IR newfd .
-¤â¤·¤¯¤Ï¡¢
-.I oldfd
-¤¬
-.I newfd
-¤ÈƱ¤¸¤Ç¤¢¤Ã¤¿¡£
-.TP
-.\"O .B EMFILE
-.\"O The process already has the maximum number of file
-.\"O descriptors open and tried to open a new one.
-.B EMFILE
-¥×¥í¥»¥¹¤¬¤¹¤Ç¤Ë¥ª¡¼¥×¥ó¤Ç¤¤ëºÇÂç¿ô¤Þ¤Ç¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿
-¤ò³«¤¤¤Æ¤¤¤Æ¡¢¤µ¤é¤Ë¿·¤·¤¤¤â¤Î¤ò³«¤³¤¦¤È¤·¤¿¡£
-.\"O .SH VERSIONS
-.SH ¥Ð¡¼¥¸¥ç¥ó
-.\"O .BR dup3 ()
-.\"O was added to Linux in version 2.6.27;
-.\"O glibc support is available starting with
-.\"O version 2.9.
-.BR dup3 ()
-¤Ï¥Ð¡¼¥¸¥ç¥ó 2.6.27 ¤Ç Linux ¤ËÄɲ䵤줿¡£
-glibc ¤Ë¤è¤ë¥µ¥Ý¡¼¥È¤Ï¥Ð¡¼¥¸¥ç¥ó 2.9 °Ê¹ß¤ÇÍøÍѤǤ¤ë¡£
-.\"O .SH "CONFORMING TO"
-.SH ½àµò
-.BR dup (),
-.BR dup2 ():
-SVr4, 4.3BSD, POSIX.1-2001.
+(\fBdup3\fP()) \fIoldfd\fP が \fInewfd\fP と同じであった。
+.TP
+\fBEMFILE\fP
+プロセスがすでにオープンできる最大数までファイルディスクリプター を開いていて、さらに新しいものを開こうとした (\fBgetrlimit\fP(2)
+のリソース上限 \fBRLIMIT_NOFILE\fP を参照)。
+.SH バージョン
+\fBdup3\fP() はバージョン 2.6.27 で Linux に追加された。 glibc によるサポートはバージョン 2.9 以降で利用できる。
+.SH 準拠
+\fBdup\fP(), \fBdup2\fP(): SVr4, 4.3BSD, POSIX.1\-2001.
+
+.\" SVr4 documents additional
+.\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
+.\" The EBUSY return is Linux-specific.
+\fBdup3\fP() は Linux 固有である。
+.SH 注意
+\fInewfd\fP が範囲を超えた時に返されるエラーは、 \fBdup2\fP() と \fBfcntl(\fP..., \fBF_DUPFD\fP, ...\fB)\fP
+では異っている。 \fBdup2\fP() が \fBF_DUPFD\fP と同じように \fBEINVAL\fP を返すシステムもある。
+
+\fInewfd\fP がオープンされていた場合、
+\fBclose\fP(2) 時に報告されることになるエラーはすべて失われる。
+これが心配で、シングルスレッドかつシグナルハンドラーで
+ファイルディスクリプターを割り当てるようなプログラムでない場合には、
+正しい方法は \fBdup2\fP() を呼び出す前に
+\fInewfd\fP をクローズ「しない」ことである。
+なぜなら、上で説明した競合状況があるからである。
+代わりに、以下のようなコードが使用できることだろう。
+
+.nf
+ /* あとで close() エラーをチェックするのに使用できる
+ ように 'newfd' の複製を取得する。 EBADF エラーは
+ 'newfd' がオープンされていないことを意味する。 */
-.\"O .BR dup3 ()
-.\"O is Linux-specific.
-.BR dup3 ()
-¤Ï Linux ¸ÇͤǤ¢¤ë¡£
-.\"O .\" SVr4 documents additional
-.\"O .\" EINTR and ENOLINK error conditions. POSIX.1 adds EINTR.
-.\"O .\" The EBUSY return is Linux-specific.
-.\" SVr4 ¤Ë¤Ï¾¤Ë EINTR, ENOLINK ¥¨¥é¡¼¾õÂ֤ε½Ò¤¬¤¢¤ë¡£
-.\" POSIX.1 ¤Ë¤Ï¾¤Ë EINTR ¤¬¤¢¤ë¡£
-.\" EBUSY ¤¬ÊÖ¤µ¤ì¤ë¤Î¤Ï Linux Æȼ«¤Î¤â¤Î¤Ç¤¢¤ë¡£
-.\"O .SH NOTES
-.SH Ãí°Õ
-.\"O The error returned by
-.\"O .BR dup2 ()
-.\"O is different from that returned by
-.\"O .BR fcntl( "..., " F_DUPFD ", ..." )
-.\"O when
-.\"O .I newfd
-.\"O is out of range.
-.\"O On some systems
-.\"O .BR dup2 ()
-.\"O also sometimes returns
-.\"O .B EINVAL
-.\"O like
-.\"O .BR F_DUPFD .
-.I newfd
-¤¬ÈϰϤòĶ¤¨¤¿»þ¤ËÊÖ¤µ¤ì¤ë¥¨¥é¡¼¤Ï¡¢
-.BR dup2 ()
-¤È
-.BR fcntl( "..., " F_DUPFD ", ..." )
-¤Ç¤Ï°Û¤Ã¤Æ¤¤¤ë¡£
-.BR dup2 ()
-¤¬
-.B F_DUPFD
-¤ÈƱ¤¸¤è¤¦¤Ë
-.B EINVAL
-¤òÊÖ¤¹¥·¥¹¥Æ¥à¤â¤¢¤ë¡£
+ tmpfd = dup(newfd);
+ if (tmpfd == \-1 && errno != EBADF) {
+ /* 予期しない dup() のエラーを処理する */
+ }
-.\"O If
-.\"O .I newfd
-.\"O was open, any errors that would have been reported at
-.\"O .BR close 2()
-.\"O time are lost.
-.\"O A careful programmer will not use
-.\"O .BR dup2 ()
-.\"O or
-.\"O .BR dup3 ()
-.\"O without closing
-.\"O .I newfd
-.\"O first.
-.I newfd
-¤¬¥ª¡¼¥×¥ó¤µ¤ì¤Æ¤¤¤ë¤È¡¢
-.BR close (2)
-¤·¤¿»þ¤ËÊó¹ð¤µ¤ì¤ë¤Ï¤º¤Î¥¨¥é¡¼¤¬¼º¤ï¤ì¤Æ¤·¤Þ¤¦¡£
-.BR dup2 ()
-¤ä
-.BR dup3 ()
-¤ò»È¤¦Á°¤ËÀ褺
-.I newfd
-¤ò¥¯¥í¡¼¥º¤¹¤ë¤è¤¦¤Ë¤·¤¿Êý¤¬¤¤¤¤¤À¤í¤¦¡£
-.\"O .SH "SEE ALSO"
-.SH ´ØÏ¢¹àÌÜ
-.BR close (2),
-.BR fcntl (2),
-.BR open (2)
+ /* アトミックに 'oldfd' を 'newfd' に複製する */
+
+ if (dup2(oldfd, newfd) == \-1) {
+ /* dup2() のエラーを処理する */
+ }
+
+ /* ここでもともと 'newfd' で参照されていたファイルの
+ close() エラーをチェックする */
+
+ if (tmpfd != \-1) {
+ if (close(tmpfd) == \-1) {
+ /* close からのエラーを処理する */
+ }
+ }
+.fi
+.SH 関連項目
+\fBclose\fP(2), \fBfcntl\fP(2), \fBopen\fP(2)
+.SH この文書について
+この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.79 の一部
+である。プロジェクトの説明とバグ報告に関する情報は
+http://www.kernel.org/doc/man\-pages/ に書かれている。