xref: /freebsd/lib/libc/gen/popen.3 (revision 1edb99c337e6a990213e5aaa9582d823d1208860)
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
3358f0484fSRodney W. Grimes.\"
341edb99c3SPeter Wemm.Dd May 3, 1995
3558f0484fSRodney W. Grimes.Dt POPEN 3
3658f0484fSRodney W. Grimes.Os
3758f0484fSRodney W. Grimes.Sh NAME
3858f0484fSRodney W. Grimes.Nm popen ,
3958f0484fSRodney W. Grimes.Nm pclose
4058f0484fSRodney W. Grimes.Nd process
4158f0484fSRodney W. Grimes.Tn I/O
4258f0484fSRodney W. Grimes.Sh SYNOPSIS
4358f0484fSRodney W. Grimes.Fd #include <stdio.h>
4458f0484fSRodney W. Grimes.Ft FILE *
4558f0484fSRodney W. Grimes.Fn popen "const char *command" "const char *type"
4658f0484fSRodney W. Grimes.Ft int
4758f0484fSRodney W. Grimes.Fn pclose "FILE *stream"
4858f0484fSRodney W. Grimes.Sh DESCRIPTION
4958f0484fSRodney W. GrimesThe
5058f0484fSRodney W. Grimes.Fn popen
5158f0484fSRodney W. Grimesfunction
5258f0484fSRodney W. Grimes.Dq opens
531edb99c3SPeter Wemma process by creating an IPC connection,
5458f0484fSRodney W. Grimesforking,
5558f0484fSRodney W. Grimesand invoking the shell.
561edb99c3SPeter WemmHistorically,
571edb99c3SPeter Wemm.Nm popen
581edb99c3SPeter Wemmwas implemented with a unidirectional pipe;
591edb99c3SPeter Wemmhence many implementations of
601edb99c3SPeter Wemm.Nm popen
611edb99c3SPeter Wemmonly allow the
6258f0484fSRodney W. Grimes.Fa type
631edb99c3SPeter Wemmargument to specify reading or writing, not both.
641edb99c3SPeter WemmSince
651edb99c3SPeter Wemm.Nm popen
661edb99c3SPeter Wemmis now implemented using sockets, the
671edb99c3SPeter Wemm.Fa type
681edb99c3SPeter Wemmmay request a bidirectional data flow.
691edb99c3SPeter WemmThe
701edb99c3SPeter Wemm.Fa type
711edb99c3SPeter Wemmargument is a pointer to a null-terminated string
721edb99c3SPeter Wemmwhich must be
731edb99c3SPeter Wemm.Ql r
741edb99c3SPeter Wemmfor reading,
751edb99c3SPeter Wemm.Ql w
761edb99c3SPeter Wemmfor writing, or
771edb99c3SPeter Wemm.Ql r+
781edb99c3SPeter Wemmfor reading and writing.
7958f0484fSRodney W. Grimes.Pp
8058f0484fSRodney W. GrimesThe
8158f0484fSRodney W. Grimes.Fa command
8258f0484fSRodney W. Grimesargument is a pointer to a null-terminated string
8358f0484fSRodney W. Grimescontaining a shell command line.
8458f0484fSRodney W. GrimesThis command is passed to
8558f0484fSRodney W. Grimes.Pa /bin/sh
8658f0484fSRodney W. Grimesusing the
8758f0484fSRodney W. Grimes.Fl c
8858f0484fSRodney W. Grimesflag; interpretation, if any, is performed by the shell.
8958f0484fSRodney W. Grimes.Pp
9058f0484fSRodney W. GrimesThe return value from
9158f0484fSRodney W. Grimes.Fn popen
9258f0484fSRodney W. Grimesis a normal standard
9358f0484fSRodney W. Grimes.Tn I/O
9458f0484fSRodney W. Grimesstream in all respects
9558f0484fSRodney W. Grimessave that it must be closed with
9658f0484fSRodney W. Grimes.Fn pclose
9758f0484fSRodney W. Grimesrather than
9858f0484fSRodney W. Grimes.Fn fclose .
9958f0484fSRodney W. GrimesWriting to such a stream
10058f0484fSRodney W. Grimeswrites to the standard input of the command;
10158f0484fSRodney W. Grimesthe command's standard output is the same as that of the process that called
10258f0484fSRodney W. Grimes.Fn popen ,
10358f0484fSRodney W. Grimesunless this is altered by the command itself.
10458f0484fSRodney W. GrimesConversely, reading from a
10558f0484fSRodney W. Grimes.Dq popened
10658f0484fSRodney W. Grimesstream reads the command's standard output, and
10758f0484fSRodney W. Grimesthe command's standard input is the same as that of the process that called
10858f0484fSRodney W. Grimes.Fn popen .
10958f0484fSRodney W. Grimes.Pp
11058f0484fSRodney W. GrimesNote that output
11158f0484fSRodney W. Grimes.Fn popen
11258f0484fSRodney W. Grimesstreams are fully buffered by default.
11358f0484fSRodney W. Grimes.Pp
11458f0484fSRodney W. GrimesThe
11558f0484fSRodney W. Grimes.Fn pclose
11658f0484fSRodney W. Grimesfunction waits for the associated process to terminate
11758f0484fSRodney W. Grimesand returns the exit status of the command
11858f0484fSRodney W. Grimesas returned by
11958f0484fSRodney W. Grimes.Fn wait4 .
12058f0484fSRodney W. Grimes.Sh RETURN VALUE
12158f0484fSRodney W. GrimesThe
12258f0484fSRodney W. Grimes.Fn popen
12358f0484fSRodney W. Grimesfunction returns
12458f0484fSRodney W. Grimes.Dv NULL
12558f0484fSRodney W. Grimesif the
1261edb99c3SPeter Wemm.Xr fork 2 ,
1271edb99c3SPeter Wemm.Xr pipe 2 ,
12858f0484fSRodney W. Grimesor
1291edb99c3SPeter Wemm.Xr socketpair 2
13058f0484fSRodney W. Grimescalls fail,
13158f0484fSRodney W. Grimesor if it cannot allocate memory.
13258f0484fSRodney W. Grimes.Pp
13358f0484fSRodney W. GrimesThe
13458f0484fSRodney W. Grimes.Fn pclose
13558f0484fSRodney W. Grimesfunction
13658f0484fSRodney W. Grimesreturns \-1 if
13758f0484fSRodney W. Grimes.Fa stream
13858f0484fSRodney W. Grimesis not associated with a
13958f0484fSRodney W. Grimes.Dq popened
14058f0484fSRodney W. Grimescommand, if
14158f0484fSRodney W. Grimes.Fa stream
14258f0484fSRodney W. Grimesalready
14358f0484fSRodney W. Grimes.Dq pclosed ,
14458f0484fSRodney W. Grimesor if
14558f0484fSRodney W. Grimes.Xr wait4
14658f0484fSRodney W. Grimesreturns an error.
14758f0484fSRodney W. Grimes.Sh ERRORS
14858f0484fSRodney W. GrimesThe
14958f0484fSRodney W. Grimes.Fn popen
15058f0484fSRodney W. Grimesfunction does not reliably set
15158f0484fSRodney W. Grimes.Va errno .
15258f0484fSRodney W. Grimes.Sh SEE ALSO
15358f0484fSRodney W. Grimes.Xr sh 1 ,
15475141cc9SWolfram Schneider.Xr fork 2 ,
15558f0484fSRodney W. Grimes.Xr pipe 2 ,
1561edb99c3SPeter Wemm.Xr socketpair 2 ,
15758f0484fSRodney W. Grimes.Xr wait4 2 ,
15858f0484fSRodney W. Grimes.Xr fclose 3 ,
15975141cc9SWolfram Schneider.Xr fflush 3 ,
16058f0484fSRodney W. Grimes.Xr fopen 3 ,
16158f0484fSRodney W. Grimes.Xr stdio 3 ,
16258f0484fSRodney W. Grimes.Xr system 3
16358f0484fSRodney W. Grimes.Sh BUGS
16458f0484fSRodney W. GrimesSince the standard input of a command opened for reading
16558f0484fSRodney W. Grimesshares its seek offset with the process that called
16658f0484fSRodney W. Grimes.Fn popen ,
16758f0484fSRodney W. Grimesif the original process has done a buffered read,
16858f0484fSRodney W. Grimesthe command's input position may not be as expected.
16958f0484fSRodney W. GrimesSimilarly, the output from a command opened for writing
17058f0484fSRodney W. Grimesmay become intermingled with that of the original process.
17158f0484fSRodney W. GrimesThe latter can be avoided by calling
17258f0484fSRodney W. Grimes.Xr fflush 3
17358f0484fSRodney W. Grimesbefore
17458f0484fSRodney W. Grimes.Fn popen .
17558f0484fSRodney W. Grimes.Pp
17658f0484fSRodney W. GrimesFailure to execute the shell
17758f0484fSRodney W. Grimesis indistinguishable from the shell's failure to execute command,
17858f0484fSRodney W. Grimesor an immediate exit of the command.
17958f0484fSRodney W. GrimesThe only hint is an exit status of 127.
18058f0484fSRodney W. Grimes.Pp
18158f0484fSRodney W. GrimesThe
18258f0484fSRodney W. Grimes.Fn popen
18358f0484fSRodney W. Grimesargument
18458f0484fSRodney W. Grimesalways calls
185064f0074SMike Pritchard.Xr sh 1 ,
18658f0484fSRodney W. Grimesnever calls
187064f0074SMike Pritchard.Xr csh 1 .
18858f0484fSRodney W. Grimes.Sh HISTORY
18958f0484fSRodney W. GrimesA
19058f0484fSRodney W. Grimes.Fn popen
19158f0484fSRodney W. Grimesand a
19258f0484fSRodney W. Grimes.Fn pclose
19358f0484fSRodney W. Grimesfunction appeared in
19458f0484fSRodney W. Grimes.At v7 .
195