Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/pclose.html
2024-02-19 00:21:47 -05:00

199 lines
7.9 KiB
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 The Open Group, All Rights Reserved -->
<title>pclose</title>
</head>
<body bgcolor="white">
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
</script>
<basefont size="3"> <a name="pclose"></a> <a name="tag_03_416"></a><!-- pclose -->
<!--header start-->
<center><font size="2">The Open Group Base Specifications Issue 6<br>
IEEE Std 1003.1-2001<br>
Copyright &copy; 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
<!--header end-->
<hr size="2" noshade>
<h4><a name="tag_03_416_01"></a>NAME</h4>
<blockquote>pclose - close a pipe stream to or from a process</blockquote>
<h4><a name="tag_03_416_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 &lt;<a href="../basedefs/stdio.h.html">stdio.h</a>&gt;<br>
<br>
int pclose(FILE *</tt><i>stream</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_416_03"></a>DESCRIPTION</h4>
<blockquote>
<p>The <i>pclose</i>() function shall close a stream that was opened by <a href="../functions/popen.html"><i>popen</i>()</a>, wait
for the command to terminate, and return the termination status of the process that was running the command language interpreter.
However, if a call caused the termination status to be unavailable to <i>pclose</i>(), then <i>pclose</i>() shall return -1 with
<i>errno</i> set to [ECHILD] to report this situation. This can happen if the application calls one of the following functions:</p>
<ul>
<li>
<p><a href="../functions/wait.html"><i>wait</i>()</a></p>
</li>
<li>
<p><a href="../functions/waitpid.html"><i>waitpid</i>()</a> with a <i>pid</i> argument less than or equal to 0 or equal to the
process ID of the command line interpreter</p>
</li>
<li>
<p>Any other function not defined in this volume of IEEE&nbsp;Std&nbsp;1003.1-2001 that could do one of the above</p>
</li>
</ul>
<p>In any case, <i>pclose</i>() shall not return before the child process created by <a href=
"../functions/popen.html"><i>popen</i>()</a> has terminated.</p>
<p>If the command language interpreter cannot be executed, the child termination status returned by <i>pclose</i>() shall be as if
the command language interpreter terminated using <i>exit</i>(127) or <i>_exit</i>(127).</p>
<p>The <i>pclose</i>() function shall not affect the termination status of any child of the calling process other than the one
created by <a href="../functions/popen.html"><i>popen</i>()</a> for the associated stream.</p>
<p>If the argument <i>stream</i> to <i>pclose</i>() is not a pointer to a stream created by <a href=
"../functions/popen.html"><i>popen</i>()</a>, the result of <i>pclose</i>() is undefined.</p>
</blockquote>
<h4><a name="tag_03_416_04"></a>RETURN VALUE</h4>
<blockquote>
<p>Upon successful return, <i>pclose</i>() shall return the termination status of the command language interpreter. Otherwise,
<i>pclose</i>() shall return -1 and set <i>errno</i> to indicate the error.</p>
</blockquote>
<h4><a name="tag_03_416_05"></a>ERRORS</h4>
<blockquote>
<p>The <i>pclose</i>() function shall fail if:</p>
<dl compact>
<dt>[ECHILD]</dt>
<dd>The status of the child process could not be obtained, as described above.</dd>
</dl>
</blockquote>
<hr>
<div class="box"><em>The following sections are informative.</em></div>
<h4><a name="tag_03_416_06"></a>EXAMPLES</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_416_07"></a>APPLICATION USAGE</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_416_08"></a>RATIONALE</h4>
<blockquote>
<p>There is a requirement that <i>pclose</i>() not return before the child process terminates. This is intended to disallow
implementations that return [EINTR] if a signal is received while waiting. If <i>pclose</i>() returned before the child terminated,
there would be no way for the application to discover which child used to be associated with the stream, and it could not do the
cleanup itself.</p>
<p>If the stream pointed to by <i>stream</i> was not created by <a href="../functions/popen.html"><i>popen</i>()</a>, historical
implementations of <i>pclose</i>() return -1 without setting <i>errno</i>. To avoid requiring <i>pclose</i>() to set <i>errno</i>
in this case, IEEE&nbsp;Std&nbsp;1003.1-2001 makes the behavior unspecified. An application should not use <i>pclose</i>() to close
any stream that was not created by <a href="../functions/popen.html"><i>popen</i>()</a>.</p>
<p>Some historical implementations of <i>pclose</i>() either block or ignore the signals SIGINT, SIGQUIT, and SIGHUP while waiting
for the child process to terminate. Since this behavior is not described for the <i>pclose</i>() function in
IEEE&nbsp;Std&nbsp;1003.1-2001, such implementations are not conforming. Also, some historical implementations return [EINTR] if a
signal is received, even though the child process has not terminated. Such implementations are also considered non-conforming.</p>
<p>Consider, for example, an application that uses:</p>
<pre>
<tt>popen("command", "r")
</tt>
</pre>
<p>to start <i>command</i>, which is part of the same application. The parent writes a prompt to its standard output (presumably
the terminal) and then reads from the <a href="../functions/popen.html"><i>popen</i>()</a>ed stream. The child reads the response
from the user, does some transformation on the response (pathname expansion, perhaps) and writes the result to its standard output.
The parent process reads the result from the pipe, does something with it, and prints another prompt. The cycle repeats. Assuming
that both processes do appropriate buffer flushing, this would be expected to work.</p>
<p>To conform to IEEE&nbsp;Std&nbsp;1003.1-2001, <i>pclose</i>() must use <a href="../functions/waitpid.html"><i>waitpid</i>()</a>,
or some similar function, instead of <a href="../functions/wait.html"><i>wait</i>()</a>.</p>
<p>The code sample below illustrates how the <i>pclose</i>() function might be implemented on a system conforming to
IEEE&nbsp;Std&nbsp;1003.1-2001.</p>
<pre>
<tt>int pclose(FILE *stream)
{
int stat;
pid_t pid;
<br>
pid = &lt;pid for process created for stream by popen()&gt;
(void) fclose(stream);
while (waitpid(pid, &amp;stat, 0) == -1) {
if (errno != EINTR){
stat = -1;
break;
}
}
return(stat);
}
</tt>
</pre>
</blockquote>
<h4><a name="tag_03_416_09"></a>FUTURE DIRECTIONS</h4>
<blockquote>
<p>None.</p>
</blockquote>
<h4><a name="tag_03_416_10"></a>SEE ALSO</h4>
<blockquote>
<p><a href="fork.html"><i>fork</i>()</a> , <a href="popen.html"><i>popen</i>()</a> , <a href="waitpid.html"><i>waitpid</i>()</a> ,
the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/stdio.h.html"><i>&lt;stdio.h&gt;</i></a></p>
</blockquote>
<h4><a name="tag_03_416_11"></a>CHANGE HISTORY</h4>
<blockquote>
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>