termios.3: wfix
[man-pages.git] / man3 / popen.3
blob5a68a38a9adb5157e66d6afa790ed85f940dc573
1 .\" Copyright 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\"    notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\"    notice, this list of conditions and the following disclaimer in the
12 .\"    documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\"    must display the following acknowledgement:
15 .\"     This product includes software developed by the University of
16 .\"     California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\"    may be used to endorse or promote products derived from this software
19 .\"    without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\"     @(#)popen.3     6.4 (Berkeley) 4/30/91
35 .\"
36 .\" Converted for Linux, Mon Nov 29 14:45:38 1993, faith@cs.unc.edu
37 .\" Modified Sat May 18 20:37:44 1996 by Martin Schulze (joey@linux.de)
38 .\" Modified 7 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
39 .\"
40 .TH POPEN 3  2021-03-22 "GNU" "Linux Programmer's Manual"
41 .SH NAME
42 popen, pclose \- pipe stream to or from a process
43 .SH SYNOPSIS
44 .nf
45 .B #include <stdio.h>
46 .PP
47 .BI "FILE *popen(const char *" command ", const char *" type );
48 .BI "int pclose(FILE *" stream );
49 .fi
50 .PP
51 .RS -4
52 Feature Test Macro Requirements for glibc (see
53 .BR feature_test_macros (7)):
54 .RE
55 .PP
56 .BR popen (),
57 .BR pclose ():
58 .nf
59     _POSIX_C_SOURCE >= 2
60         || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
61 .fi
62 .SH DESCRIPTION
63 The
64 .BR popen ()
65 function opens a process by creating a pipe, forking, and invoking the
66 shell.
67 Since a pipe is by definition unidirectional, the
68 .I type
69 argument may specify only reading or writing, not both; the resulting
70 stream is correspondingly read-only or write-only.
71 .PP
72 The
73 .I command
74 argument is a pointer to a null-terminated string containing a shell
75 command line.
76 This command is passed to
77 .I /bin/sh
78 using the
79 .B \-c
80 flag; interpretation, if any, is performed by the shell.
81 .PP
82 The
83 .I type
84 argument is a pointer to a null-terminated string which must contain
85 either the letter \(aqr\(aq for reading or the letter \(aqw\(aq for writing.
86 Since glibc 2.9,
87 this argument can additionally include the letter \(aqe\(aq,
88 which causes the close-on-exec flag
89 .RB ( FD_CLOEXEC )
90 to be set on the underlying file descriptor;
91 see the description of the
92 .B O_CLOEXEC
93 flag in
94 .BR open (2)
95 for reasons why this may be useful.
96 .PP
97 The return value from
98 .BR popen ()
99 is a normal standard I/O stream in all respects save that it must be closed
100 with
101 .BR pclose ()
102 rather than
103 .BR fclose (3).
104 Writing to such a stream writes to the standard input of the command; the
105 command's standard output is the same as that of the process that called
106 .BR popen (),
107 unless this is altered by the command itself.
108 Conversely, reading from
109 the stream reads the command's standard output, and the command's
110 standard input is the same as that of the process that called
111 .BR popen ().
113 Note that output
114 .BR popen ()
115 streams are block buffered by default.
118 .BR pclose ()
119 function waits for the associated process to terminate and returns the exit
120 status of the command as returned by
121 .BR wait4 (2).
122 .SH RETURN VALUE
123 .BR popen ():
124 on success, returns a pointer to an open stream that
125 can be used to read or write to the pipe;
126 if the
127 .BR fork (2)
129 .BR pipe (2)
130 calls fail, or if the function cannot allocate memory,
131 NULL is returned.
133 .BR pclose ():
134 on success, returns the exit status of the command; if
135 .\" These conditions actually give undefined results, so I commented
136 .\" them out.
137 .\" .I stream
138 .\" is not associated with a "popen()ed" command, if
139 .\".I stream
140 .\" already "pclose()d", or if
141 .BR wait4 (2)
142 returns an error, or some other error is detected,
143 \-1 is returned.
145 On failure, both functions set
146 .I errno
147 to indicate the error.
148 .SH ERRORS
150 .BR popen ()
151 function does not set
152 .I errno
153 if memory allocation fails.
154 If the underlying
155 .BR fork (2)
157 .BR pipe (2)
158 fails,
159 .I errno
160 is set to indicate the error.
161 If the
162 .I type
163 argument is invalid, and this condition is detected,
164 .I errno
165 is set to
166 .BR EINVAL .
169 .BR pclose ()
170 cannot obtain the child status,
171 .I errno
172 is set to
173 .BR ECHILD .
174 .SH ATTRIBUTES
175 For an explanation of the terms used in this section, see
176 .BR attributes (7).
177 .ad l
180 allbox;
181 lbx lb lb
182 l l l.
183 Interface       Attribute       Value
185 .BR popen (),
186 .BR pclose ()
187 T}      Thread safety   MT-Safe
191 .sp 1
192 .SH CONFORMING TO
193 POSIX.1-2001, POSIX.1-2008.
195 The \(aqe\(aq value for
196 .I type
197 is a Linux extension.
198 .SH NOTES
199 .BR Note :
200 carefully read Caveats in
201 .BR system (3).
202 .SH BUGS
203 Since the standard input of a command opened for reading shares its seek
204 offset with the process that called
205 .BR popen (),
206 if the original process has done a buffered read, the command's input
207 position may not be as expected.
208 Similarly, the output from a command
209 opened for writing may become intermingled with that of the original
210 process.
211 The latter can be avoided by calling
212 .BR fflush (3)
213 before
214 .BR popen ().
216 Failure to execute the shell is indistinguishable from the shell's failure
217 to execute command, or an immediate exit of the command.
218 The only hint is an exit status of 127.
219 .\" .SH HISTORY
220 .\" A
221 .\" .BR popen ()
222 .\" and a
223 .\" .BR pclose ()
224 .\" function appeared in Version 7 AT&T UNIX.
225 .SH SEE ALSO
226 .BR sh (1),
227 .BR fork (2),
228 .BR pipe (2),
229 .BR wait4 (2),
230 .BR fclose (3),
231 .BR fflush (3),
232 .BR fopen (3),
233 .BR stdio (3),
234 .BR system (3)