]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/popen.3
Libc-339.tar.gz
[apple/libc.git] / gen / FreeBSD / popen.3
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: src/lib/libc/gen/popen.3,v 1.15 2003/01/03 05:21:59 tjr Exp $
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
52 The
53 .Fn popen
54 function
55 .Dq opens
56 a process by creating a bidirectional pipe
57 forking,
58 and invoking the shell.
59 Any streams opened by previous
60 .Fn popen
61 calls in the parent process are closed in the new child process.
62 Historically,
63 .Fn popen
64 was implemented with a unidirectional pipe;
65 hence many implementations of
66 .Fn popen
67 only allow the
68 .Fa type
69 argument to specify reading or writing, not both.
70 Since
71 .Fn popen
72 is now implemented using a bidirectional pipe, the
73 .Fa type
74 argument may request a bidirectional data flow.
75 The
76 .Fa type
77 argument is a pointer to a null-terminated string
78 which must be
79 .Ql r
80 for reading,
81 .Ql w
82 for writing, or
83 .Ql r+
84 for reading and writing.
85 .Pp
86 The
87 .Fa command
88 argument is a pointer to a null-terminated string
89 containing a shell command line.
90 This command is passed to
91 .Pa /bin/sh
92 using the
93 .Fl c
94 flag; interpretation, if any, is performed by the shell.
95 .Pp
96 The return value from
97 .Fn popen
98 is a normal standard
99 .Tn I/O
100 stream in all respects
101 save that it must be closed with
102 .Fn pclose
103 rather than
104 .Fn fclose .
105 Writing to such a stream
106 writes to the standard input of the command;
107 the command's standard output is the same as that of the process that called
108 .Fn popen ,
109 unless this is altered by the command itself.
110 Conversely, reading from a
111 .Dq popened
112 stream reads the command's standard output, and
113 the command's standard input is the same as that of the process that called
114 .Fn popen .
115 .Pp
116 Note that output
117 .Fn popen
118 streams are fully buffered by default.
119 .Pp
120 The
121 .Fn pclose
122 function waits for the associated process to terminate
123 and returns the exit status of the command
124 as returned by
125 .Fn wait4 .
126 .Sh RETURN VALUES
127 The
128 .Fn popen
129 function returns
130 .Dv NULL
131 if the
132 .Xr fork 2
133 or
134 .Xr pipe 2
135 calls fail,
136 or if it cannot allocate memory.
137 .Pp
138 The
139 .Fn pclose
140 function
141 returns \-1 if
142 .Fa stream
143 is not associated with a
144 .Dq popened
145 command, if
146 .Fa stream
147 already
148 .Dq pclosed ,
149 or if
150 .Xr wait4
151 returns an error.
152 .Sh ERRORS
153 The
154 .Fn popen
155 function 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
168 Since the standard input of a command opened for reading
169 shares its seek offset with the process that called
170 .Fn popen ,
171 if the original process has done a buffered read,
172 the command's input position may not be as expected.
173 Similarly, the output from a command opened for writing
174 may become intermingled with that of the original process.
175 The latter can be avoided by calling
176 .Xr fflush 3
177 before
178 .Fn popen .
179 .Pp
180 Failure to execute the shell
181 is indistinguishable from the shell's failure to execute command,
182 or an immediate exit of the command.
183 The only hint is an exit status of 127.
184 .Pp
185 The
186 .Fn popen
187 function
188 always calls
189 .Xr sh 1 ,
190 never calls
191 .Xr csh 1 .
192 .Sh HISTORY
193 A
194 .Fn popen
195 and a
196 .Fn pclose
197 function appeared in
198 .At v7 .
199 .Pp
200 Bidirectional functionality was added in
201 .Fx 2.2.6 .