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. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.\" @(#)popen.3 8.2 (Berkeley) 5/3/95 29.\" 30.Dd May 20, 2013 31.Dt POPEN 3 32.Os 33.Sh NAME 34.Nm popen , 35.Nm pclose 36.Nd process 37.Tn I/O 38.Sh LIBRARY 39.Lb libc 40.Sh SYNOPSIS 41.In stdio.h 42.Ft FILE * 43.Fn popen "const char *command" "const char *type" 44.Ft int 45.Fn pclose "FILE *stream" 46.Sh DESCRIPTION 47The 48.Fn popen 49function 50.Dq opens 51a process by creating a bidirectional pipe 52forking, 53and invoking the shell. 54Any streams opened by previous 55.Fn popen 56calls in the parent process are closed in the new child process. 57Historically, 58.Fn popen 59was implemented with a unidirectional pipe; 60hence many implementations of 61.Fn popen 62only allow the 63.Fa type 64argument to specify reading or writing, not both. 65Since 66.Fn popen 67is now implemented using a bidirectional pipe, the 68.Fa type 69argument may request a bidirectional data flow. 70The 71.Fa type 72argument is a pointer to a null-terminated string 73which must be 74.Ql r 75for reading, 76.Ql w 77for writing, or 78.Ql r+ 79for reading and writing. 80.Pp 81A letter 82.Ql e 83may be appended to that to request that the underlying file descriptor 84be set close-on-exec. 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