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