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