2 .\" Copyright 1991 The Regents of the University of California.
3 .\" All rights reserved.
5 .\" SPDX-License-Identifier: BSD-4-Clause-UC
7 .\" @(#)popen.3 6.4 (Berkeley) 4/30/91
9 .\" Converted for Linux, Mon Nov 29 14:45:38 1993, faith@cs.unc.edu
10 .\" Modified Sat May 18 20:37:44 1996 by Martin Schulze (joey@linux.de)
11 .\" Modified 7 May 1998 by Joseph S. Myers (jsm28@cam.ac.uk)
13 .TH popen 3 (date) "Linux man-pages (unreleased)"
15 popen, pclose \- pipe stream to or from a process
18 .RI ( libc ", " \-lc )
23 .BI "FILE *popen(const char *" command ", const char *" type );
24 .BI "int pclose(FILE *" stream );
28 Feature Test Macro Requirements for glibc (see
29 .BR feature_test_macros (7)):
36 || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
41 function opens a process by creating a pipe, forking, and invoking the
43 Since a pipe is by definition unidirectional, the
45 argument may specify only reading or writing, not both; the resulting
46 stream is correspondingly read-only or write-only.
50 argument is a pointer to a null-terminated string containing a shell
52 This command is passed to
56 flag; interpretation, if any, is performed by the shell.
60 argument is a pointer to a null-terminated string which must contain
61 either the letter \[aq]r\[aq] for reading or the letter \[aq]w\[aq] for writing.
63 this argument can additionally include the letter \[aq]e\[aq],
64 which causes the close-on-exec flag
66 to be set on the underlying file descriptor;
67 see the description of the
71 for reasons why this may be useful.
75 is a normal standard I/O stream in all respects save that it must be closed
80 Writing to such a stream writes to the standard input of the command; the
81 command's standard output is the same as that of the process that called
83 unless this is altered by the command itself.
84 Conversely, reading from
85 the stream reads the command's standard output, and the command's
86 standard input is the same as that of the process that called
91 streams are block buffered by default.
95 function waits for the associated process to terminate and returns the exit
96 status of the command as returned by
100 on success, returns a pointer to an open stream that
101 can be used to read or write to the pipe;
106 calls fail, or if the function cannot allocate memory,
110 on success, returns the exit status of the command; if
111 .\" These conditions actually give undefined results, so I commented
114 .\" is not associated with a "popen()ed" command, if
116 .\" already "pclose()d", or if
118 returns an error, or some other error is detected,
121 On failure, both functions set
123 to indicate the error.
127 function does not set
129 if memory allocation fails.
136 is set to indicate the error.
139 argument is invalid, and this condition is detected,
146 cannot obtain the child status,
151 For an explanation of the terms used in this section, see
159 Interface Attribute Value
163 T} Thread safety MT-Safe
169 The \[aq]e\[aq] value for
171 is a Linux extension.
177 Carefully read Caveats in
180 Since the standard input of a command opened for reading shares its seek
181 offset with the process that called
183 if the original process has done a buffered read, the command's input
184 position may not be as expected.
185 Similarly, the output from a command
186 opened for writing may become intermingled with that of the original
188 The latter can be avoided by calling
193 Failure to execute the shell is indistinguishable from the shell's failure
194 to execute command, or an immediate exit of the command.
195 The only hint is an exit status of 127.
201 .\" function appeared in Version 7 AT&T UNIX.