xref: /openbsd-src/lib/libc/gen/popen.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: popen.3,v 1.10 2001/07/17 17:08:29 aaron Exp $
2.\"
3.\" Copyright (c) 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd June 4, 1993
35.Dt POPEN 3
36.Os
37.Sh NAME
38.Nm popen ,
39.Nm pclose
40.Nd process
41.Tn I/O
42.Sh SYNOPSIS
43.Fd #include <stdio.h>
44.Ft FILE *
45.Fn popen "const char *command" "const char *type"
46.Ft int
47.Fn pclose "FILE *stream"
48.Sh DESCRIPTION
49The
50.Fn popen
51function
52.Dq opens
53a process by creating a pipe, forking, and invoking the shell.
54Since a pipe is by definition unidirectional, the
55.Fa type
56argument may specify only reading or writing, not both;
57the resulting stream is correspondingly read-only or write-only.
58.Pp
59The
60.Fa command
61argument is a pointer to a null-terminated
62string containing a shell command line.
63This command is passed to
64.Pa /bin/sh
65using the
66.Fl c
67flag; interpretation, if any, is performed by the shell.
68The
69.Fa type
70argument is a pointer to a null-terminated
71string which must be either
72.Qq r
73for reading
74or
75.Qq w
76for writing.
77.Pp
78The return value from
79.Fn popen
80is a normal standard
81.Tn I/O
82stream in all respects
83except that it must be closed with
84.Fn pclose
85rather than
86.Fn fclose .
87Writing to such a stream
88writes to the standard input of the command;
89the command's standard output is the same as that of the process that called
90.Fn popen ,
91unless this is altered by the command itself.
92Conversely, reading from a
93.Dq popened
94stream reads the command's standard output, and
95the command's standard input is the same as that of the process that called
96.Fn popen .
97.Pp
98Note that output
99.Fn popen
100streams are fully buffered by default.
101.Pp
102The
103.Fn pclose
104function waits for the associated process to terminate and returns the
105exit status of the command as returned by
106.Xr wait4 2 .
107.Sh RETURN VALUES
108The
109.Fn popen
110function returns
111.Dv NULL
112if the
113.Xr fork 2
114or
115.Xr pipe 2
116calls fail,
117or if it cannot allocate memory.
118.Pp
119The
120.Fn pclose
121function returns \-1 if
122.Fa stream
123is not associated with a
124.Dq popened
125command, if
126.Fa stream
127already
128.Dq pclosed ,
129or if
130.Xr wait4
131returns an error.
132.Sh ERRORS
133The
134.Fn popen
135function does not reliably set
136.Va errno .
137.Sh SEE ALSO
138.Xr sh 1 ,
139.Xr fork 2 ,
140.Xr pipe 2 ,
141.Xr wait4 2 ,
142.Xr fclose 3 ,
143.Xr fflush 3 ,
144.Xr fopen 3 ,
145.Xr stdio 3 ,
146.Xr system 3
147.Sh BUGS
148Since the standard input of a command opened for reading
149shares its seek offset with the process that called
150.Fn popen ,
151if the original process has done a buffered read,
152the command's input position may not be as expected.
153Similarly, the output from a command opened for writing
154may become intermingled with that of the original process.
155The latter can be avoided by calling
156.Xr fflush 3
157before
158.Fn popen .
159.Pp
160Failure to execute the shell is indistinguishable from the shell's
161failure to execute command, or an immediate exit of the command.
162The only hint is an exit status of 127.
163.Pp
164The
165.Fn popen
166argument always calls
167.Xr sh ;
168it never calls
169.Xr csh .
170.Sh HISTORY
171A
172.Fn popen
173and a
174.Fn pclose
175function appeared in
176.At v7 .
177