.\" This manpage is Copyright (C) 1992 Drew Eckhardt; .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson. .\" and Copyright (C) 2005, 2008 Michael Kerrisk .\" .\" %%%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. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" 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 .\" Modified 1994-08-21, Michael Chastain : .\" Fixed typoes. .\" Modified 1997-01-31, Eric S. Raymond .\" Modified 2002-09-28, aeb .\" 2009-01-12, mtk, reordered text in DESCRIPTION and added some .\" 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 .\" Modified 1997-12-14, HANATAKA Shinya .\" Modified 2003-01-16, Akihiro Motoki .\" Updated & Modified 2004-05-19, Yuichi SATO .\" Updated & Modified 2005-09-07, Akihiro MOTOKI .\" Updated 2008-02-10, Akihiro MOTOKI , LDP v2.77 .\" Updated 2008-11-09, Akihiro MOTOKI, LDP v3.13 .\" .TH DUP 2 2012\-02\-14 Linux "Linux Programmer's Manual" .SH 名前 dup, dup2, dup3 \- ファイルディスクリプタを複製する .SH 書式 .nf \fB#include \fP .sp \fBint dup(int \fP\fIoldfd\fP\fB);\fP \fBint dup2(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB);\fP .sp \fB#define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ \fB#include \fP /* 定数 O_* の定義の取得 */ \fB#include \fP .sp \fBint dup3(int \fP\fIoldfd\fP\fB, int \fP\fInewfd\fP\fB, int \fP\fIflags\fP\fB);\fP .fi .SH 説明 これらのシステムコールは、ファイルディスクリプタ \fIoldfd\fP の複製を作る。 \fBdup\fP() は最も小さい番号の未使用のディスクリプタを 新しいディスクリプタとして使用する。 \fBdup2\fP() は \fInewfd\fP を \fIoldfd\fP の複製として作成する。 必要であれば最初に \fInewfd\fP をクローズする。 以下の点に注意すること。 .IP * 3 \fIoldfd\fP が有効なファイルディスクリプタでない場合、その呼び出しは失敗し、 \fInewfd\fP はクローズされない。 .IP * \fIoldfd\fP が有効なファイルディスクリプタで、 \fInewfd\fP が \fIoldfd\fP と同じ値の場合、 \fBdup2\fP() は何もせず、 \fInewfd\fP を返す。 .PP これらのシステムコールのいずれかが成功を返した場合には、 古いファイルディスクリプタと新しいファイルディスクリプタは 互いに可換なものとして使うことができる。 2つのファイルディスクリプタは同じファイル記述 (description) (\fBopen\fP(2) 参照) を参照しており、したがってファイルオフセットやファイル状態フラグが 共有される。例えば、一方のディスクリプタに対して \fBlseek\fP(2) を使ってファイルオフセットを変更した場合、もう一方のディスクリプタの オフセットも変化する。 2つのディスクリプタはファイルディスクリプタフラグ (close\-on\-exec flag) を共有しない。複製されたディスクリプタの close\-on\-exec flag (\fBfcntl\fP(2) 参照) は off となる。 \fBdup3\fP() は \fBdup2\fP() と同じだが、以下の点が異なる。 .IP * 3 呼び出し元が、新しいファイルディスクリプタに対して 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 \fIoldfd\fP が \fInewfd\fP と同じ場合、 \fBdup3\fP() は \fBEINVAL\fP エラーで失敗する。 .SH 返り値 成功すると、これらのシステムコールは新しいディスクリプタを返す。 エラーの場合、\-1 を返し、 \fIerrno\fP を適切に設定する。 .SH エラー .TP \fBEBADF\fP \fIoldfd\fP がオープンされたファイルディスクリプタでないか、 \fInewfd\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 .\" FIXME . To confirm with Al Viro that this was intended, and its rationale (\fBdup3\fP()) \fIflags\fP に無効な値が入っている。 もしくは、 \fIoldfd\fP が \fInewfd\fP と同じであった。 .TP \fBEMFILE\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() や \fBdup3\fP() を使う前に先ず \fInewfd\fP をクローズするようにした方がいいだろう。 .SH 関連項目 \fBclose\fP(2), \fBfcntl\fP(2), \fBopen\fP(2) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。