1.\" Copyright 1991 The Regents of the University of California. 2.\" 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.\" from: @(#)popen.3 6.4 (Berkeley) 4/30/91 33.\" $Id: popen.3,v 1.2 1993/07/30 08:38:04 mycroft Exp $ 34.\" 35.Dd April 30, 1991 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 pipe, 55forking, 56and invoking the shell. 57Since a pipe is by definition unidirectional, the 58.Fa type 59argument may specify only reading or writing, not both; 60the resulting stream is correspondingly read-only or write-only. 61.Pp 62The 63.Fa command 64argument is a pointer to a null-terminated string 65containing a shell command line. 66This command is passed to 67.Pa /bin/sh 68using the 69.Fl c 70flag; interpretation, if any, is performed by the shell. 71The 72.Fa mode 73argument is a pointer to a null-terminated string 74which must be either 75.Ql r 76for reading 77or 78.Ql w 79for writing. 80.Pp 81The return value from 82.Fn popen 83is a normal standard 84.Tn I/O 85stream in all respects 86save that it must be closed with 87.Fn pclose 88rather than 89.Fn fclose . 90Writing to such a stream 91writes to the standard input of the command; 92the command's standard output is the same as that of the process that called 93.Fn popen , 94unless this is altered by the command itself. 95Conversely, reading from a 96.Dq popened 97stream reads the command's standard output, and 98the command's standard input is the same as that of the process that called 99.Fn popen . 100.Pp 101Note that output 102.Fn popen 103streams are fully buffered by default. 104.Pp 105The 106.Fn pclose 107function waits for the associated process to terminate 108and returns the exit status of the command 109as returned by 110.Fn wait4 . 111.Sh RETURN VALUE 112The 113.Fn popen 114function returns 115.Dv NULL 116if the 117.Xr fork 2 118or 119.Xr pipe 2 120calls fail, 121or if it cannot allocate memory. 122.Pp 123The 124.Fn pclose 125function 126returns \-1 if 127.Fa stream 128is not associated with a 129.Dq popened 130command, if 131.Fa stream 132already 133.Dq pclosed , 134or if 135.Xr wait4 136returns an error. 137.Sh ERRORS 138The 139.Fn popen 140function does not reliably set 141.Va errno . 142.Sh SEE ALSO 143.Xr fork 2 , 144.Xr sh 1 , 145.Xr pipe 2 , 146.Xr wait4 2 , 147.Xr fflush 3 , 148.Xr fclose 3 , 149.Xr fopen 3 , 150.Xr stdio 3 , 151.Xr system 3 152.Sh BUGS 153Since the standard input of a command opened for reading 154shares its seek offset with the process that called 155.Fn popen , 156if the original process has done a buffered read, 157the command's input position may not be as expected. 158Similarly, the output from a command opened for writing 159may become intermingled with that of the original process. 160The latter can be avoided by calling 161.Xr fflush 3 162before 163.Fn popen . 164.Pp 165Failure to execute the shell 166is indistinguishable from the shell's failure to execute command, 167or an immediate exit of the command. 168The only hint is an exit status of 127. 169.Pp 170The 171.Fn popen 172argument 173always calls 174.Xr sh , 175never calls 176.Xr csh . 177.Sh HISTORY 178A 179.Fn popen 180and a 181.Fn pclose 182function appeared in 183.At v7 . 184