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. 1258f0484fSRodney W. Grimes.\" 4. 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.\" 281edb99c3SPeter Wemm.\" @(#)popen.3 8.2 (Berkeley) 5/3/95 297f3dea24SPeter Wemm.\" $FreeBSD$ 3058f0484fSRodney W. Grimes.\" 31*e9dec775SJilles Tjoelker.Dd May 20, 2013 3258f0484fSRodney W. Grimes.Dt POPEN 3 3358f0484fSRodney W. Grimes.Os 3458f0484fSRodney W. Grimes.Sh NAME 3558f0484fSRodney W. Grimes.Nm popen , 3658f0484fSRodney W. Grimes.Nm pclose 3758f0484fSRodney W. Grimes.Nd process 3858f0484fSRodney W. Grimes.Tn I/O 3925bb73e0SAlexey Zelkin.Sh LIBRARY 4025bb73e0SAlexey Zelkin.Lb libc 4158f0484fSRodney W. Grimes.Sh SYNOPSIS 4232eef9aeSRuslan Ermilov.In stdio.h 4358f0484fSRodney W. Grimes.Ft FILE * 4458f0484fSRodney W. Grimes.Fn popen "const char *command" "const char *type" 4558f0484fSRodney W. Grimes.Ft int 4658f0484fSRodney W. Grimes.Fn pclose "FILE *stream" 4758f0484fSRodney W. Grimes.Sh DESCRIPTION 4858f0484fSRodney W. GrimesThe 4958f0484fSRodney W. Grimes.Fn popen 5058f0484fSRodney W. Grimesfunction 5158f0484fSRodney W. Grimes.Dq opens 52e1dfe717SBruce Evansa process by creating a bidirectional pipe 5358f0484fSRodney W. Grimesforking, 5458f0484fSRodney W. Grimesand invoking the shell. 5541a3e3b0SJoseph KoshyAny streams opened by previous 5641a3e3b0SJoseph Koshy.Fn popen 5741a3e3b0SJoseph Koshycalls in the parent process are closed in the new child process. 581edb99c3SPeter WemmHistorically, 5941a3e3b0SJoseph Koshy.Fn popen 601edb99c3SPeter Wemmwas implemented with a unidirectional pipe; 611edb99c3SPeter Wemmhence many implementations of 6241a3e3b0SJoseph Koshy.Fn popen 631edb99c3SPeter Wemmonly allow the 6458f0484fSRodney W. Grimes.Fa type 651edb99c3SPeter Wemmargument to specify reading or writing, not both. 661edb99c3SPeter WemmSince 67bee0365aSJoseph Koshy.Fn popen 68e1dfe717SBruce Evansis now implemented using a bidirectional pipe, the 691edb99c3SPeter Wemm.Fa type 70e1dfe717SBruce Evansargument may request a bidirectional data flow. 711edb99c3SPeter WemmThe 721edb99c3SPeter Wemm.Fa type 731edb99c3SPeter Wemmargument is a pointer to a null-terminated string 741edb99c3SPeter Wemmwhich must be 751edb99c3SPeter Wemm.Ql r 761edb99c3SPeter Wemmfor reading, 771edb99c3SPeter Wemm.Ql w 781edb99c3SPeter Wemmfor writing, or 791edb99c3SPeter Wemm.Ql r+ 801edb99c3SPeter Wemmfor reading and writing. 8158f0484fSRodney W. Grimes.Pp 82*e9dec775SJilles TjoelkerA letter 83*e9dec775SJilles Tjoelker.Ql e 84*e9dec775SJilles Tjoelkermay be appended to that to request that the underlying file descriptor 85*e9dec775SJilles Tjoelkerbe set close-on-exec. 86*e9dec775SJilles Tjoelker.Pp 8758f0484fSRodney W. GrimesThe 8858f0484fSRodney W. Grimes.Fa command 8958f0484fSRodney W. Grimesargument is a pointer to a null-terminated string 9058f0484fSRodney W. Grimescontaining a shell command line. 9158f0484fSRodney W. GrimesThis command is passed to 9258f0484fSRodney W. Grimes.Pa /bin/sh 9358f0484fSRodney W. Grimesusing the 9458f0484fSRodney W. Grimes.Fl c 9558f0484fSRodney W. Grimesflag; interpretation, if any, is performed by the shell. 9658f0484fSRodney W. Grimes.Pp 9758f0484fSRodney W. GrimesThe return value from 9858f0484fSRodney W. Grimes.Fn popen 9958f0484fSRodney W. Grimesis a normal standard 10058f0484fSRodney W. Grimes.Tn I/O 10158f0484fSRodney W. Grimesstream in all respects 10258f0484fSRodney W. Grimessave that it must be closed with 10358f0484fSRodney W. Grimes.Fn pclose 10458f0484fSRodney W. Grimesrather than 10558f0484fSRodney W. Grimes.Fn fclose . 10658f0484fSRodney W. GrimesWriting to such a stream 10758f0484fSRodney W. Grimeswrites to the standard input of the command; 10858f0484fSRodney W. Grimesthe command's standard output is the same as that of the process that called 10958f0484fSRodney W. Grimes.Fn popen , 11058f0484fSRodney W. Grimesunless this is altered by the command itself. 11158f0484fSRodney W. GrimesConversely, reading from a 11258f0484fSRodney W. Grimes.Dq popened 11358f0484fSRodney W. Grimesstream reads the command's standard output, and 11458f0484fSRodney W. Grimesthe command's standard input is the same as that of the process that called 11558f0484fSRodney W. Grimes.Fn popen . 11658f0484fSRodney W. Grimes.Pp 11758f0484fSRodney W. GrimesNote that output 11858f0484fSRodney W. Grimes.Fn popen 11958f0484fSRodney W. Grimesstreams are fully buffered by default. 12058f0484fSRodney W. Grimes.Pp 12158f0484fSRodney W. GrimesThe 12258f0484fSRodney W. Grimes.Fn pclose 12358f0484fSRodney W. Grimesfunction waits for the associated process to terminate 12458f0484fSRodney W. Grimesand returns the exit status of the command 12558f0484fSRodney W. Grimesas returned by 1260d511e32SPhilippe Charnier.Xr wait4 2 . 127251c176fSRuslan Ermilov.Sh RETURN VALUES 12858f0484fSRodney W. GrimesThe 12958f0484fSRodney W. Grimes.Fn popen 13058f0484fSRodney W. Grimesfunction returns 13158f0484fSRodney W. Grimes.Dv NULL 13258f0484fSRodney W. Grimesif the 133e1dfe717SBruce Evans.Xr fork 2 13458f0484fSRodney W. Grimesor 135e1dfe717SBruce Evans.Xr pipe 2 13658f0484fSRodney W. Grimescalls fail, 13758f0484fSRodney W. Grimesor if it cannot allocate memory. 13858f0484fSRodney W. Grimes.Pp 13958f0484fSRodney W. GrimesThe 14058f0484fSRodney W. Grimes.Fn pclose 14158f0484fSRodney W. Grimesfunction 14258f0484fSRodney W. Grimesreturns \-1 if 14358f0484fSRodney W. Grimes.Fa stream 14458f0484fSRodney W. Grimesis not associated with a 14558f0484fSRodney W. Grimes.Dq popened 14658f0484fSRodney W. Grimescommand, if 14758f0484fSRodney W. Grimes.Fa stream 14858f0484fSRodney W. Grimesalready 14958f0484fSRodney W. Grimes.Dq pclosed , 15058f0484fSRodney W. Grimesor if 1510d511e32SPhilippe Charnier.Xr wait4 2 15258f0484fSRodney W. Grimesreturns an error. 15358f0484fSRodney W. Grimes.Sh ERRORS 15458f0484fSRodney W. GrimesThe 15558f0484fSRodney W. Grimes.Fn popen 15658f0484fSRodney W. Grimesfunction does not reliably set 15758f0484fSRodney W. Grimes.Va errno . 15858f0484fSRodney W. Grimes.Sh SEE ALSO 15958f0484fSRodney W. Grimes.Xr sh 1 , 16075141cc9SWolfram Schneider.Xr fork 2 , 16158f0484fSRodney W. Grimes.Xr pipe 2 , 16258f0484fSRodney W. Grimes.Xr wait4 2 , 16358f0484fSRodney W. Grimes.Xr fclose 3 , 16475141cc9SWolfram Schneider.Xr fflush 3 , 16558f0484fSRodney W. Grimes.Xr fopen 3 , 16658f0484fSRodney W. Grimes.Xr stdio 3 , 16758f0484fSRodney W. Grimes.Xr system 3 16824a0682cSRuslan Ermilov.Sh HISTORY 16924a0682cSRuslan ErmilovA 17024a0682cSRuslan Ermilov.Fn popen 17124a0682cSRuslan Ermilovand a 17224a0682cSRuslan Ermilov.Fn pclose 17324a0682cSRuslan Ermilovfunction appeared in 17424a0682cSRuslan Ermilov.At v7 . 17524a0682cSRuslan Ermilov.Pp 17624a0682cSRuslan ErmilovBidirectional functionality was added in 17724a0682cSRuslan Ermilov.Fx 2.2.6 . 17858f0484fSRodney W. Grimes.Sh BUGS 17958f0484fSRodney W. GrimesSince the standard input of a command opened for reading 18058f0484fSRodney W. Grimesshares its seek offset with the process that called 18158f0484fSRodney W. Grimes.Fn popen , 18258f0484fSRodney W. Grimesif the original process has done a buffered read, 18358f0484fSRodney W. Grimesthe command's input position may not be as expected. 18458f0484fSRodney W. GrimesSimilarly, the output from a command opened for writing 18558f0484fSRodney W. Grimesmay become intermingled with that of the original process. 18658f0484fSRodney W. GrimesThe latter can be avoided by calling 18758f0484fSRodney W. Grimes.Xr fflush 3 18858f0484fSRodney W. Grimesbefore 18958f0484fSRodney W. Grimes.Fn popen . 19058f0484fSRodney W. Grimes.Pp 19158f0484fSRodney W. GrimesFailure to execute the shell 19258f0484fSRodney W. Grimesis indistinguishable from the shell's failure to execute command, 19358f0484fSRodney W. Grimesor an immediate exit of the command. 19458f0484fSRodney W. GrimesThe only hint is an exit status of 127. 19558f0484fSRodney W. Grimes.Pp 19658f0484fSRodney W. GrimesThe 19758f0484fSRodney W. Grimes.Fn popen 198c0595bfcSTim J. Robbinsfunction 19958f0484fSRodney W. Grimesalways calls 200064f0074SMike Pritchard.Xr sh 1 , 20158f0484fSRodney W. Grimesnever calls 202064f0074SMike Pritchard.Xr csh 1 . 203