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