1.\" $NetBSD: popen.3,v 1.10 2002/02/07 07:00:16 ross Exp $ 2.\" 3.\" Copyright (c) 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)popen.3 8.2 (Berkeley) 5/3/95 35.\" 36.Dd May 3, 1995 37.Dt POPEN 3 38.Os 39.Sh NAME 40.Nm popen , 41.Nm pclose 42.Nd process 43.Tn I/O 44.Sh LIBRARY 45.Lb libc 46.Sh SYNOPSIS 47.Fd #include \*[Lt]stdio.h\*[Gt] 48.Ft FILE * 49.Fn popen "const char *command" "const char *type" 50.Ft int 51.Fn pclose "FILE *stream" 52.Sh DESCRIPTION 53The 54.Fn popen 55function 56.Dq opens 57a process by creating an IPC connection, 58forking, 59and invoking the shell. 60Historically, 61.Nm popen 62was implemented with a unidirectional pipe; 63hence many implementations of 64.Nm popen 65only allow the 66.Fa type 67argument to specify reading or writing, not both. 68Since 69.Nm popen 70is now implemented using sockets, the 71.Fa type 72may 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 VALUES 125The 126.Fn popen 127function returns 128.Dv NULL 129if the 130.Xr fork 2 , 131.Xr pipe 2 , 132or 133.Xr socketpair 2 134calls fail, 135or if it cannot allocate memory. 136.Pp 137The 138.Fn pclose 139function 140returns \-1 if 141.Fa stream 142is not associated with a 143.Dq popened 144command, if 145.Fa stream 146already 147.Dq pclosed , 148or if 149.Xr wait4 2 150returns an error. 151.Sh ERRORS 152The 153.Fn popen 154function does not reliably set 155.Va errno . 156.Sh SEE ALSO 157.Xr sh 1 , 158.Xr fork 2 , 159.Xr pipe 2 , 160.Xr socketpair 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.Sh BUGS 175Since the standard input of a command opened for reading 176shares its seek offset with the process that called 177.Fn popen , 178if the original process has done a buffered read, 179the command's input position may not be as expected. 180Similarly, the output from a command opened for writing 181may become intermingled with that of the original process. 182The latter can be avoided by calling 183.Xr fflush 3 184before 185.Fn popen . 186.Pp 187Failure to execute the shell 188is indistinguishable from the shell's failure to execute command, 189or an immediate exit of the command. 190The only hint is an exit status of 127. 191.Pp 192The 193.Fn popen 194argument 195always calls 196.Xr sh 1 , 197never calls 198.Xr csh 1 . 199