xref: /minix3/lib/libc/gen/popen.3 (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.\"	$NetBSD: popen.3,v 1.20 2015/01/21 08:35:31 wiz Exp $
22fe8fb19SBen Gras.\"
32fe8fb19SBen Gras.\" Copyright (c) 1991, 1993
42fe8fb19SBen Gras.\"	The Regents of the University of California.  All rights reserved.
52fe8fb19SBen Gras.\"
62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without
72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions
82fe8fb19SBen Gras.\" are met:
92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright
102fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer.
112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright
122fe8fb19SBen Gras.\"    notice, this list of conditions and the following disclaimer in the
132fe8fb19SBen Gras.\"    documentation and/or other materials provided with the distribution.
142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors
152fe8fb19SBen Gras.\"    may be used to endorse or promote products derived from this software
162fe8fb19SBen Gras.\"    without specific prior written permission.
172fe8fb19SBen Gras.\"
182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
212fe8fb19SBen Gras.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
282fe8fb19SBen Gras.\" SUCH DAMAGE.
292fe8fb19SBen Gras.\"
302fe8fb19SBen Gras.\"     @(#)popen.3	8.2 (Berkeley) 5/3/95
312fe8fb19SBen Gras.\"
32*0a6a1f1dSLionel Sambuc.Dd January 19, 2015
332fe8fb19SBen Gras.Dt POPEN 3
342fe8fb19SBen Gras.Os
352fe8fb19SBen Gras.Sh NAME
362fe8fb19SBen Gras.Nm popen ,
37*0a6a1f1dSLionel Sambuc.Nm popenve ,
382fe8fb19SBen Gras.Nm pclose
392fe8fb19SBen Gras.Nd process
402fe8fb19SBen Gras.Tn I/O
412fe8fb19SBen Gras.Sh LIBRARY
422fe8fb19SBen Gras.Lb libc
432fe8fb19SBen Gras.Sh SYNOPSIS
442fe8fb19SBen Gras.In stdio.h
452fe8fb19SBen Gras.Ft FILE *
462fe8fb19SBen Gras.Fn popen "const char *command" "const char *type"
47*0a6a1f1dSLionel Sambuc.Ft FILE *
48*0a6a1f1dSLionel Sambuc.Fn popenve "const char *path" "char * const *argv" "char * const *envp" "const char *type"
492fe8fb19SBen Gras.Ft int
502fe8fb19SBen Gras.Fn pclose "FILE *stream"
512fe8fb19SBen Gras.Sh DESCRIPTION
522fe8fb19SBen GrasThe
532fe8fb19SBen Gras.Fn popen
542fe8fb19SBen Grasfunction
552fe8fb19SBen Gras.Dq opens
562fe8fb19SBen Grasa process by creating an IPC connection,
572fe8fb19SBen Grasforking,
582fe8fb19SBen Grasand invoking the shell.
592fe8fb19SBen GrasHistorically,
602fe8fb19SBen Gras.Nm popen
612fe8fb19SBen Graswas implemented with a unidirectional pipe;
622fe8fb19SBen Grashence many implementations of
632fe8fb19SBen Gras.Nm popen
642fe8fb19SBen Grasonly allow the
652fe8fb19SBen Gras.Fa type
662fe8fb19SBen Grasargument to specify reading or writing, not both.
672fe8fb19SBen GrasSince
682fe8fb19SBen Gras.Nm popen
692fe8fb19SBen Grasis now implemented using sockets, the
702fe8fb19SBen Gras.Fa type
712fe8fb19SBen Grasmay request a bidirectional data flow.
722fe8fb19SBen GrasThe
732fe8fb19SBen Gras.Fa type
742fe8fb19SBen Grasargument is a pointer to a null-terminated string
752fe8fb19SBen Graswhich must be
762fe8fb19SBen Gras.Ql r
772fe8fb19SBen Grasfor reading,
782fe8fb19SBen Gras.Ql w
792fe8fb19SBen Grasfor writing, or
802fe8fb19SBen Gras.Ql r+
812fe8fb19SBen Grasfor reading and writing.
82f14fb602SLionel SambucIn addition if the character
83f14fb602SLionel Sambuc.Ql e
84f14fb602SLionel Sambucis present in the
85f14fb602SLionel Sambuc.Fa type
86f14fb602SLionel Sambucstring, the file descriptor used internally is set to be closed on
87f14fb602SLionel Sambuc.Xr exec 3 .
882fe8fb19SBen Gras.Pp
892fe8fb19SBen GrasThe
902fe8fb19SBen Gras.Fa command
912fe8fb19SBen Grasargument is a pointer to a null-terminated string
922fe8fb19SBen Grascontaining a shell command line.
932fe8fb19SBen GrasThis command is passed to
942fe8fb19SBen Gras.Pa /bin/sh
952fe8fb19SBen Grasusing the
962fe8fb19SBen Gras.Fl c
972fe8fb19SBen Grasflag; interpretation, if any, is performed by the shell.
982fe8fb19SBen Gras.Pp
99*0a6a1f1dSLionel SambucThe
100*0a6a1f1dSLionel Sambuc.Fn popenve
101*0a6a1f1dSLionel Sambucfunction is similar to
102*0a6a1f1dSLionel Sambuc.Fn popen
103*0a6a1f1dSLionel Sambucbut the first three arguments are passed
104*0a6a1f1dSLionel Sambucto
105*0a6a1f1dSLionel Sambuc.Xr execve 2
106*0a6a1f1dSLionel Sambucand there is no shell involved in the command invocation.
107*0a6a1f1dSLionel Sambuc.Pp
1082fe8fb19SBen GrasThe return value from
1092fe8fb19SBen Gras.Fn popen
110*0a6a1f1dSLionel Sambucand
111*0a6a1f1dSLionel Sambuc.Fn popenve
1122fe8fb19SBen Grasis a normal standard
1132fe8fb19SBen Gras.Tn I/O
1142fe8fb19SBen Grasstream in all respects
1152fe8fb19SBen Grassave that it must be closed with
1162fe8fb19SBen Gras.Fn pclose
1172fe8fb19SBen Grasrather than
1182fe8fb19SBen Gras.Fn fclose .
1192fe8fb19SBen GrasWriting to such a stream
1202fe8fb19SBen Graswrites to the standard input of the command;
1212fe8fb19SBen Grasthe command's standard output is the same as that of the process that called
1222fe8fb19SBen Gras.Fn popen ,
1232fe8fb19SBen Grasunless this is altered by the command itself.
1242fe8fb19SBen GrasConversely, reading from a
1252fe8fb19SBen Gras.Dq popened
1262fe8fb19SBen Grasstream reads the command's standard output, and
1272fe8fb19SBen Grasthe command's standard input is the same as that of the process that called
1282fe8fb19SBen Gras.Fn popen .
1292fe8fb19SBen Gras.Pp
1302fe8fb19SBen GrasNote that output
1312fe8fb19SBen Gras.Fn popen
1322fe8fb19SBen Grasstreams are fully buffered by default.
1332fe8fb19SBen Gras.Pp
1342fe8fb19SBen GrasThe
1352fe8fb19SBen Gras.Fn pclose
1362fe8fb19SBen Grasfunction waits for the associated process to terminate
1372fe8fb19SBen Grasand returns the exit status of the command
1382fe8fb19SBen Grasas returned by
1392fe8fb19SBen Gras.Fn wait4 .
1402fe8fb19SBen Gras.Sh RETURN VALUES
1412fe8fb19SBen GrasThe
1422fe8fb19SBen Gras.Fn popen
1432fe8fb19SBen Grasfunction returns
1442fe8fb19SBen Gras.Dv NULL
1452fe8fb19SBen Grasif the
146*0a6a1f1dSLionel Sambuc.Xr vfork 2 ,
1472fe8fb19SBen Gras.Xr pipe 2 ,
1482fe8fb19SBen Grasor
1492fe8fb19SBen Gras.Xr socketpair 2
1502fe8fb19SBen Grascalls fail,
151*0a6a1f1dSLionel Sambucor if it cannot allocate memory, preserving
152*0a6a1f1dSLionel Sambucthe errno from those functions.
1532fe8fb19SBen Gras.Pp
1542fe8fb19SBen GrasThe
1552fe8fb19SBen Gras.Fn pclose
1562fe8fb19SBen Grasfunction
1572fe8fb19SBen Grasreturns \-1 if
1582fe8fb19SBen Gras.Fa stream
1592fe8fb19SBen Grasis not associated with a
1602fe8fb19SBen Gras.Dq popened
1612fe8fb19SBen Grascommand, if
1622fe8fb19SBen Gras.Fa stream
1632fe8fb19SBen Grashas already been
1642fe8fb19SBen Gras.Dq pclosed ,
165*0a6a1f1dSLionel Sambucsetting errno to
166*0a6a1f1dSLionel Sambuc.Dv ESRCH
1672fe8fb19SBen Grasor if
1682fe8fb19SBen Gras.Xr wait4 2
169*0a6a1f1dSLionel Sambucreturns an error, preserving the errno returned by
170*0a6a1f1dSLionel Sambuc.Xr wait4 2 .
1712fe8fb19SBen Gras.Sh SEE ALSO
1722fe8fb19SBen Gras.Xr sh 1 ,
173*0a6a1f1dSLionel Sambuc.Xr execve 2 ,
1742fe8fb19SBen Gras.Xr fork 2 ,
1752fe8fb19SBen Gras.Xr pipe 2 ,
1762fe8fb19SBen Gras.Xr socketpair 2 ,
1772fe8fb19SBen Gras.Xr wait4 2 ,
1782fe8fb19SBen Gras.Xr fclose 3 ,
1792fe8fb19SBen Gras.Xr fflush 3 ,
1802fe8fb19SBen Gras.Xr fopen 3 ,
1812fe8fb19SBen Gras.Xr shquote 3 ,
1822fe8fb19SBen Gras.Xr stdio 3 ,
1832fe8fb19SBen Gras.Xr system 3
1842fe8fb19SBen Gras.Sh STANDARDS
1852fe8fb19SBen GrasThe
1862fe8fb19SBen Gras.Fn popen
1872fe8fb19SBen Grasand
1882fe8fb19SBen Gras.Fn pclose
1892fe8fb19SBen Grasfunctions conform to
1902fe8fb19SBen Gras.St -p1003.2-92 .
1912fe8fb19SBen Gras.Sh HISTORY
1922fe8fb19SBen GrasA
1932fe8fb19SBen Gras.Fn popen
1942fe8fb19SBen Grasand a
1952fe8fb19SBen Gras.Fn pclose
1962fe8fb19SBen Grasfunction appeared in
1972fe8fb19SBen Gras.At v7 .
1982fe8fb19SBen Gras.Sh BUGS
1992fe8fb19SBen GrasSince the standard input of a command opened for reading
2002fe8fb19SBen Grasshares its seek offset with the process that called
2012fe8fb19SBen Gras.Fn popen ,
2022fe8fb19SBen Grasif the original process has done a buffered read,
2032fe8fb19SBen Grasthe command's input position may not be as expected.
2042fe8fb19SBen GrasSimilarly, the output from a command opened for writing
2052fe8fb19SBen Grasmay become intermingled with that of the original process.
2062fe8fb19SBen GrasThe latter can be avoided by calling
2072fe8fb19SBen Gras.Xr fflush 3
2082fe8fb19SBen Grasbefore
2092fe8fb19SBen Gras.Fn popen .
2102fe8fb19SBen Gras.Pp
2112fe8fb19SBen GrasFailure to execute the shell
2122fe8fb19SBen Grasis indistinguishable from the shell's failure to execute command,
2132fe8fb19SBen Grasor an immediate exit of the command.
2142fe8fb19SBen GrasThe only hint is an exit status of 127.
2152fe8fb19SBen Gras.Pp
2162fe8fb19SBen GrasThe
2172fe8fb19SBen Gras.Fn popen
2182fe8fb19SBen Grasargument
2192fe8fb19SBen Grasalways calls
2202fe8fb19SBen Gras.Xr sh 1 ,
2212fe8fb19SBen Grasnever calls
2222fe8fb19SBen Gras.Xr csh 1 .
223*0a6a1f1dSLionel Sambuc.Pp
224*0a6a1f1dSLionel SambucThe
225*0a6a1f1dSLionel Sambuc.Fn popenve
226*0a6a1f1dSLionel Sambucfunction first appeared in
227*0a6a1f1dSLionel Sambuc.Nx 8 .
228