📄 popen.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>popen</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="popen"></a> <a name="tag_03_420"></a><!-- popen --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_420_01"></a>NAME</h4><blockquote>popen - initiate pipe streams to or from a process</blockquote><h4><a name="tag_03_420_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <<a href="../basedefs/stdio.h.html">stdio.h</a>><br><br> FILE *popen(const char *</tt><i>command</i><tt>, const char *</tt><i>mode</i><tt>); <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br></tt></blockquote><h4><a name="tag_03_420_03"></a>DESCRIPTION</h4><blockquote><p>The <i>popen</i>() function shall execute the command specified by the string <i>command</i>. It shall create a pipe between thecalling program and the executed command, and shall return a pointer to a stream that can be used to either read from or write tothe pipe.</p><p>The environment of the executed command shall be as if a child process were created within the <i>popen</i>() call using the <ahref="../functions/fork.html"><i>fork</i>()</a> function, and the child invoked the <a href="../utilities/sh.html"><i>sh</i></a>utility using the call:</p><pre><tt>execl(</tt><i>shell path</i><tt>, "sh", "-c",</tt> <i>command</i><tt>, (char *)0);</tt></pre><p>where <i>shell path</i> is an unspecified pathname for the <a href="../utilities/sh.html"><i>sh</i></a> utility.</p><p>The <i>popen</i>() function shall ensure that any streams from previous <i>popen</i>() calls that remain open in the parentprocess are closed in the new child process.</p><p>The <i>mode</i> argument to <i>popen</i>() is a string that specifies I/O mode:</p><ol><li><p>If <i>mode</i> is <i>r</i>, when the child process is started, its file descriptor STDOUT_FILENO shall be the writable end ofthe pipe, and the file descriptor <i>fileno</i>(<i>stream</i>) in the calling process, where <i>stream</i> is the stream pointerreturned by <i>popen</i>(), shall be the readable end of the pipe.</p></li><li><p>If <i>mode</i> is <i>w</i>, when the child process is started its file descriptor STDIN_FILENO shall be the readable end of thepipe, and the file descriptor <i>fileno</i>(<i>stream</i>) in the calling process, where <i>stream</i> is the stream pointerreturned by <i>popen</i>(), shall be the writable end of the pipe.</p></li><li><p>If <i>mode</i> is any other value, the result is undefined.</p></li></ol><p>After <i>popen</i>(), both the parent and the child process shall be capable of executing independently before eitherterminates.</p><p>Pipe streams are byte-oriented.</p></blockquote><h4><a name="tag_03_420_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>popen</i>() shall return a pointer to an open stream that can be used to read or write to thepipe. Otherwise, it shall return a null pointer and may set <i>errno</i> to indicate the error.</p></blockquote><h4><a name="tag_03_420_05"></a>ERRORS</h4><blockquote><p>The <i>popen</i>() function may fail if:</p><dl compact><dt>[EMFILE]</dt><dd>{FOPEN_MAX} or {STREAM_MAX} streams are currently open in the calling process.</dd><dt>[EINVAL]</dt><dd>The <i>mode</i> argument is invalid.</dd></dl><p>The <i>popen</i>() function may also set <i>errno</i> values as described by <a href="fork.html"><i>fork</i>()</a> or <a href="pipe.html"><i>pipe</i>()</a> .</p></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_420_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_420_07"></a>APPLICATION USAGE</h4><blockquote><p>Since open files are shared, a mode <i>r</i> command can be used as an input filter and a mode <i>w</i> command as an outputfilter.</p><p>Buffered reading before opening an input filter may leave the standard input of that filter mispositioned. Similar problems withan output filter may be prevented by careful buffer flushing; for example, with <a href="fflush.html"><i>fflush</i>()</a> .</p><p>A stream opened by <i>popen</i>() should be closed by <a href="../functions/pclose.html"><i>pclose</i>()</a>.</p><p>The behavior of <i>popen</i>() is specified for values of <i>mode</i> of <i>r</i> and <i>w</i>. Other modes such as <i>rb</i>and <i>wb</i> might be supported by specific implementations, but these would not be portable features. Note that historicalimplementations of <i>popen</i>() only check to see if the first character of <i>mode</i> is <i>r</i>. Thus, a <i>mode</i> of<i>robert the robot</i> would be treated as <i>mode</i> <i>r</i>, and a <i>mode</i> of <i>anything else</i> would be treated as<i>mode</i> <i>w</i>.</p><p>If the application calls <a href="../functions/waitpid.html"><i>waitpid</i>()</a> or <a href="../functions/waitid.html"><i>waitid</i>()</a> with a <i>pid</i> argument greater than 0, and it still has a stream that was calledwith <i>popen</i>() open, it must ensure that <i>pid</i> does not refer to the process started by <i>popen</i>().</p><p>To determine whether or not the environment specified in the Shell and Utilities volume of IEEE Std 1003.1-2001 ispresent, use the function call:</p><pre><tt>sysconf(_SC_2_VERSION)</tt></pre><p>(See <a href="sysconf.html"><i>sysconf</i>()</a> ).</p></blockquote><h4><a name="tag_03_420_08"></a>RATIONALE</h4><blockquote><p>The <i>popen</i>() function should not be used by programs that have set user (or group) ID privileges. The <a href="../functions/fork.html"><i>fork</i>()</a> and <i><a href="../functions/exec.html">exec</a></i> family of functions (except <ahref="../functions/execlp.html"><i>execlp</i>()</a> and <a href="../functions/execvp.html"><i>execvp</i>()</a>), should be usedinstead. This prevents any unforeseen manipulation of the environment of the user that could cause execution of commands notanticipated by the calling program.</p><p>If the original and <i>popen</i>()ed processes both intend to read or write or read and write a common file, and either will beusing FILE-type C functions ( <a href="../functions/fread.html"><i>fread</i>()</a>, <a href="../functions/fwrite.html"><i>fwrite</i>()</a>, and so on), the rules for sharing file handles must be observed (see <a href="xsh_chap02_05.html#tag_02_05_01"><i>Interaction of File Descriptors and Standard I/O Streams</i></a> ).</p></blockquote><h4><a name="tag_03_420_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_420_10"></a>SEE ALSO</h4><blockquote><p><a href="pclose.html"><i>pclose</i>()</a> , <a href="pipe.html"><i>pipe</i>()</a> , <a href="sysconf.html"><i>sysconf</i>()</a>, <a href="system.html"><i>system</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/stdio.h.html"><i><stdio.h></i></a>, the Shell and Utilities volume of IEEE Std 1003.1-2001, <a href="../utilities/sh.html"><i>sh</i></a></p></blockquote><h4><a name="tag_03_420_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_420_12"></a>Issue 5</h4><blockquote><p>A statement is added to the DESCRIPTION indicating that pipe streams are byte-oriented.</p></blockquote><h4><a name="tag_03_420_13"></a>Issue 6</h4><blockquote><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The optional [EMFILE] error condition is added.</p></li></ul></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -