man-pages/man3/popen.3.html
2021-03-31 01:06:50 +01:00

325 lines
7.0 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of POPEN</TITLE>
</HEAD><BODY>
<H1>POPEN</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
popen, pclose - pipe stream to or from a process
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;</B>
<B>FILE *popen(const char *</B><I>command</I><B>, const char *</B><I>type</I><B>);</B>
<B>int pclose(FILE *</B><I>stream</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>popen</B>(),
<B>pclose</B>():
<DL COMPACT><DT id="1"><DD>
_POSIX_C_SOURCE&nbsp;&gt;=&nbsp;2
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_BSD_SOURCE&nbsp;||&nbsp;_SVID_SOURCE
</DL>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>popen</B>()
function opens a process by creating a pipe, forking, and invoking the
shell.
Since a pipe is by definition unidirectional, the
<I>type</I>
argument may specify only reading or writing, not both; the resulting
stream is correspondingly read-only or write-only.
<P>
The
<I>command</I>
argument is a pointer to a null-terminated string containing a shell
command line.
This command is passed to
<I>/bin/sh</I>
using the
<B>-c</B>
flag; interpretation, if any, is performed by the shell.
<P>
The
<I>type</I>
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
(<B>FD_CLOEXEC</B>)
to be set on the underlying file descriptor;
see the description of the
<B>O_CLOEXEC</B>
flag in
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
for reasons why this may be useful.
<P>
The return value from
<B>popen</B>()
is a normal standard I/O stream in all respects save that it must be closed
with
<B>pclose</B>()
rather than
<B><A HREF="/cgi-bin/man/man2html?3+fclose">fclose</A></B>(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
<B>popen</B>(),
unless this is altered by the command itself.
Conversely, reading from
the stream reads the command's standard output, and the command's
standard input is the same as that of the process that called
<B>popen</B>().
<P>
Note that output
<B>popen</B>()
streams are block buffered by default.
<P>
The
<B>pclose</B>()
function waits for the associated process to terminate and returns the exit
status of the command as returned by
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2).
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
<B>popen</B>():
on success, returns a pointer to an open stream that
can be used to read or write to the pipe;
if the
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+pipe">pipe</A></B>(2)
calls fail, or if the function cannot allocate memory,
NULL is returned.
<P>
<B>pclose</B>():
on success, returns the exit status of the command; if
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2)
returns an error, or some other error is detected,
-1 is returned.
<P>
Both functions set
<I>errno</I>
to an appropriate value in the case of an error.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
The
<B>popen</B>()
function does not set
<I>errno</I>
if memory allocation fails.
If the underlying
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2)
or
<B><A HREF="/cgi-bin/man/man2html?2+pipe">pipe</A></B>(2)
fails,
<I>errno</I>
is set appropriately.
If the
<I>type</I>
argument is invalid, and this condition is detected,
<I>errno</I>
is set to
<B>EINVAL</B>.
<P>
If
<B>pclose</B>()
cannot obtain the child status,
<I>errno</I>
is set to
<B>ECHILD</B>.
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>popen</B>(),
<B>pclose</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<P>
The 'e' value for
<I>type</I>
is a Linux extension.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
<B>Note</B>:
carefully read Caveats in
<B><A HREF="/cgi-bin/man/man2html?3+system">system</A></B>(3).
<A NAME="lbAJ">&nbsp;</A>
<H2>BUGS</H2>
Since the standard input of a command opened for reading shares its seek
offset with the process that called
<B>popen</B>(),
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
<B><A HREF="/cgi-bin/man/man2html?3+fflush">fflush</A></B>(3)
before
<B>popen</B>().
<P>
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.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+sh">sh</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+pipe">pipe</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+wait4">wait4</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+fclose">fclose</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fflush">fflush</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+fopen">fopen</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+stdio">stdio</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+system">system</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="2"><A HREF="#lbAB">NAME</A><DD>
<DT id="3"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="4"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="5"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="6"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="7"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="8"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="9"><A HREF="#lbAI">NOTES</A><DD>
<DT id="10"><A HREF="#lbAJ">BUGS</A><DD>
<DT id="11"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="12"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:52 GMT, March 31, 2021
</BODY>
</HTML>