158f0484fSRodney W. Grimes.\" Copyright (c) 1991, 1993 258f0484fSRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 358f0484fSRodney W. Grimes.\" 458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions 658f0484fSRodney W. Grimes.\" are met: 758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 858f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 1058f0484fSRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 1158f0484fSRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12*fbbd9655SWarner Losh.\" 3. Neither the name of the University nor the names of its contributors 1358f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1458f0484fSRodney W. Grimes.\" without specific prior written permission. 1558f0484fSRodney W. Grimes.\" 1658f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1758f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1858f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1958f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2058f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2158f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2258f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2358f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2458f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2558f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2658f0484fSRodney W. Grimes.\" SUCH DAMAGE. 2758f0484fSRodney W. Grimes.\" 28e9dec775SJilles Tjoelker.Dd May 20, 2013 2958f0484fSRodney W. Grimes.Dt POPEN 3 3058f0484fSRodney W. Grimes.Os 3158f0484fSRodney W. Grimes.Sh NAME 3258f0484fSRodney W. Grimes.Nm popen , 3358f0484fSRodney W. Grimes.Nm pclose 3458f0484fSRodney W. Grimes.Nd process 3558f0484fSRodney W. Grimes.Tn I/O 3625bb73e0SAlexey Zelkin.Sh LIBRARY 3725bb73e0SAlexey Zelkin.Lb libc 3858f0484fSRodney W. Grimes.Sh SYNOPSIS 3932eef9aeSRuslan Ermilov.In stdio.h 4058f0484fSRodney W. Grimes.Ft FILE * 4158f0484fSRodney W. Grimes.Fn popen "const char *command" "const char *type" 4258f0484fSRodney W. Grimes.Ft int 4358f0484fSRodney W. Grimes.Fn pclose "FILE *stream" 4458f0484fSRodney W. Grimes.Sh DESCRIPTION 4558f0484fSRodney W. GrimesThe 4658f0484fSRodney W. Grimes.Fn popen 4758f0484fSRodney W. Grimesfunction 4858f0484fSRodney W. Grimes.Dq opens 49e1dfe717SBruce Evansa process by creating a bidirectional pipe 5058f0484fSRodney W. Grimesforking, 5158f0484fSRodney W. Grimesand invoking the shell. 5241a3e3b0SJoseph KoshyAny streams opened by previous 5341a3e3b0SJoseph Koshy.Fn popen 5441a3e3b0SJoseph Koshycalls in the parent process are closed in the new child process. 551edb99c3SPeter WemmHistorically, 5641a3e3b0SJoseph Koshy.Fn popen 571edb99c3SPeter Wemmwas implemented with a unidirectional pipe; 581edb99c3SPeter Wemmhence many implementations of 5941a3e3b0SJoseph Koshy.Fn popen 601edb99c3SPeter Wemmonly allow the 6158f0484fSRodney W. Grimes.Fa type 621edb99c3SPeter Wemmargument to specify reading or writing, not both. 631edb99c3SPeter WemmSince 64bee0365aSJoseph Koshy.Fn popen 65e1dfe717SBruce Evansis now implemented using a bidirectional pipe, the 661edb99c3SPeter Wemm.Fa type 67e1dfe717SBruce Evansargument may request a bidirectional data flow. 681edb99c3SPeter WemmThe 691edb99c3SPeter Wemm.Fa type 701edb99c3SPeter Wemmargument is a pointer to a null-terminated string 711edb99c3SPeter Wemmwhich must be 721edb99c3SPeter Wemm.Ql r 731edb99c3SPeter Wemmfor reading, 741edb99c3SPeter Wemm.Ql w 751edb99c3SPeter Wemmfor writing, or 761edb99c3SPeter Wemm.Ql r+ 771edb99c3SPeter Wemmfor reading and writing. 7858f0484fSRodney W. Grimes.Pp 79e9dec775SJilles TjoelkerA letter 80e9dec775SJilles Tjoelker.Ql e 81e9dec775SJilles Tjoelkermay be appended to that to request that the underlying file descriptor 82e9dec775SJilles Tjoelkerbe set close-on-exec. 83e9dec775SJilles Tjoelker.Pp 8458f0484fSRodney W. GrimesThe 8558f0484fSRodney W. Grimes.Fa command 8658f0484fSRodney W. Grimesargument is a pointer to a null-terminated string 8758f0484fSRodney W. Grimescontaining a shell command line. 8858f0484fSRodney W. GrimesThis command is passed to 8958f0484fSRodney W. Grimes.Pa /bin/sh 9058f0484fSRodney W. Grimesusing the 9158f0484fSRodney W. Grimes.Fl c 9258f0484fSRodney W. Grimesflag; interpretation, if any, is performed by the shell. 9358f0484fSRodney W. Grimes.Pp 9458f0484fSRodney W. GrimesThe return value from 9558f0484fSRodney W. Grimes.Fn popen 9658f0484fSRodney W. Grimesis a normal standard 9758f0484fSRodney W. Grimes.Tn I/O 9858f0484fSRodney W. Grimesstream in all respects 9958f0484fSRodney W. Grimessave that it must be closed with 10058f0484fSRodney W. Grimes.Fn pclose 10158f0484fSRodney W. Grimesrather than 10258f0484fSRodney W. Grimes.Fn fclose . 10358f0484fSRodney W. GrimesWriting to such a stream 10458f0484fSRodney W. Grimeswrites to the standard input of the command; 10558f0484fSRodney W. Grimesthe command's standard output is the same as that of the process that called 10658f0484fSRodney W. Grimes.Fn popen , 10758f0484fSRodney W. Grimesunless this is altered by the command itself. 10858f0484fSRodney W. GrimesConversely, reading from a 10958f0484fSRodney W. Grimes.Dq popened 11058f0484fSRodney W. Grimesstream reads the command's standard output, and 11158f0484fSRodney W. Grimesthe command's standard input is the same as that of the process that called 11258f0484fSRodney W. Grimes.Fn popen . 11358f0484fSRodney W. Grimes.Pp 11458f0484fSRodney W. GrimesNote that output 11558f0484fSRodney W. Grimes.Fn popen 11658f0484fSRodney W. Grimesstreams are fully buffered by default. 11758f0484fSRodney W. Grimes.Pp 11858f0484fSRodney W. GrimesThe 11958f0484fSRodney W. Grimes.Fn pclose 12058f0484fSRodney W. Grimesfunction waits for the associated process to terminate 12158f0484fSRodney W. Grimesand returns the exit status of the command 12258f0484fSRodney W. Grimesas returned by 1230d511e32SPhilippe Charnier.Xr wait4 2 . 124251c176fSRuslan Ermilov.Sh RETURN VALUES 12558f0484fSRodney W. GrimesThe 12658f0484fSRodney W. Grimes.Fn popen 12758f0484fSRodney W. Grimesfunction returns 12858f0484fSRodney W. Grimes.Dv NULL 12958f0484fSRodney W. Grimesif the 130e1dfe717SBruce Evans.Xr fork 2 13158f0484fSRodney W. Grimesor 132e1dfe717SBruce Evans.Xr pipe 2 13358f0484fSRodney W. Grimescalls fail, 13458f0484fSRodney W. Grimesor if it cannot allocate memory. 13558f0484fSRodney W. Grimes.Pp 13658f0484fSRodney W. GrimesThe 13758f0484fSRodney W. Grimes.Fn pclose 13858f0484fSRodney W. Grimesfunction 13958f0484fSRodney W. Grimesreturns \-1 if 14058f0484fSRodney W. Grimes.Fa stream 14158f0484fSRodney W. Grimesis not associated with a 14258f0484fSRodney W. Grimes.Dq popened 14358f0484fSRodney W. Grimescommand, if 14458f0484fSRodney W. Grimes.Fa stream 14558f0484fSRodney W. Grimesalready 14658f0484fSRodney W. Grimes.Dq pclosed , 14758f0484fSRodney W. Grimesor if 1480d511e32SPhilippe Charnier.Xr wait4 2 14958f0484fSRodney W. Grimesreturns an error. 15058f0484fSRodney W. Grimes.Sh ERRORS 15158f0484fSRodney W. GrimesThe 15258f0484fSRodney W. Grimes.Fn popen 15358f0484fSRodney W. Grimesfunction does not reliably set 15458f0484fSRodney W. Grimes.Va errno . 15558f0484fSRodney W. Grimes.Sh SEE ALSO 15658f0484fSRodney W. Grimes.Xr sh 1 , 15775141cc9SWolfram Schneider.Xr fork 2 , 15858f0484fSRodney W. Grimes.Xr pipe 2 , 15958f0484fSRodney W. Grimes.Xr wait4 2 , 16058f0484fSRodney W. Grimes.Xr fclose 3 , 16175141cc9SWolfram Schneider.Xr fflush 3 , 16258f0484fSRodney W. Grimes.Xr fopen 3 , 16358f0484fSRodney W. Grimes.Xr stdio 3 , 16458f0484fSRodney W. Grimes.Xr system 3 16524a0682cSRuslan Ermilov.Sh HISTORY 16624a0682cSRuslan ErmilovA 16724a0682cSRuslan Ermilov.Fn popen 16824a0682cSRuslan Ermilovand a 16924a0682cSRuslan Ermilov.Fn pclose 17024a0682cSRuslan Ermilovfunction appeared in 17124a0682cSRuslan Ermilov.At v7 . 17224a0682cSRuslan Ermilov.Pp 17324a0682cSRuslan ErmilovBidirectional functionality was added in 17424a0682cSRuslan Ermilov.Fx 2.2.6 . 17558f0484fSRodney W. Grimes.Sh BUGS 17658f0484fSRodney W. GrimesSince the standard input of a command opened for reading 17758f0484fSRodney W. Grimesshares its seek offset with the process that called 17858f0484fSRodney W. Grimes.Fn popen , 17958f0484fSRodney W. Grimesif the original process has done a buffered read, 18058f0484fSRodney W. Grimesthe command's input position may not be as expected. 18158f0484fSRodney W. GrimesSimilarly, the output from a command opened for writing 18258f0484fSRodney W. Grimesmay become intermingled with that of the original process. 18358f0484fSRodney W. GrimesThe latter can be avoided by calling 18458f0484fSRodney W. Grimes.Xr fflush 3 18558f0484fSRodney W. Grimesbefore 18658f0484fSRodney W. Grimes.Fn popen . 18758f0484fSRodney W. Grimes.Pp 18858f0484fSRodney W. GrimesFailure to execute the shell 18958f0484fSRodney W. Grimesis indistinguishable from the shell's failure to execute command, 19058f0484fSRodney W. Grimesor an immediate exit of the command. 19158f0484fSRodney W. GrimesThe only hint is an exit status of 127. 19258f0484fSRodney W. Grimes.Pp 19358f0484fSRodney W. GrimesThe 19458f0484fSRodney W. Grimes.Fn popen 195c0595bfcSTim J. Robbinsfunction 19658f0484fSRodney W. Grimesalways calls 197064f0074SMike Pritchard.Xr sh 1 , 19858f0484fSRodney W. Grimesnever calls 199064f0074SMike Pritchard.Xr csh 1 . 200