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