xref: /freebsd/lib/libc/gen/popen.3 (revision 61e21613)
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. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd May 20, 2013
29.Dt POPEN 3
30.Os
31.Sh NAME
32.Nm popen ,
33.Nm pclose
34.Nd process
35.Tn I/O
36.Sh LIBRARY
37.Lb libc
38.Sh SYNOPSIS
39.In stdio.h
40.Ft FILE *
41.Fn popen "const char *command" "const char *type"
42.Ft int
43.Fn pclose "FILE *stream"
44.Sh DESCRIPTION
45The
46.Fn popen
47function
48.Dq opens
49a process by creating a bidirectional pipe
50forking,
51and invoking the shell.
52Any streams opened by previous
53.Fn popen
54calls in the parent process are closed in the new child process.
55Historically,
56.Fn popen
57was implemented with a unidirectional pipe;
58hence many implementations of
59.Fn popen
60only allow the
61.Fa type
62argument to specify reading or writing, not both.
63Since
64.Fn popen
65is now implemented using a bidirectional pipe, the
66.Fa type
67argument may request a bidirectional data flow.
68The
69.Fa type
70argument is a pointer to a null-terminated string
71which must be
72.Ql r
73for reading,
74.Ql w
75for writing, or
76.Ql r+
77for reading and writing.
78.Pp
79A letter
80.Ql e
81may be appended to that to request that the underlying file descriptor
82be set close-on-exec.
83.Pp
84The
85.Fa command
86argument is a pointer to a null-terminated string
87containing a shell command line.
88This command is passed to
89.Pa /bin/sh
90using the
91.Fl c
92flag; interpretation, if any, is performed by the shell.
93.Pp
94The return value from
95.Fn popen
96is a normal standard
97.Tn I/O
98stream in all respects
99save that it must be closed with
100.Fn pclose
101rather than
102.Fn fclose .
103Writing to such a stream
104writes to the standard input of the command;
105the command's standard output is the same as that of the process that called
106.Fn popen ,
107unless this is altered by the command itself.
108Conversely, reading from a
109.Dq popened
110stream reads the command's standard output, and
111the command's standard input is the same as that of the process that called
112.Fn popen .
113.Pp
114Note that output
115.Fn popen
116streams are fully buffered by default.
117.Pp
118The
119.Fn pclose
120function waits for the associated process to terminate
121and returns the exit status of the command
122as returned by
123.Xr wait4 2 .
124.Sh RETURN VALUES
125The
126.Fn popen
127function returns
128.Dv NULL
129if the
130.Xr fork 2
131or
132.Xr pipe 2
133calls fail,
134or if it cannot allocate memory.
135.Pp
136The
137.Fn pclose
138function
139returns \-1 if
140.Fa stream
141is not associated with a
142.Dq popened
143command, if
144.Fa stream
145already
146.Dq pclosed ,
147or if
148.Xr wait4 2
149returns an error.
150.Sh ERRORS
151The
152.Fn popen
153function does not reliably set
154.Va errno .
155.Sh SEE ALSO
156.Xr sh 1 ,
157.Xr fork 2 ,
158.Xr pipe 2 ,
159.Xr wait4 2 ,
160.Xr fclose 3 ,
161.Xr fflush 3 ,
162.Xr fopen 3 ,
163.Xr stdio 3 ,
164.Xr system 3
165.Sh HISTORY
166A
167.Fn popen
168and a
169.Fn pclose
170function appeared in
171.At v7 .
172.Pp
173Bidirectional functionality was added in
174.Fx 2.2.6 .
175.Sh BUGS
176Since the standard input of a command opened for reading
177shares its seek offset with the process that called
178.Fn popen ,
179if the original process has done a buffered read,
180the command's input position may not be as expected.
181Similarly, the output from a command opened for writing
182may become intermingled with that of the original process.
183The latter can be avoided by calling
184.Xr fflush 3
185before
186.Fn popen .
187.Pp
188Failure to execute the shell
189is indistinguishable from the shell's failure to execute command,
190or an immediate exit of the command.
191The only hint is an exit status of 127.
192.Pp
193The
194.Fn popen
195function
196always calls
197.Xr sh 1 ,
198never calls
199.Xr csh 1 .
200