1.\" $OpenBSD: popen.3,v 1.16 2008/04/04 19:30:41 kurt 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. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.Dd $Mdocdate: April 4 2008 $ 31.Dt POPEN 3 32.Os 33.Sh NAME 34.Nm popen , 35.Nm pclose 36.Nd process 37.Tn I/O 38.Sh SYNOPSIS 39.Fd #include <stdio.h> 40.Ft FILE * 41.Fn popen "const char *command" "const char *type" 42.Ft int 43.Fn pclose "FILE *stream" 44.Sh DESCRIPTION 45The 46.Fn popen 47function 48.Dq opens 49a process by creating a pipe, forking, and invoking the shell. 50Since a pipe is by definition unidirectional, the 51.Fa type 52argument may specify only reading or writing, not both; 53the resulting stream is correspondingly read-only or write-only. 54.Pp 55The 56.Fa command 57argument is a pointer to a NUL-terminated 58string containing a shell command line. 59This command is passed to 60.Pa /bin/sh 61using the 62.Fl c 63flag; interpretation, if any, is performed by the shell. 64The 65.Fa type 66argument is a pointer to a NUL-terminated 67string which must be either 68.Qq r 69for reading 70or 71.Qq w 72for writing. 73.Pp 74The return value from 75.Fn popen 76is a normal standard 77.Tn I/O 78stream in all respects 79except that it must be closed with 80.Fn pclose 81rather than 82.Xr fclose 3 . 83Writing to such a stream 84writes to the standard input of the command; 85the command's standard output is the same as that of the process that called 86.Fn popen , 87unless this is altered by the command itself. 88Conversely, reading from a 89.Dq popened 90stream reads the command's standard output, and 91the command's standard input is the same as that of the process that called 92.Fn popen . 93.Pp 94Note that 95.Fn popen 96output streams are fully buffered by default. 97In addition, fork handlers established using 98.Xr pthread_atfork 3 99are not called when a multithreaded program calls 100.Fn popen . 101.Pp 102The 103.Fn pclose 104function waits for the associated process to terminate and returns the 105exit status of the command as returned by 106.Xr wait4 2 . 107.Sh RETURN VALUES 108The 109.Fn popen 110function returns 111.Dv NULL 112if the 113.Xr fork 2 114or 115.Xr pipe 2 116calls fail, 117or if it cannot allocate memory. 118.Pp 119The 120.Fn pclose 121function returns \-1 if 122.Fa stream 123is not associated with a 124.Dq popened 125command, if 126.Fa stream 127already 128.Dq pclosed , 129or if 130.Xr wait4 2 131returns an error. 132.Sh ERRORS 133The 134.Fn popen 135function does not reliably set 136.Va errno . 137.Sh SEE ALSO 138.Xr sh 1 , 139.Xr fork 2 , 140.Xr pipe 2 , 141.Xr wait4 2 , 142.Xr fclose 3 , 143.Xr fflush 3 , 144.Xr fopen 3 , 145.Xr stdio 3 , 146.Xr system 3 147.Sh HISTORY 148A 149.Fn popen 150and a 151.Fn pclose 152function appeared in 153.At v7 . 154.Sh BUGS 155Since the standard input of a command opened for reading 156shares its seek offset with the process that called 157.Fn popen , 158if the original process has done a buffered read, 159the command's input position may not be as expected. 160Similarly, the output from a command opened for writing 161may become intermingled with that of the original process. 162The latter can be avoided by calling 163.Xr fflush 3 164before 165.Fn popen . 166.Pp 167Failure to execute the shell is indistinguishable from the shell's 168failure to execute 169.Fa command , 170or an immediate exit of the command. 171The only hint is an exit status of 127. 172.Pp 173The 174.Fn popen 175argument always calls 176.Xr sh 1 . 177