]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/popen.3
Libc-1082.50.1.tar.gz
[apple/libc.git] / gen / FreeBSD / popen.3
CommitLineData
5b2abdfb
A
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
3d9156a7 33.\" $FreeBSD: src/lib/libc/gen/popen.3,v 1.16 2003/06/08 10:01:51 charnier Exp $
5b2abdfb
A
34.\"
35.Dd May 3, 1995
36.Dt POPEN 3
37.Os
38.Sh NAME
ad3c9f2a
A
39.Nm pclose ,
40.Nm popen
5b2abdfb
A
41.Nd process
42.Tn I/O
43.Sh LIBRARY
44.Lb libc
45.Sh SYNOPSIS
46.In stdio.h
47.Ft FILE *
ad3c9f2a
A
48.Fo popen
49.Fa "const char *command"
50.Fa "const char *mode"
51.Fc
5b2abdfb 52.Ft int
ad3c9f2a
A
53.Fo pclose
54.Fa "FILE *stream"
55.Fc
5b2abdfb
A
56.Sh DESCRIPTION
57The
58.Fn popen
59function
60.Dq opens
ad3c9f2a 61a process by creating a bidirectional pipe, forking,
5b2abdfb
A
62and invoking the shell.
63Any streams opened by previous
64.Fn popen
65calls in the parent process are closed in the new child process.
66Historically,
67.Fn popen
68was implemented with a unidirectional pipe;
ad3c9f2a 69hence, many implementations of
5b2abdfb
A
70.Fn popen
71only allow the
ad3c9f2a 72.Fa mode
5b2abdfb 73argument to specify reading or writing, not both.
ad3c9f2a 74Because
5b2abdfb
A
75.Fn popen
76is now implemented using a bidirectional pipe, the
ad3c9f2a 77.Fa mode
5b2abdfb
A
78argument may request a bidirectional data flow.
79The
ad3c9f2a 80.Fa mode
5b2abdfb
A
81argument is a pointer to a null-terminated string
82which must be
83.Ql r
84for reading,
85.Ql w
86for writing, or
87.Ql r+
88for reading and writing.
89.Pp
90The
91.Fa command
92argument is a pointer to a null-terminated string
93containing a shell command line.
94This command is passed to
ad3c9f2a 95.Pa /bin/sh ,
5b2abdfb
A
96using the
97.Fl c
98flag; interpretation, if any, is performed by the shell.
99.Pp
100The return value from
101.Fn popen
102is a normal standard
103.Tn I/O
ad3c9f2a 104stream in all respects,
5b2abdfb
A
105save that it must be closed with
106.Fn pclose
107rather than
108.Fn fclose .
109Writing to such a stream
110writes to the standard input of the command;
111the command's standard output is the same as that of the process that called
112.Fn popen ,
113unless this is altered by the command itself.
114Conversely, reading from a
115.Dq popened
116stream reads the command's standard output, and
117the command's standard input is the same as that of the process that called
118.Fn popen .
119.Pp
120Note that output
121.Fn popen
ad3c9f2a 122streams are fully buffered, by default.
5b2abdfb
A
123.Pp
124The
125.Fn pclose
ad3c9f2a
A
126function waits for the associated process to terminate;
127it returns the exit status of the command,
5b2abdfb 128as returned by
3d9156a7 129.Xr wait4 2 .
5b2abdfb
A
130.Sh RETURN VALUES
131The
132.Fn popen
133function returns
134.Dv NULL
135if the
136.Xr fork 2
137or
138.Xr pipe 2
139calls fail,
140or if it cannot allocate memory.
141.Pp
142The
143.Fn pclose
144function
145returns \-1 if
146.Fa stream
147is not associated with a
148.Dq popened
149command, if
150.Fa stream
151already
152.Dq pclosed ,
153or if
3d9156a7 154.Xr wait4 2
5b2abdfb
A
155returns an error.
156.Sh ERRORS
157The
158.Fn popen
159function does not reliably set
160.Va errno .
161.Sh SEE ALSO
162.Xr sh 1 ,
163.Xr fork 2 ,
164.Xr pipe 2 ,
165.Xr wait4 2 ,
166.Xr fclose 3 ,
167.Xr fflush 3 ,
168.Xr fopen 3 ,
169.Xr stdio 3 ,
170.Xr system 3
171.Sh BUGS
172Since the standard input of a command opened for reading
173shares its seek offset with the process that called
174.Fn popen ,
175if the original process has done a buffered read,
176the command's input position may not be as expected.
177Similarly, the output from a command opened for writing
178may become intermingled with that of the original process.
179The latter can be avoided by calling
180.Xr fflush 3
181before
182.Fn popen .
183.Pp
184Failure to execute the shell
185is indistinguishable from the shell's failure to execute command,
186or an immediate exit of the command.
187The only hint is an exit status of 127.
188.Pp
189The
190.Fn popen
9385eb3d 191function
5b2abdfb
A
192always calls
193.Xr sh 1 ,
194never calls
195.Xr csh 1 .
196.Sh HISTORY
197A
198.Fn popen
199and a
200.Fn pclose
201function appeared in
202.At v7 .
203.Pp
204Bidirectional functionality was added in
205.Fx 2.2.6 .