1 .\" Copyright (c) 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 .Fn popen "const char *command" "const char *type"
43 .Fn pclose "FILE *stream"
49 a process by creating a bidirectional pipe
51 and invoking the shell.
52 Any streams opened by previous
54 calls in the parent process are closed in the new child process.
57 was implemented with a unidirectional pipe;
58 hence many implementations of
62 argument to specify reading or writing, not both.
65 is now implemented using a bidirectional pipe, the
67 argument may request a bidirectional data flow.
70 argument is a pointer to a null-terminated string
77 for reading and writing.
81 argument may be augmented by appending an
83 to set the descriptor's close-on-exec flag.
90 for reading and writing.
91 Use of this flag is important when operating in threaded environments.
95 argument is a pointer to a null-terminated string
96 containing a shell command line.
97 This command is passed to
101 flag; interpretation, if any, is performed by the shell.
103 The return value from
107 stream in all respects
108 save that it must be closed with
112 Writing to such a stream
113 writes to the standard input of the command;
114 the command's standard output is the same as that of the process that called
116 unless this is altered by the command itself.
117 Conversely, reading from a
119 stream reads the command's standard output, and
120 the command's standard input is the same as that of the process that called
125 streams are fully buffered by default.
128 automatically interlocks and closes descriptors associated with other
131 files in any sub-process it creates, preventing file descriptor leakage
134 calls in a thread-safe manner.
137 has no control over fork or fork/exec sequences run by other threads which
138 do not use the popen mechanism and in this situation it is likely that
139 popen descriptors will leak into those sub-processes.
140 It is recommended that the
142 flag be used to prevent descriptor leakages into miscellanious fork/exec
143 sequences that might be executed by other threads in a multi-threaded
148 function waits for the associated process to terminate
149 and returns the exit status of the command
162 or if it cannot allocate memory.
169 is not associated with a
181 function does not reliably set
201 Bidirectional functionality was added in
204 Since the standard input of a command opened for reading
205 shares its seek offset with the process that called
207 if the original process has done a buffered read,
208 the command's input position may not be as expected.
209 Similarly, the output from a command opened for writing
210 may become intermingled with that of the original process.
211 The latter can be avoided by calling
216 Failure to execute the shell
217 is indistinguishable from the shell's failure to execute command,
218 or an immediate exit of the command.
219 The only hint is an exit status of 127.