1.\" $NetBSD: popen.3,v 1.18 2011/06/27 08:21:07 wiz 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.\" @(#)popen.3 8.2 (Berkeley) 5/3/95 31.\" 32.Dd June 24, 2011 33.Dt POPEN 3 34.Os 35.Sh NAME 36.Nm popen , 37.Nm pclose 38.Nd process 39.Tn I/O 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In 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 an IPC connection, 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 sockets, the 67.Fa type 68may 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. 79In addition if the character 80.Ql e 81is present in the 82.Fa type 83string, the file descriptor used internally is set to be closed on 84.Xr exec 3 . 85.Pp 86The 87.Fa command 88argument is a pointer to a null-terminated string 89containing a shell command line. 90This command is passed to 91.Pa /bin/sh 92using the 93.Fl c 94flag; interpretation, if any, is performed by the shell. 95.Pp 96The return value from 97.Fn popen 98is a normal standard 99.Tn I/O 100stream in all respects 101save that it must be closed with 102.Fn pclose 103rather than 104.Fn fclose . 105Writing to such a stream 106writes to the standard input of the command; 107the command's standard output is the same as that of the process that called 108.Fn popen , 109unless this is altered by the command itself. 110Conversely, reading from a 111.Dq popened 112stream reads the command's standard output, and 113the command's standard input is the same as that of the process that called 114.Fn popen . 115.Pp 116Note that output 117.Fn popen 118streams are fully buffered by default. 119.Pp 120The 121.Fn pclose 122function waits for the associated process to terminate 123and returns the exit status of the command 124as returned by 125.Fn wait4 . 126.Sh RETURN VALUES 127The 128.Fn popen 129function returns 130.Dv NULL 131if the 132.Xr fork 2 , 133.Xr pipe 2 , 134or 135.Xr socketpair 2 136calls fail, 137or if it cannot allocate memory. 138.Pp 139The 140.Fn pclose 141function 142returns \-1 if 143.Fa stream 144is not associated with a 145.Dq popened 146command, if 147.Fa stream 148has already been 149.Dq pclosed , 150or if 151.Xr wait4 2 152returns an error. 153.Sh ERRORS 154The 155.Fn popen 156function does not reliably set 157.Va errno . 158.Sh SEE ALSO 159.Xr sh 1 , 160.Xr fork 2 , 161.Xr pipe 2 , 162.Xr socketpair 2 , 163.Xr wait4 2 , 164.Xr fclose 3 , 165.Xr fflush 3 , 166.Xr fopen 3 , 167.Xr shquote 3 , 168.Xr stdio 3 , 169.Xr system 3 170.Sh STANDARDS 171The 172.Fn popen 173and 174.Fn pclose 175functions conform to 176.St -p1003.2-92 . 177.Sh HISTORY 178A 179.Fn popen 180and a 181.Fn pclose 182function appeared in 183.At v7 . 184.Sh BUGS 185Since the standard input of a command opened for reading 186shares its seek offset with the process that called 187.Fn popen , 188if the original process has done a buffered read, 189the command's input position may not be as expected. 190Similarly, the output from a command opened for writing 191may become intermingled with that of the original process. 192The latter can be avoided by calling 193.Xr fflush 3 194before 195.Fn popen . 196.Pp 197Failure to execute the shell 198is indistinguishable from the shell's failure to execute command, 199or an immediate exit of the command. 200The only hint is an exit status of 127. 201.Pp 202The 203.Fn popen 204argument 205always calls 206.Xr sh 1 , 207never calls 208.Xr csh 1 . 209