xref: /freebsd/lib/libc/gen/popen.3 (revision 32eef9aeb1f39a1623cea55da147c89abbd5b9a5)
1.\" Copyright (c) 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)popen.3	8.2 (Berkeley) 5/3/95
33.\" $FreeBSD$
34.\"
35.Dd May 3, 1995
36.Dt POPEN 3
37.Os
38.Sh NAME
39.Nm popen ,
40.Nm pclose
41.Nd process
42.Tn I/O
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdio.h
47.Ft FILE *
48.Fn popen "const char *command" "const char *type"
49.Ft int
50.Fn pclose "FILE *stream"
51.Sh DESCRIPTION
52The
53.Fn popen
54function
55.Dq opens
56a process by creating a bidirectional pipe
57forking,
58and invoking the shell.
59Any streams opened by previous
60.Fn popen
61calls in the parent process are closed in the new child process.
62Historically,
63.Fn popen
64was implemented with a unidirectional pipe;
65hence many implementations of
66.Fn popen
67only allow the
68.Fa type
69argument to specify reading or writing, not both.
70Since
71.Fn popen
72is now implemented using a bidirectional pipe, the
73.Fa type
74argument may request a bidirectional data flow.
75The
76.Fa type
77argument is a pointer to a null-terminated string
78which must be
79.Ql r
80for reading,
81.Ql w
82for writing, or
83.Ql r+
84for reading and writing.
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
133or
134.Xr pipe 2
135calls fail,
136or if it cannot allocate memory.
137.Pp
138The
139.Fn pclose
140function
141returns \-1 if
142.Fa stream
143is not associated with a
144.Dq popened
145command, if
146.Fa stream
147already
148.Dq pclosed ,
149or if
150.Xr wait4
151returns an error.
152.Sh ERRORS
153The
154.Fn popen
155function does not reliably set
156.Va errno .
157.Sh SEE ALSO
158.Xr sh 1 ,
159.Xr fork 2 ,
160.Xr pipe 2 ,
161.Xr wait4 2 ,
162.Xr fclose 3 ,
163.Xr fflush 3 ,
164.Xr fopen 3 ,
165.Xr stdio 3 ,
166.Xr system 3
167.Sh BUGS
168Since the standard input of a command opened for reading
169shares its seek offset with the process that called
170.Fn popen ,
171if the original process has done a buffered read,
172the command's input position may not be as expected.
173Similarly, the output from a command opened for writing
174may become intermingled with that of the original process.
175The latter can be avoided by calling
176.Xr fflush 3
177before
178.Fn popen .
179.Pp
180Failure to execute the shell
181is indistinguishable from the shell's failure to execute command,
182or an immediate exit of the command.
183The only hint is an exit status of 127.
184.Pp
185The
186.Fn popen
187argument
188always calls
189.Xr sh 1 ,
190never calls
191.Xr csh 1 .
192.Sh HISTORY
193A
194.Fn popen
195and a
196.Fn pclose
197function appeared in
198.At v7 .
199.Pp
200Bidirectional functionality was added in
201.Fx 2.2.6 .
202