popenのヘルプ・マニュアル
日本語 英語
popen --help
man popen
POPEN(3) Linux Programmer’s Manual POPEN(3)
名前
popen, pclose - プロセスとの入力/出力用のパイプ・ストリーム
書式
#include
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE ||
_POSIX_SOURCE _BSD_SOURCE || _SVID_SOURCE
説明
popen() 関数は、プロセスをオープンする。具体的には、パイプを生成し、 フ
ォ ークを行い、シェルを起動する。定義から分かるように、パイプは一方向な
ので、 type 引き数には読み込みか書き込みのどちらか一方だけを指定でき る
( 両方は指定できない)。生成されるストリームは、この指定に対応して、読み
取り専用または書き込み専用のいずれかとなる。
command 引き数は、シェルのコマンドラインを含む NULL 終端された文字列 へ
のポインタである。このコマンドは -c フラグを用いて /bin/sh に渡される。
コマンドの解釈は (もし必要ならば) シェルによって行われる。 type 引き 数
は 、NULL 終端された文字列へのポインタで、読み込みを示す文字 'r' か、書
き込みを示す文字 'w' のどちらか一方を指定しなければならない。 glibc 2.9
以 降では、この引き数に文字 'e' を追加で指定できる。文字 'e' を指定する
と、対応するファイルディスクリプタにおいて 、 close-on-exec フ ラ グ
(FD_CLOEXEC) がセットされる。これが役に立つ理由については、 open(2) の
O_CLOEXEC フラグの説明を参照のこと。
popen() からの返り値は、通常の標準 I/O ストリームと同 じ で あ る が 、
fclose(3) ではなく pclose() で閉じなくてはならないことだけが異なる。こ
のストリームへ書き込んだ結果はコマンドの標準入力に書き込まれる。そし て
、 コマンドの標準出力は、コマンドそのものが置き換わってしまわない限り、
popen() を呼んだプロセスの標準出力と同じことになる 。 逆 に 、"popened"
(popen() によって開かれた) ストリームからの読み込みは、そのコマンドの標
準出力を読み込むことになる。そして、そのコマンドの標準入力は popen() を
呼んだプロセスの標準入力と同一である。
デフォルトでは、 popen() の出力ストリームは完全にバッファリングされるこ
とに注意しよう。
pclose() 関数は、(パイプに) 関連づけられたプロセスが終了するのを待ち 、
wait4(2) によって返されたコマンドの終了状態を返す。
返り値
popen() 関数は、 fork(2) または pipe(2) 呼び出しが失敗した場合や、メモ
リ割り当てができなかった場合、 NULL を返す。
pclose() 関数は、 wait4(2) がエラーを返したり、何か他のエラーが見つかっ
た場合、 -1 を返す。
エラー
popen() 関数は、メモリアロケーションに失敗しても errno をセットしない。
popen() が中で呼び出す fork(2) や pipe(2) が失敗した場合には、 errno が
適 切にセットされる。引き数 type が無効であり、この状態が検知された場合
には、 errno が EINVAL にセットされる。
pclose() が、子プロセスの状態を取得できなかった場合、 errno が ECHILD
にセットされる。
準拠
POSIX.1-2001.
type に指定できる 'e' は Linux での拡張である。
バグ
読 み込みのために開かれたコマンドの標準入力は popen(), を呼んだプロセス
と一緒に、その読み取り位置を共有する。そのため、もとのプロセスがバッ フ
ァ リングされた読み取りを終了したら、そのコマンドの入力位置は予想された
ものにはなっていないかもしれない。同様に、書き込みのために開かれたコ マ
ン ドからの出力は、もとのプロセスの出力と混ざり合うことになるかもしれな
い。後者は popen() の前に fflush(3) を呼び出すことによって回避可能で あ
る。
シ ェルの実行の失敗は、シェルがコマンドの実行に失敗したことや、コマンド
がすぐに終了してしまったことと、区別がつかない。唯一のヒントは終了状 態
が 127 になることである。
関連項目
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3),
stdio(3), system(3)
GNU 2008-10-10 POPEN(3)
POPEN(3) Linux Programmer’s Manual POPEN(3)
NAME
popen, pclose - pipe stream to or from a process
SYNOPSIS
#include
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
popen(), pclose(): _POSIX_C_SOURCE >= 2 || _XOPEN_SOURCE ||
_POSIX_SOURCE _BSD_SOURCE || _SVID_SOURCE
DESCRIPTION
The popen() function opens a process by creating a pipe, forking, and
invoking the shell. Since a pipe is by definition unidirectional, the
type argument may specify only reading or writing, not both; the
resulting stream is correspondingly read-only or write-only.
The command argument is a pointer to a null-terminated string contain-
ing a shell command line. This command is passed to /bin/sh using the
-c flag; interpretation, if any, is performed by the shell. The type
argument is a pointer to a null-terminated string which must contain
either the letter 'r' for reading or the letter 'w' for writing. Since
glibc 2.9, this argument can additionally include the letter 'e', which
causes the close-on-exec flag (FD_CLOEXEC) to be set on the underlying
file descriptor; see the description of the O_CLOEXEC flag in open(2)
for reasons why this may be useful.
The return value from popen() is a normal standard I/O stream in all
respects save that it must be closed with pclose() rather than
fclose(3). Writing to such a stream writes to the standard input of
the command; the command’s standard output is the same as that of the
process that called popen(), unless this is altered by the command
itself. Conversely, reading from a "popened" stream reads the com-
mand’s standard output, and the command’s standard input is the same as
that of the process that called popen().
Note that output popen() streams are fully buffered by default.
The pclose() function waits for the associated process to terminate and
returns the exit status of the command as returned by wait4(2).
RETURN VALUE
The popen() function returns NULL if the fork(2) or pipe(2) calls fail,
or if it cannot allocate memory.
The pclose() function returns -1 if wait4(2) returns an error, or some
other error is detected.
ERRORS
The popen() function does not set errno if memory allocation fails. If
the underlying fork(2) or pipe(2) fails, errno is set appropriately.
If the type argument is invalid, and this condition is detected, errno
is set to EINVAL.
If pclose() cannot obtain the child status, errno is set to ECHILD.
CONFORMING TO
POSIX.1-2001.
The 'e' value for type is a Linux extension.
BUGS
Since the standard input of a command opened for reading shares its
seek offset with the process that called popen(), if the original pro-
cess has done a buffered read, the command’s input position may not be
as expected. Similarly, the output from a command opened for writing
may become intermingled with that of the original process. The latter
can be avoided by calling fflush(3) before popen().
Failure to execute the shell is indistinguishable from the shell’s
failure to execute command, or an immediate exit of the command. The
only hint is an exit status of 127.
SEE ALSO
sh(1), fork(2), pipe(2), wait4(2), fclose(3), fflush(3), fopen(3),
stdio(3), system(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
GNU 2008-10-10 POPEN(3)