1.\" Copyright (c) 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. All advertising materials mentioning features or use of this software 13.\" must display the following acknowledgement: 14.\" This product includes software developed by the University of 15.\" California, Berkeley and its contributors. 16.\" 4. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.\" @(#)popen.3 8.2 (Berkeley) 5/3/95 33.\" $FreeBSD$ 34.\" 35.Dd May 3, 1995 36.Dt POPEN 3 37.Os 38.Sh NAME 39.Nm popen , 40.Nm pclose 41.Nd process 42.Tn I/O 43.Sh SYNOPSIS 44.Fd #include <stdio.h> 45.Ft FILE * 46.Fn popen "const char *command" "const char *type" 47.Ft int 48.Fn pclose "FILE *stream" 49.Sh DESCRIPTION 50The 51.Fn popen 52function 53.Dq opens 54a process by creating a bidirectional pipe 55forking, 56and invoking the shell. 57Any streams opened by previous 58.Fn popen 59calls in the parent process are closed in the new child process. 60Historically, 61.Fn popen 62was implemented with a unidirectional pipe; 63hence many implementations of 64.Fn popen 65only allow the 66.Fa type 67argument to specify reading or writing, not both. 68Since 69.Fn popen 70is now implemented using a bidirectional pipe, the 71.Fa type 72argument may request a bidirectional data flow. 73The 74.Fa type 75argument is a pointer to a null-terminated string 76which must be 77.Ql r 78for reading, 79.Ql w 80for writing, or 81.Ql r+ 82for reading and writing. 83.Pp 84The 85.Fa command 86argument is a pointer to a null-terminated string 87containing a shell command line. 88This command is passed to 89.Pa /bin/sh 90using the 91.Fl c 92flag; interpretation, if any, is performed by the shell. 93.Pp 94The return value from 95.Fn popen 96is a normal standard 97.Tn I/O 98stream in all respects 99save that it must be closed with 100.Fn pclose 101rather than 102.Fn fclose . 103Writing to such a stream 104writes to the standard input of the command; 105the command's standard output is the same as that of the process that called 106.Fn popen , 107unless this is altered by the command itself. 108Conversely, reading from a 109.Dq popened 110stream reads the command's standard output, and 111the command's standard input is the same as that of the process that called 112.Fn popen . 113.Pp 114Note that output 115.Fn popen 116streams are fully buffered by default. 117.Pp 118The 119.Fn pclose 120function waits for the associated process to terminate 121and returns the exit status of the command 122as returned by 123.Fn wait4 . 124.Sh RETURN VALUE 125The 126.Fn popen 127function returns 128.Dv NULL 129if the 130.Xr fork 2 131or 132.Xr pipe 2 133calls fail, 134or if it cannot allocate memory. 135.Pp 136The 137.Fn pclose 138function 139returns \-1 if 140.Fa stream 141is not associated with a 142.Dq popened 143command, if 144.Fa stream 145already 146.Dq pclosed , 147or if 148.Xr wait4 149returns an error. 150.Sh ERRORS 151The 152.Fn popen 153function does not reliably set 154.Va errno . 155.Sh SEE ALSO 156.Xr sh 1 , 157.Xr fork 2 , 158.Xr pipe 2 , 159.Xr wait4 2 , 160.Xr fclose 3 , 161.Xr fflush 3 , 162.Xr fopen 3 , 163.Xr stdio 3 , 164.Xr system 3 165.Sh BUGS 166Since the standard input of a command opened for reading 167shares its seek offset with the process that called 168.Fn popen , 169if the original process has done a buffered read, 170the command's input position may not be as expected. 171Similarly, the output from a command opened for writing 172may become intermingled with that of the original process. 173The latter can be avoided by calling 174.Xr fflush 3 175before 176.Fn popen . 177.Pp 178Failure to execute the shell 179is indistinguishable from the shell's failure to execute command, 180or an immediate exit of the command. 181The only hint is an exit status of 127. 182.Pp 183The 184.Fn popen 185argument 186always calls 187.Xr sh 1 , 188never calls 189.Xr csh 1 . 190.Sh HISTORY 191A 192.Fn popen 193and a 194.Fn pclose 195function appeared in 196.At v7 . 197.br 198Bidirectional functionality was added in 199.Tn FreeBSD 2002.2.6. 201