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 LIBRARY 44.Lb libc 45.Sh SYNOPSIS 46.In stdio.h 47.Ft FILE * 48.Fn popen "const char *command" "const char *type" 49.Ft int 50.Fn pclose "FILE *stream" 51.Sh DESCRIPTION 52The 53.Fn popen 54function 55.Dq opens 56a process by creating a bidirectional pipe 57forking, 58and invoking the shell. 59Any streams opened by previous 60.Fn popen 61calls in the parent process are closed in the new child process. 62Historically, 63.Fn popen 64was implemented with a unidirectional pipe; 65hence many implementations of 66.Fn popen 67only allow the 68.Fa type 69argument to specify reading or writing, not both. 70Since 71.Fn popen 72is now implemented using a bidirectional pipe, the 73.Fa type 74argument may request a bidirectional data flow. 75The 76.Fa type 77argument is a pointer to a null-terminated string 78which must be 79.Ql r 80for reading, 81.Ql w 82for writing, or 83.Ql r+ 84for reading and writing. 85.Pp 86The 87.Fa command 88argument is a pointer to a null-terminated string 89containing a shell command line. 90This command is passed to 91.Pa /bin/sh 92using the 93.Fl c 94flag; interpretation, if any, is performed by the shell. 95.Pp 96The return value from 97.Fn popen 98is a normal standard 99.Tn I/O 100stream in all respects 101save that it must be closed with 102.Fn pclose 103rather than 104.Fn fclose . 105Writing to such a stream 106writes to the standard input of the command; 107the command's standard output is the same as that of the process that called 108.Fn popen , 109unless this is altered by the command itself. 110Conversely, reading from a 111.Dq popened 112stream reads the command's standard output, and 113the command's standard input is the same as that of the process that called 114.Fn popen . 115.Pp 116Note that output 117.Fn popen 118streams are fully buffered by default. 119.Pp 120The 121.Fn pclose 122function waits for the associated process to terminate 123and returns the exit status of the command 124as returned by 125.Xr wait4 2 . 126.Sh RETURN VALUES 127The 128.Fn popen 129function returns 130.Dv NULL 131if the 132.Xr fork 2 133or 134.Xr pipe 2 135calls fail, 136or if it cannot allocate memory. 137.Pp 138The 139.Fn pclose 140function 141returns \-1 if 142.Fa stream 143is not associated with a 144.Dq popened 145command, if 146.Fa stream 147already 148.Dq pclosed , 149or if 150.Xr wait4 2 151returns an error. 152.Sh ERRORS 153The 154.Fn popen 155function does not reliably set 156.Va errno . 157.Sh SEE ALSO 158.Xr sh 1 , 159.Xr fork 2 , 160.Xr pipe 2 , 161.Xr wait4 2 , 162.Xr fclose 3 , 163.Xr fflush 3 , 164.Xr fopen 3 , 165.Xr stdio 3 , 166.Xr system 3 167.Sh HISTORY 168A 169.Fn popen 170and a 171.Fn pclose 172function appeared in 173.At v7 . 174.Pp 175Bidirectional functionality was added in 176.Fx 2.2.6 . 177.Sh BUGS 178Since the standard input of a command opened for reading 179shares its seek offset with the process that called 180.Fn popen , 181if the original process has done a buffered read, 182the command's input position may not be as expected. 183Similarly, the output from a command opened for writing 184may become intermingled with that of the original process. 185The latter can be avoided by calling 186.Xr fflush 3 187before 188.Fn popen . 189.Pp 190Failure to execute the shell 191is indistinguishable from the shell's failure to execute command, 192or an immediate exit of the command. 193The only hint is an exit status of 127. 194.Pp 195The 196.Fn popen 197function 198always calls 199.Xr sh 1 , 200never calls 201.Xr csh 1 . 202