1 .\" Copyright 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\" @(#)popen.3 6.4 (Berkeley) 4/30/91
8 .\" Converted for Linux, Mon Nov 29 14:45:38 1993, faith@cs.unc.edu
9 .\" Modified Sat May 18 20:37:44 1996 by Martin Schulze (joey@linux.de)
10 .\" Modified 7 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
12 .TH popen 3 (date) "Linux man-pages (unreleased)"
14 popen, pclose \- pipe stream to or from a process
17 .RI ( libc ", " \-lc )
22 .BI "FILE *popen(const char *" command ", const char *" type );
23 .BI "int pclose(FILE *" stream );
27 Feature Test Macro Requirements for glibc (see
28 .BR feature_test_macros (7)):
35 || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
40 function opens a process by creating a pipe, forking, and invoking the
42 Since a pipe is by definition unidirectional, the
44 argument may specify only reading or writing, not both; the resulting
45 stream is correspondingly read-only or write-only.
49 argument is a pointer to a null-terminated string containing a shell
51 This command is passed to
55 flag; interpretation, if any, is performed by the shell.
59 argument is a pointer to a null-terminated string which must contain
60 either the letter \(aqr\(aq for reading or the letter \(aqw\(aq for writing.
62 this argument can additionally include the letter \(aqe\(aq,
63 which causes the close-on-exec flag
65 to be set on the underlying file descriptor;
66 see the description of the
70 for reasons why this may be useful.
74 is a normal standard I/O stream in all respects save that it must be closed
79 Writing to such a stream writes to the standard input of the command; the
80 command's standard output is the same as that of the process that called
82 unless this is altered by the command itself.
83 Conversely, reading from
84 the stream reads the command's standard output, and the command's
85 standard input is the same as that of the process that called
90 streams are block buffered by default.
94 function waits for the associated process to terminate and returns the exit
95 status of the command as returned by
99 on success, returns a pointer to an open stream that
100 can be used to read or write to the pipe;
105 calls fail, or if the function cannot allocate memory,
109 on success, returns the exit status of the command; if
110 .\" These conditions actually give undefined results, so I commented
113 .\" is not associated with a "popen()ed" command, if
115 .\" already "pclose()d", or if
117 returns an error, or some other error is detected,
120 On failure, both functions set
122 to indicate the error.
126 function does not set
128 if memory allocation fails.
135 is set to indicate the error.
138 argument is invalid, and this condition is detected,
145 cannot obtain the child status,
150 For an explanation of the terms used in this section, see
158 Interface Attribute Value
162 T} Thread safety MT-Safe
168 POSIX.1-2001, POSIX.1-2008.
170 The \(aqe\(aq value for
172 is a Linux extension.
175 carefully read Caveats in
178 Since the standard input of a command opened for reading shares its seek
179 offset with the process that called
181 if the original process has done a buffered read, the command's input
182 position may not be as expected.
183 Similarly, the output from a command
184 opened for writing may become intermingled with that of the original
186 The latter can be avoided by calling
191 Failure to execute the shell is indistinguishable from the shell's failure
192 to execute command, or an immediate exit of the command.
193 The only hint is an exit status of 127.
199 .\" function appeared in Version 7 AT&T UNIX.