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