.\" Copyright 1991 The Regents of the University of California. .\" All rights reserved. .\" .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB) .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. All advertising materials mentioning features or use of this software .\" must display the following acknowledgement: .\" This product includes software developed by the University of .\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" %%%LICENSE_END .\" .\" @(#)popen.3 6.4 (Berkeley) 4/30/91 .\" .\" Converted for Linux, Mon Nov 29 14:45:38 1993, faith@cs.unc.edu .\" Modified Sat May 18 20:37:44 1996 by Martin Schulze (joey@linux.de) .\" Modified 7 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk) .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 1997 Takashi Yoshino .\" all rights reserved. .\" Translated 1997-01-21, Takashi Yoshino .\" Modified 2007-05-03, Akihiro MOTOKI .\" Modified 2008-11-09, Akihiro MOTOKI, LDP v3.13 .\" .TH POPEN 3 2017\-09\-15 GNU "Linux Programmer's Manual" .SH 名前 popen, pclose \- プロセスとの入力/出力用のパイプストリーム .SH 書式 .nf \fB#include \fP .PP \fBFILE *popen(const char *\fP\fIcommand\fP\fB, const char *\fP\fItype\fP\fB);\fP .PP \fBint pclose(FILE *\fP\fIstream\fP\fB);\fP .fi .PP .RS -4 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7) 参照): .ad l .RE .PP \fBpopen\fP(), \fBpclose\fP(): .RS 4 _POSIX_C_SOURCE\ >=\ 2 || /* glibc 2.19 以前: */ _BSD_SOURCE || _SVID_SOURCE .RE .ad b .SH 説明 \fBpopen\fP() 関数は、プロセスをオープンする。具体的には、 パイプを生成し、フォークを行い、シェルを起動する。 定義から分かるように、パイプは一方向なので、 \fItype\fP 引数には読み込みか書き込みのどちらか一方だけを指定できる (両方は指定できない)。 生成されるストリームは、この指定に対応して、読み取り専用または 書き込み専用のいずれかとなる。 .PP \fIcommand\fP 引数は、シェルのコマンドラインを含むヌル終端された文字列へのポインターである。 このコマンドは \fB\-c\fP フラグを用いて \fI/bin/sh\fP に渡される。 コマンドの解釈は (もし必要ならば) シェルによって行われる。 .PP \fItype\fP 引数は、ヌル終端された文字列へのポインターで、 読み込みを示す文字 \(aqr\(aq か、書き込みを示す文字 \(aqw\(aq の どちらか一方を指定しなければならない。 glibc 2.9 以降では、この引数に文字 \(aqe\(aq を追加で指定できる。 文字 \(aqe\(aq を指定すると、 対応するファイルディスクリプターにおいて、 close\-on\-exec フラグ (\fBFD_CLOEXEC\fP) がセットされる。 これが役に立つ理由については、 \fBopen\fP(2) の \fBO_CLOEXEC\fP フラグの説明を参照のこと。 .PP \fBpopen\fP() からの返り値は、通常の標準 I/O ストリームと同じであるが、 \fBfclose\fP(3) ではなく \fBpclose\fP() で閉じなくてはならないことだけが異なる。 このストリームへ書き込んだ結果はコマンドの標準入力に書き込まれる。 そして、コマンドの標準出力は、 コマンドそのものが置き換わってしまわない限り、 \fBpopen\fP() を呼んだプロセスの標準出力と同じことになる。 逆に、 ストリームからの読み込みは、 そのコマンドの標準出力を読み込むことになる。 そして、そのコマンドの標準入力は \fBpopen\fP() を呼んだプロセスの標準入力と同一である。 .PP デフォルトでは、 \fBpopen\fP() の出力ストリームは block buffered であることに注意すること。 .PP \fBpclose\fP() 関数は、(パイプに) 関連づけられたプロセスが終了するのを待ち、 \fBwait4\fP(2) によって返されたコマンドの終了状態を返す。 .SH 返り値 \fBpopen\fP(): on success, returns a pointer to an open stream that can be used to read or write to the pipe; if the \fBfork\fP(2) or \fBpipe\fP(2) calls fail, or if the function cannot allocate memory, NULL is returned. .PP .\" These conditions actually give undefined results, so I commented .\" them out. .\" .I stream .\" is not associated with a "popen()ed" command, if .\".I stream .\" already "pclose()d", or if \fBpclose\fP(): on success, returns the exit status of the command; if \fBwait4\fP(2) returns an error, or some other error is detected, \-1 is returned. .PP Both functions set \fIerrno\fP to an appropriate value in the case of an error. .SH エラー \fBpopen\fP() 関数は、メモリーアロケーションに失敗しても \fIerrno\fP をセットしない。 \fBpopen\fP() が中で呼び出す \fBfork\fP(2) や \fBpipe\fP(2) が失敗した場合には、 \fIerrno\fP が適切にセットされる。 引数 \fItype\fP が無効であり、この状態が検知された場合には、 \fIerrno\fP が \fBEINVAL\fP にセットされる。 .PP \fBpclose\fP() が、子プロセスの状態を取得できなかった場合、 \fIerrno\fP が \fBECHILD\fP にセットされる。 .SH 属性 この節で使用されている用語の説明については、 \fBattributes\fP(7) を参照。 .TS allbox; lbw17 lb lb l l l. インターフェース 属性 値 T{ \fBpopen\fP(), \fBpclose\fP() T} Thread safety MT\-Safe .TE .sp 1 .SH 準拠 POSIX.1\-2001, POSIX.1\-2008. .PP \fItype\fP に指定できる \(aqe\(aq は Linux での拡張である。 .SH 注意 \fBNote\fP: carefully read Caveats in \fBsystem\fP(3). .SH バグ 読み込みのために開かれたコマンドの標準入力は \fBpopen\fP(), を呼んだプロセスと一緒に、その読み取り位置を共有する。 そのため、もとのプロセスがバッファーリングされた読み取りを終了したら、 そのコマンドの入力位置は予想されたものには なっていないかもしれない。 同様に、書き込みのために開かれたコマンドからの出力は、 もとのプロセスの出力と混ざり合うことになるかもしれない。 後者は \fBpopen\fP() の前に \fBfflush\fP(3) を呼び出すことによって回避可能である。 .PP .\" .SH HISTORY .\" A .\" .BR popen () .\" and a .\" .BR pclose () .\" function appeared in Version 7 AT&T UNIX. シェルの実行の失敗は、 シェルがコマンドの実行に失敗したことや、 コマンドがすぐに終了してしまったことと、区別がつかない。 唯一のヒントは終了状態が 127 になることである。 .SH 関連項目 \fBsh\fP(1), \fBfork\fP(2), \fBpipe\fP(2), \fBwait4\fP(2), \fBfclose\fP(3), \fBfflush\fP(3), \fBfopen\fP(3), \fBstdio\fP(3), \fBsystem\fP(3) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。