pclose 中文man頁(yè)面
NAME
popen, pclose - process I/O
SYNOPSIS
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
描述
popen() 函數(shù) 用 創(chuàng)建管道 的 方式 啟動(dòng) 一個(gè) 進(jìn)程, 并調(diào)用 shell. 因?yàn)?管道 是被定義成 單向的, 所以 type 參數(shù) 只能 定義成 只讀 或者 只寫(xiě), 不能是 兩者同時(shí), 結(jié)果流 也相應(yīng)的 是 只讀 或者 只寫(xiě).
command 參數(shù) 是 一個(gè) 字符串指針, 指向的 是 一個(gè) 以 null 結(jié)束符 結(jié)尾的字符串, 這個(gè) 字符串 包含 一個(gè) shell 命令. 這個(gè)命令 被送到 /bin/sh 以 -c 參數(shù) 執(zhí)行, 即由 shell 來(lái)執(zhí)行. type 參數(shù) 也是 一個(gè) 指向 以 null 結(jié)束符 結(jié)尾的 字符串的指針, 這個(gè)字符串 必須是 'r' 或者 'w’ 來(lái)指明 是 讀還是寫(xiě).
popen() 函數(shù) 的 返回值 是 一個(gè) 普通的 標(biāo)準(zhǔn)I/O流, 它只能用 pclose() 函數(shù) 來(lái)關(guān)閉, 而不是 fclose(). 函數(shù). 向 這個(gè)流 的 寫(xiě)入 被轉(zhuǎn)化為 對(duì) command 命令的 標(biāo)準(zhǔn)輸入; 而 command 命令的 標(biāo)準(zhǔn)輸出 則是和 調(diào)用 popen(), 函數(shù) 的 進(jìn)程 相同,除非 這個(gè) 被command命令 自己 改變. 相反的, 讀取 一個(gè) “被popen了的” 流, 就相當(dāng)于 讀取 command 命令的 標(biāo)準(zhǔn)輸出, 而 command 的 標(biāo)準(zhǔn)輸入 則是和 調(diào)用 popen, 函數(shù)的 進(jìn)程 相同.
注意, popen 函數(shù)的 輸出流 默認(rèn)是 被全緩沖的.
pclose 函數(shù) 等待 相關(guān)的進(jìn)程 結(jié)束 并返回 一個(gè) command 命令的 退出狀態(tài), 就像 wait4 函數(shù) 一樣.
返回值
如果 fork(2) 或者 pipe(2) 調(diào)用 失敗, 或者 它 分配不到內(nèi)存, popen 函數(shù) 返回 NULL .
如果 wait4 返回 一個(gè) 錯(cuò)誤, 或者 其他什么 錯(cuò)誤 發(fā)生, pclose 函數(shù) 返回 一個(gè) -1.
錯(cuò)誤
如果 內(nèi)存空間 開(kāi)辟 失敗, popen 函數(shù) 并不設(shè)置 errno . 如果 內(nèi)部的 fork() 函數(shù) 或者 pipe() 函數(shù) 失敗, errno 則會(huì)被 適當(dāng)?shù)?設(shè)置. 如果 type 參數(shù) 是不可用的, 而且 這個(gè) 狀態(tài) 被偵測(cè)到, errno 將 被設(shè)置成 EINVAL.
如果 pclose() 函數(shù) 不能 偵測(cè)到 子進(jìn)程的狀態(tài), errno 將 被設(shè)置成 ECHILD.
適應(yīng)環(huán)境
POSIX.2
BUGS
因?yàn)?command 命令 讀取的 標(biāo)準(zhǔn)輸入 和 調(diào)用 popen() 函數(shù) 的 進(jìn)程 共享 一個(gè) “搜索偏移量”, 所以, 如果 原進(jìn)程 已經(jīng) 完成了 一個(gè) 緩沖的讀取, 那么 command 命令的 輸入位置 將是 不可預(yù)料的. 相似的, command 命令的 輸出 會(huì)和 原進(jìn)程的輸出 混雜在一起. 后者 可以 在調(diào)用 popen. 函數(shù)前 調(diào)用 fflush(3) 函數(shù) 來(lái)避免.
如果 這個(gè) shell 執(zhí)行 失敗, 將不能 辨別出 這個(gè)錯(cuò)誤 是由 shell 運(yùn)行 這個(gè) command 命令 失敗, 還是 command 命令 立即退出 引起的. 唯一的 一個(gè) “線索”, 就是 127 這個(gè)退出狀態(tài).
歷史
popen() 函數(shù)和 pclose() 函數(shù) (第一次) 出現(xiàn)在 Version 7 AT&T UNIX.
參考
fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), stdio(3), system(3)
#p#
NAME
popen, pclose - process I/O
SYNOPSIS
#include <stdio.h>
FILE *popen(const char *command, const char *type);
int pclose(FILE *stream);
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 containing 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 be either `r' for reading or `w' for writing.
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(). 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 command'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.
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 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() or pipe() 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.2
BUGS
Since the standard input of a command opened for reading shares its seek offset with the process that called popen(), if the original process 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.
HISTORY
A popen() and a pclose() function appeared in Version 7 AT&T UNIX.
SEE ALSO
fork(2), sh(1), pipe(2), wait4(2), fflush(3), fclose(3), fopen(3), stdio(3), system(3)