xref: /freebsd/lib/libc/gen/popen.3 (revision 25bb73e063c17cd9048cf60100dbc0ac5177e94a)
158f0484fSRodney W. Grimes.\" Copyright (c) 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1258f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
1358f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1458f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1558f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
321edb99c3SPeter Wemm.\"     @(#)popen.3	8.2 (Berkeley) 5/3/95
337f3dea24SPeter Wemm.\" $FreeBSD$
3458f0484fSRodney W. Grimes.\"
351edb99c3SPeter Wemm.Dd May 3, 1995
3658f0484fSRodney W. Grimes.Dt POPEN 3
3758f0484fSRodney W. Grimes.Os
3858f0484fSRodney W. Grimes.Sh NAME
3958f0484fSRodney W. Grimes.Nm popen ,
4058f0484fSRodney W. Grimes.Nm pclose
4158f0484fSRodney W. Grimes.Nd process
4258f0484fSRodney W. Grimes.Tn I/O
4325bb73e0SAlexey Zelkin.Sh LIBRARY
4425bb73e0SAlexey Zelkin.Lb libc
4558f0484fSRodney W. Grimes.Sh SYNOPSIS
4658f0484fSRodney W. Grimes.Fd #include <stdio.h>
4758f0484fSRodney W. Grimes.Ft FILE *
4858f0484fSRodney W. Grimes.Fn popen "const char *command" "const char *type"
4958f0484fSRodney W. Grimes.Ft int
5058f0484fSRodney W. Grimes.Fn pclose "FILE *stream"
5158f0484fSRodney W. Grimes.Sh DESCRIPTION
5258f0484fSRodney W. GrimesThe
5358f0484fSRodney W. Grimes.Fn popen
5458f0484fSRodney W. Grimesfunction
5558f0484fSRodney W. Grimes.Dq opens
56e1dfe717SBruce Evansa process by creating a bidirectional pipe
5758f0484fSRodney W. Grimesforking,
5858f0484fSRodney W. Grimesand invoking the shell.
5941a3e3b0SJoseph KoshyAny streams opened by previous
6041a3e3b0SJoseph Koshy.Fn popen
6141a3e3b0SJoseph Koshycalls in the parent process are closed in the new child process.
621edb99c3SPeter WemmHistorically,
6341a3e3b0SJoseph Koshy.Fn popen
641edb99c3SPeter Wemmwas implemented with a unidirectional pipe;
651edb99c3SPeter Wemmhence many implementations of
6641a3e3b0SJoseph Koshy.Fn popen
671edb99c3SPeter Wemmonly allow the
6858f0484fSRodney W. Grimes.Fa type
691edb99c3SPeter Wemmargument to specify reading or writing, not both.
701edb99c3SPeter WemmSince
71bee0365aSJoseph Koshy.Fn popen
72e1dfe717SBruce Evansis now implemented using a bidirectional pipe, the
731edb99c3SPeter Wemm.Fa type
74e1dfe717SBruce Evansargument may request a bidirectional data flow.
751edb99c3SPeter WemmThe
761edb99c3SPeter Wemm.Fa type
771edb99c3SPeter Wemmargument is a pointer to a null-terminated string
781edb99c3SPeter Wemmwhich must be
791edb99c3SPeter Wemm.Ql r
801edb99c3SPeter Wemmfor reading,
811edb99c3SPeter Wemm.Ql w
821edb99c3SPeter Wemmfor writing, or
831edb99c3SPeter Wemm.Ql r+
841edb99c3SPeter Wemmfor reading and writing.
8558f0484fSRodney W. Grimes.Pp
8658f0484fSRodney W. GrimesThe
8758f0484fSRodney W. Grimes.Fa command
8858f0484fSRodney W. Grimesargument is a pointer to a null-terminated string
8958f0484fSRodney W. Grimescontaining a shell command line.
9058f0484fSRodney W. GrimesThis command is passed to
9158f0484fSRodney W. Grimes.Pa /bin/sh
9258f0484fSRodney W. Grimesusing the
9358f0484fSRodney W. Grimes.Fl c
9458f0484fSRodney W. Grimesflag; interpretation, if any, is performed by the shell.
9558f0484fSRodney W. Grimes.Pp
9658f0484fSRodney W. GrimesThe return value from
9758f0484fSRodney W. Grimes.Fn popen
9858f0484fSRodney W. Grimesis a normal standard
9958f0484fSRodney W. Grimes.Tn I/O
10058f0484fSRodney W. Grimesstream in all respects
10158f0484fSRodney W. Grimessave that it must be closed with
10258f0484fSRodney W. Grimes.Fn pclose
10358f0484fSRodney W. Grimesrather than
10458f0484fSRodney W. Grimes.Fn fclose .
10558f0484fSRodney W. GrimesWriting to such a stream
10658f0484fSRodney W. Grimeswrites to the standard input of the command;
10758f0484fSRodney W. Grimesthe command's standard output is the same as that of the process that called
10858f0484fSRodney W. Grimes.Fn popen ,
10958f0484fSRodney W. Grimesunless this is altered by the command itself.
11058f0484fSRodney W. GrimesConversely, reading from a
11158f0484fSRodney W. Grimes.Dq popened
11258f0484fSRodney W. Grimesstream reads the command's standard output, and
11358f0484fSRodney W. Grimesthe command's standard input is the same as that of the process that called
11458f0484fSRodney W. Grimes.Fn popen .
11558f0484fSRodney W. Grimes.Pp
11658f0484fSRodney W. GrimesNote that output
11758f0484fSRodney W. Grimes.Fn popen
11858f0484fSRodney W. Grimesstreams are fully buffered by default.
11958f0484fSRodney W. Grimes.Pp
12058f0484fSRodney W. GrimesThe
12158f0484fSRodney W. Grimes.Fn pclose
12258f0484fSRodney W. Grimesfunction waits for the associated process to terminate
12358f0484fSRodney W. Grimesand returns the exit status of the command
12458f0484fSRodney W. Grimesas returned by
12558f0484fSRodney W. Grimes.Fn wait4 .
12658f0484fSRodney W. Grimes.Sh RETURN VALUE
12758f0484fSRodney W. GrimesThe
12858f0484fSRodney W. Grimes.Fn popen
12958f0484fSRodney W. Grimesfunction returns
13058f0484fSRodney W. Grimes.Dv NULL
13158f0484fSRodney W. Grimesif the
132e1dfe717SBruce Evans.Xr fork 2
13358f0484fSRodney W. Grimesor
134e1dfe717SBruce Evans.Xr pipe 2
13558f0484fSRodney W. Grimescalls fail,
13658f0484fSRodney W. Grimesor if it cannot allocate memory.
13758f0484fSRodney W. Grimes.Pp
13858f0484fSRodney W. GrimesThe
13958f0484fSRodney W. Grimes.Fn pclose
14058f0484fSRodney W. Grimesfunction
14158f0484fSRodney W. Grimesreturns \-1 if
14258f0484fSRodney W. Grimes.Fa stream
14358f0484fSRodney W. Grimesis not associated with a
14458f0484fSRodney W. Grimes.Dq popened
14558f0484fSRodney W. Grimescommand, if
14658f0484fSRodney W. Grimes.Fa stream
14758f0484fSRodney W. Grimesalready
14858f0484fSRodney W. Grimes.Dq pclosed ,
14958f0484fSRodney W. Grimesor if
15058f0484fSRodney W. Grimes.Xr wait4
15158f0484fSRodney W. Grimesreturns an error.
15258f0484fSRodney W. Grimes.Sh ERRORS
15358f0484fSRodney W. GrimesThe
15458f0484fSRodney W. Grimes.Fn popen
15558f0484fSRodney W. Grimesfunction does not reliably set
15658f0484fSRodney W. Grimes.Va errno .
15758f0484fSRodney W. Grimes.Sh SEE ALSO
15858f0484fSRodney W. Grimes.Xr sh 1 ,
15975141cc9SWolfram Schneider.Xr fork 2 ,
16058f0484fSRodney W. Grimes.Xr pipe 2 ,
16158f0484fSRodney W. Grimes.Xr wait4 2 ,
16258f0484fSRodney W. Grimes.Xr fclose 3 ,
16375141cc9SWolfram Schneider.Xr fflush 3 ,
16458f0484fSRodney W. Grimes.Xr fopen 3 ,
16558f0484fSRodney W. Grimes.Xr stdio 3 ,
16658f0484fSRodney W. Grimes.Xr system 3
16758f0484fSRodney W. Grimes.Sh BUGS
16858f0484fSRodney W. GrimesSince the standard input of a command opened for reading
16958f0484fSRodney W. Grimesshares its seek offset with the process that called
17058f0484fSRodney W. Grimes.Fn popen ,
17158f0484fSRodney W. Grimesif the original process has done a buffered read,
17258f0484fSRodney W. Grimesthe command's input position may not be as expected.
17358f0484fSRodney W. GrimesSimilarly, the output from a command opened for writing
17458f0484fSRodney W. Grimesmay become intermingled with that of the original process.
17558f0484fSRodney W. GrimesThe latter can be avoided by calling
17658f0484fSRodney W. Grimes.Xr fflush 3
17758f0484fSRodney W. Grimesbefore
17858f0484fSRodney W. Grimes.Fn popen .
17958f0484fSRodney W. Grimes.Pp
18058f0484fSRodney W. GrimesFailure to execute the shell
18158f0484fSRodney W. Grimesis indistinguishable from the shell's failure to execute command,
18258f0484fSRodney W. Grimesor an immediate exit of the command.
18358f0484fSRodney W. GrimesThe only hint is an exit status of 127.
18458f0484fSRodney W. Grimes.Pp
18558f0484fSRodney W. GrimesThe
18658f0484fSRodney W. Grimes.Fn popen
18758f0484fSRodney W. Grimesargument
18858f0484fSRodney W. Grimesalways calls
189064f0074SMike Pritchard.Xr sh 1 ,
19058f0484fSRodney W. Grimesnever calls
191064f0074SMike Pritchard.Xr csh 1 .
19258f0484fSRodney W. Grimes.Sh HISTORY
19358f0484fSRodney W. GrimesA
19458f0484fSRodney W. Grimes.Fn popen
19558f0484fSRodney W. Grimesand a
19658f0484fSRodney W. Grimes.Fn pclose
19758f0484fSRodney W. Grimesfunction appeared in
19858f0484fSRodney W. Grimes.At v7 .
199a414d605SJordan K. Hubbard.br
200a414d605SJordan K. HubbardBidirectional functionality was added in
201a414d605SJordan K. Hubbard.Tn FreeBSD
202a414d605SJordan K. Hubbard2.2.6.
203